Skip to content

Commit 467e954

Browse files
fix(tasks): serialize template subtask creation
Co-authored-by: Neil Raina <makeitraina@users.noreply.github.com>
1 parent c5e163c commit 467e954

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

src/constants/tasks.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { subtaskTemplateBatchSize } from '@/constants/tasks'
2+
import { runInBatches } from '@/utils/array'
3+
4+
describe('task constants', () => {
5+
it('keeps subtask template fan-out within the production Prisma pool', async () => {
6+
expect(subtaskTemplateBatchSize).toBe(1)
7+
8+
const state = { active: 0, max: 0 }
9+
10+
await runInBatches([1, 2, 3], subtaskTemplateBatchSize, async () => {
11+
state.active += 1
12+
state.max = Math.max(state.max, state.active)
13+
await new Promise((resolve) => setTimeout(resolve, 1))
14+
state.active -= 1
15+
})
16+
17+
expect(state.max).toBe(1)
18+
})
19+
})

src/constants/tasks.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const maxSubTaskDepth = 1
22

3-
// Bounds how many subtasks we create concurrently when applying a template, so a
4-
// template with many sub-templates can't exhaust the DB connection pool.
5-
export const subtaskTemplateBatchSize = 5
3+
// Production Prisma pool limit is 2; each subtask creation performs multiple DB
4+
// writes, so keep template application serial within a single request.
5+
export const subtaskTemplateBatchSize = 1

0 commit comments

Comments
 (0)