plantr 2.0.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +31 -25
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1050 -251
- package/dist/src/cli.d.ts +13 -3
- package/dist/src/cli.d.ts.map +1 -0
- package/dist/src/create-seeder-instance.d.ts +64 -78
- package/dist/src/create-seeder-instance.d.ts.map +1 -0
- package/dist/src/define-seeder.d.ts +4 -3
- package/dist/src/define-seeder.d.ts.map +1 -0
- package/dist/src/dependency-resolver.d.ts +2 -1
- package/dist/src/dependency-resolver.d.ts.map +1 -0
- package/dist/src/expect.d.ts +41 -0
- package/dist/src/expect.d.ts.map +1 -0
- package/dist/src/formatting.d.ts +36 -14
- package/dist/src/formatting.d.ts.map +1 -0
- package/dist/src/lazy-faker.d.ts +12 -0
- package/dist/src/lazy-faker.d.ts.map +1 -0
- package/dist/src/run-seeders.d.ts +46 -26
- package/dist/src/run-seeders.d.ts.map +1 -0
- package/dist/src/seeding-state.d.ts +15 -4
- package/dist/src/seeding-state.d.ts.map +1 -0
- package/dist/src/step.d.ts +79 -22
- package/dist/src/step.d.ts.map +1 -0
- package/dist/src/types.d.ts +121 -38
- package/dist/src/types.d.ts.map +1 -0
- package/dist/state.d.ts +2 -1
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +10 -2
- package/index.ts +70 -28
- package/package.json +5 -2
- package/readme.md +102 -52
- package/src/cli.ts +155 -22
- package/src/create-seeder-instance.ts +252 -111
- package/src/define-seeder.ts +14 -7
- package/src/dependency-resolver.ts +17 -28
- package/src/expect.ts +502 -0
- package/src/formatting.ts +142 -37
- package/src/lazy-faker.ts +46 -0
- package/src/run-seeders.ts +352 -122
- package/src/seeding-state.ts +27 -5
- package/src/step.ts +208 -64
- package/src/types.ts +173 -41
- package/state.ts +1 -1
package/src/run-seeders.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { resolveDependencyOrder, UnknownSeederError } from './dependency-resolver'
|
|
1
|
+
import { resolveDependencyOrder, UnknownSeederError } from './dependency-resolver.js'
|
|
2
2
|
import {
|
|
3
|
+
printConcurrentComplete,
|
|
4
|
+
printConcurrentSkipped,
|
|
3
5
|
printErrors,
|
|
4
6
|
printResultsTable,
|
|
5
7
|
printSeederComplete,
|
|
@@ -7,25 +9,55 @@ import {
|
|
|
7
9
|
printSeederSkipped,
|
|
8
10
|
printStartBanner,
|
|
9
11
|
printSummary,
|
|
10
|
-
} from './formatting'
|
|
11
|
-
import
|
|
12
|
-
import type {
|
|
12
|
+
} from './formatting.js'
|
|
13
|
+
import { enterSeeding, exitSeeding } from './seeding-state.js'
|
|
14
|
+
import type { StepRecord } from './step.js'
|
|
15
|
+
import type {
|
|
16
|
+
MaybePromise,
|
|
17
|
+
RunSeedersOptions,
|
|
18
|
+
RunSeedersResult,
|
|
19
|
+
SeederAny,
|
|
20
|
+
SeederResult,
|
|
21
|
+
SeederResultFor,
|
|
22
|
+
} from './types.js'
|
|
23
|
+
|
|
24
|
+
/** Parsed description of a database target URL (without credentials) */
|
|
25
|
+
export type TargetDescription = {
|
|
26
|
+
label: string
|
|
27
|
+
local: boolean
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Per-seeder scope handle returned by `InternalSeederConfig.runSeederScope` */
|
|
31
|
+
export type SeederScopeHandle = {
|
|
32
|
+
record?: StepRecord | undefined
|
|
33
|
+
run: <T>(fn: () => Promise<T>) => Promise<T>
|
|
34
|
+
}
|
|
13
35
|
|
|
14
36
|
/**
|
|
15
37
|
* Internal configuration used by the runner.
|
|
16
|
-
* This is created by createSeederInstance and includes the wrapped context factory.
|
|
38
|
+
* This is created by `createSeederInstance` and includes the wrapped context factory.
|
|
17
39
|
*/
|
|
18
40
|
export type InternalSeederConfig<TContext> = {
|
|
19
41
|
/** Factory function that creates the full context; called once, after onBeforeAll */
|
|
20
|
-
context: () =>
|
|
21
|
-
/** Called
|
|
22
|
-
|
|
42
|
+
context: () => MaybePromise<TContext>
|
|
43
|
+
/** Called once after onBeforeAll to preload step cache entries (when cache.getAll is configured) */
|
|
44
|
+
preloadCache?: (() => MaybePromise<void>) | undefined
|
|
45
|
+
/** Called once before onAfterAll to flush buffered step cache writes (when cache.setMany is configured) */
|
|
46
|
+
flushCache?: (() => MaybePromise<void>) | undefined
|
|
47
|
+
/** Called before each seeder runs (legacy sequential hook); may return the record its steps fill */
|
|
48
|
+
beforeSeeder?: ((name: string, options: { fresh: boolean }) => StepRecord | undefined) | undefined
|
|
49
|
+
/** Concurrency-safe scope wrapper for running a seeder (used by createSeederInstance) */
|
|
50
|
+
createSeederScope?:
|
|
51
|
+
| ((options: { name: string; fresh: boolean; concurrent: boolean }) => SeederScopeHandle)
|
|
52
|
+
| undefined
|
|
53
|
+
/** Banner title (default: `'Seeders'`) */
|
|
54
|
+
bannerName?: string | undefined
|
|
23
55
|
/** Database URL the run writes to; non-local hosts are refused unless allowRemote */
|
|
24
|
-
target?: () => string | undefined
|
|
56
|
+
target?: (() => string | undefined) | undefined
|
|
25
57
|
/** Called before the context is created */
|
|
26
|
-
onBeforeAll?: () => void |
|
|
58
|
+
onBeforeAll?: (() => MaybePromise<void>) | undefined
|
|
27
59
|
/** Called after all seeders complete, also when a seeder or the context factory throws */
|
|
28
|
-
onAfterAll?: () => void |
|
|
60
|
+
onAfterAll?: ((ctx: TContext | undefined) => MaybePromise<void>) | undefined
|
|
29
61
|
}
|
|
30
62
|
|
|
31
63
|
/**
|
|
@@ -44,25 +76,30 @@ const isLocalHost = (hostPort: string): boolean => {
|
|
|
44
76
|
if (hostPort.startsWith('/')) return true // socket directory
|
|
45
77
|
const lower = hostPort.toLowerCase()
|
|
46
78
|
const bracketed = /^\[([^\]]*)\]/.exec(lower)
|
|
79
|
+
const hasPort = bracketed ? /:\d+$/.test(lower.slice(bracketed[0].length)) : /:\d+$/.test(lower)
|
|
47
80
|
const host = bracketed ? (bracketed[1] ?? '') : lower.replace(/:\d+$/, '')
|
|
48
81
|
return (
|
|
82
|
+
host === 'localhost' ||
|
|
49
83
|
host === '::1' ||
|
|
50
84
|
host === '0.0.0.0' ||
|
|
51
85
|
host === 'host.docker.internal' ||
|
|
52
86
|
host.endsWith('.localhost') ||
|
|
53
87
|
/^127\./.test(host) ||
|
|
54
|
-
!/[.:]/.test(host)
|
|
88
|
+
(hasPort && host.length > 0 && !/[.:]/.test(host))
|
|
55
89
|
)
|
|
56
90
|
}
|
|
57
91
|
|
|
58
92
|
/**
|
|
59
93
|
* Describe a database URL without its credentials and say whether every host it names
|
|
60
|
-
* is local: loopback
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* (
|
|
94
|
+
* is local: loopback (`localhost`, `127.*`, `::1`, `0.0.0.0`), `*.localhost`,
|
|
95
|
+
* `host.docker.internal`, a socket or `file:` path, or a single-label hostname with an
|
|
96
|
+
* explicit port (such as a compose service like `postgres:5432`). A bare name without a
|
|
97
|
+
* port (such as a cloud project URL like `firestore://my-prod-project`) is treated as
|
|
98
|
+
* remote. A `host=` parameter overrides the URL's host, as libpq does. Hand-parsed so
|
|
99
|
+
* driver formats `URL` rejects still work (several hosts, `sqlserver://host;key=value`,
|
|
100
|
+
* sockets with credentials).
|
|
64
101
|
*/
|
|
65
|
-
export function describeTarget(target: string):
|
|
102
|
+
export function describeTarget(target: string): TargetDescription {
|
|
66
103
|
if (/^file:/i.test(target)) return { label: target.replace(/[?#].*$/, ''), local: true }
|
|
67
104
|
const scheme = /^[a-z][\w+.-]*:\/\//i.exec(target)
|
|
68
105
|
if (!scheme) return { label: 'a target that is not a URL', local: false }
|
|
@@ -73,32 +110,43 @@ export function describeTarget(target: string): { label: string; local: boolean
|
|
|
73
110
|
const query = queryStart === -1 ? '' : rest.slice(queryStart)
|
|
74
111
|
// Drop credentials: everything up to the last @ before the query
|
|
75
112
|
const authorityAndPath = beforeQuery.slice(beforeQuery.lastIndexOf('@') + 1)
|
|
76
|
-
const
|
|
113
|
+
const pathStart = authorityAndPath.search(/[/;]/)
|
|
114
|
+
const authority = pathStart === -1 ? authorityAndPath : authorityAndPath.slice(0, pathStart)
|
|
115
|
+
const path = pathStart === -1 ? '' : authorityAndPath.slice(pathStart)
|
|
77
116
|
let hosts = authority.split(',').filter(Boolean)
|
|
78
117
|
const hostParam = /[?&;]host=([^&;#]*)/i.exec(`${query}${path.startsWith(';') ? path : ''}`)?.[1]
|
|
79
|
-
if (hostParam)
|
|
118
|
+
if (hostParam) {
|
|
119
|
+
try {
|
|
120
|
+
hosts = decodeURIComponent(hostParam).split(',').filter(Boolean)
|
|
121
|
+
} catch {
|
|
122
|
+
return { label: 'a target with an invalid host parameter', local: false }
|
|
123
|
+
}
|
|
124
|
+
}
|
|
80
125
|
|
|
81
|
-
const
|
|
126
|
+
const pathWithoutParams = path.startsWith('/') ? (path.split(';', 1)[0] ?? '') : ''
|
|
127
|
+
const pathLabel = pathWithoutParams.length > 1 ? pathWithoutParams : ''
|
|
128
|
+
const label = `${hosts.join(',') || 'local socket'}${pathLabel}`
|
|
82
129
|
return { label, local: hosts.every(isLocalHost) }
|
|
83
130
|
}
|
|
84
131
|
|
|
85
132
|
/**
|
|
86
|
-
* Core seeder runner - executes seeders in dependency order
|
|
133
|
+
* Core seeder runner - executes seeders in dependency order (sequentially or in parallel)
|
|
87
134
|
*
|
|
88
135
|
* @param seeders - Array of seeders to run
|
|
89
136
|
* @param config - Instance configuration (context factory, lifecycle hooks)
|
|
90
|
-
* @param options - Runner options (verbose output, etc.)
|
|
91
|
-
* @returns Object containing results and
|
|
92
|
-
* @throws Dependency errors, `UnknownSeederError
|
|
137
|
+
* @param options - Runner options (verbose output, concurrency, etc.)
|
|
138
|
+
* @returns Object containing `success`, `results`, and `outputs`
|
|
139
|
+
* @throws Dependency errors, `UnknownSeederError`, `SeedTargetError`, or `RangeError` (invalid concurrency), before any hook runs
|
|
93
140
|
*/
|
|
94
|
-
export async function runSeedersCore<
|
|
95
|
-
|
|
141
|
+
export async function runSeedersCore<
|
|
142
|
+
TContext,
|
|
143
|
+
const TSeeders extends readonly SeederAny[] = readonly SeederAny[],
|
|
144
|
+
const TOptions extends RunSeedersOptions = RunSeedersOptions,
|
|
145
|
+
>(
|
|
146
|
+
seeders: TSeeders,
|
|
96
147
|
config: InternalSeederConfig<TContext>,
|
|
97
|
-
options:
|
|
98
|
-
): Promise<{
|
|
99
|
-
results: SeederResult[]
|
|
100
|
-
success: boolean
|
|
101
|
-
}> {
|
|
148
|
+
options: TOptions = {} as TOptions,
|
|
149
|
+
): Promise<RunSeedersResult<TSeeders, TOptions>> {
|
|
102
150
|
const {
|
|
103
151
|
printResults = true,
|
|
104
152
|
verbose = true,
|
|
@@ -106,17 +154,30 @@ export async function runSeedersCore<TContext>(
|
|
|
106
154
|
only,
|
|
107
155
|
fresh = false,
|
|
108
156
|
allowRemote = false,
|
|
157
|
+
concurrency = 1,
|
|
109
158
|
} = options
|
|
110
159
|
|
|
111
|
-
|
|
160
|
+
if (
|
|
161
|
+
Number.isNaN(concurrency) ||
|
|
162
|
+
concurrency < 1 ||
|
|
163
|
+
(Number.isFinite(concurrency) && !Number.isInteger(concurrency))
|
|
164
|
+
) {
|
|
165
|
+
throw new RangeError(
|
|
166
|
+
`Invalid concurrency (${String(concurrency)}): expected a positive integer or Infinity`,
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Validate everything before setting the seeding flag or touching the database
|
|
112
171
|
const orderedNames = resolveDependencyOrder(seeders, only)
|
|
113
172
|
|
|
114
173
|
const seederMap = new Map<string, SeederAny>()
|
|
115
174
|
for (const seeder of seeders) {
|
|
116
175
|
seederMap.set(seeder.name, seeder)
|
|
117
176
|
}
|
|
118
|
-
|
|
119
|
-
|
|
177
|
+
|
|
178
|
+
const freshSet = Array.isArray(fresh) ? new Set<string>(fresh) : undefined
|
|
179
|
+
if (freshSet) {
|
|
180
|
+
for (const name of freshSet) {
|
|
120
181
|
if (!seederMap.has(name)) throw new UnknownSeederError(name, [...seederMap.keys()])
|
|
121
182
|
}
|
|
122
183
|
}
|
|
@@ -127,125 +188,294 @@ export async function runSeedersCore<TContext>(
|
|
|
127
188
|
throw new SeedTargetError(target.label)
|
|
128
189
|
}
|
|
129
190
|
|
|
130
|
-
|
|
131
|
-
printStartBanner(target)
|
|
132
|
-
}
|
|
191
|
+
const isFresh = (name: string): boolean => fresh === true || (freshSet?.has(name) ?? false)
|
|
133
192
|
|
|
134
|
-
// Initialize results
|
|
193
|
+
// Initialize results in deterministic topological order
|
|
135
194
|
const results: SeederResult[] = orderedNames.map((name) => ({
|
|
136
195
|
name,
|
|
137
196
|
status: 'pending',
|
|
138
197
|
}))
|
|
198
|
+
const resultIndex = new Map<string, number>()
|
|
199
|
+
for (let i = 0; i < orderedNames.length; i++) {
|
|
200
|
+
resultIndex.set(orderedNames[i]!, i)
|
|
201
|
+
}
|
|
139
202
|
|
|
140
|
-
// Store outputs for dependency injection
|
|
203
|
+
// Store outputs for dependency injection and return value
|
|
141
204
|
const outputs: Record<string, unknown> = {}
|
|
142
205
|
|
|
143
|
-
// Track failed seeders to skip their dependents
|
|
206
|
+
// Track failed (and skipped) seeders to skip their dependents
|
|
144
207
|
const failedSeeders = new Set<string>()
|
|
208
|
+
const concurrent = concurrency > 1
|
|
145
209
|
|
|
146
|
-
|
|
147
|
-
await config.onBeforeAll()
|
|
148
|
-
}
|
|
149
|
-
|
|
210
|
+
enterSeeding()
|
|
150
211
|
try {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
// TypeScript safety - should never happen since we iterate within bounds
|
|
160
|
-
if (!name || !result) continue
|
|
161
|
-
|
|
162
|
-
const seeder = seederMap.get(name)
|
|
163
|
-
if (!seeder) continue
|
|
164
|
-
|
|
165
|
-
// Check if any dependencies have failed (only relevant when continueOnFailure is true)
|
|
166
|
-
if (continueOnFailure) {
|
|
167
|
-
const hasFailedDep = seeder.dependsOn.some((dep) => failedSeeders.has(dep))
|
|
168
|
-
if (hasFailedDep) {
|
|
169
|
-
result.status = 'skipped'
|
|
170
|
-
failedSeeders.add(name) // Treat skipped as failed for dependency chain
|
|
171
|
-
if (verbose) {
|
|
172
|
-
printSeederHeader(name, i, orderedNames.length, seeder.description)
|
|
173
|
-
printSeederSkipped(seeder.dependsOn.filter((d) => failedSeeders.has(d)))
|
|
174
|
-
}
|
|
175
|
-
continue
|
|
176
|
-
}
|
|
177
|
-
}
|
|
212
|
+
if (verbose) {
|
|
213
|
+
printStartBanner({
|
|
214
|
+
name: config.bannerName,
|
|
215
|
+
count: orderedNames.length,
|
|
216
|
+
target,
|
|
217
|
+
concurrency,
|
|
218
|
+
})
|
|
219
|
+
}
|
|
178
220
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
221
|
+
const runStartTime = performance.now()
|
|
222
|
+
|
|
223
|
+
if (config.onBeforeAll) {
|
|
224
|
+
await config.onBeforeAll()
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
let ctx: TContext | undefined
|
|
228
|
+
try {
|
|
229
|
+
if (config.preloadCache) {
|
|
230
|
+
await config.preloadCache()
|
|
182
231
|
}
|
|
183
232
|
|
|
184
|
-
//
|
|
185
|
-
|
|
233
|
+
// Create context once for all seeders
|
|
234
|
+
ctx = await config.context()
|
|
235
|
+
const activeCtx = ctx
|
|
236
|
+
|
|
237
|
+
const executeSeeder = async (name: string): Promise<boolean> => {
|
|
238
|
+
const idx = resultIndex.get(name)
|
|
239
|
+
const seeder = seederMap.get(name)
|
|
240
|
+
if (idx === undefined || !seeder) return true
|
|
241
|
+
|
|
242
|
+
const scopeHandle: SeederScopeHandle = config.createSeederScope
|
|
243
|
+
? config.createSeederScope({ name, fresh: isFresh(name), concurrent })
|
|
244
|
+
: {
|
|
245
|
+
record: config.beforeSeeder?.(name, { fresh: isFresh(name) }),
|
|
246
|
+
run: (fn) => fn(),
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const deps: Record<string, unknown> = {}
|
|
250
|
+
for (const depName of seeder.dependsOn) {
|
|
251
|
+
deps[depName] = outputs[depName]
|
|
252
|
+
}
|
|
186
253
|
|
|
187
|
-
|
|
188
|
-
fresh: fresh === true || (Array.isArray(fresh) && fresh.includes(name)),
|
|
189
|
-
})
|
|
254
|
+
const startTime = performance.now()
|
|
190
255
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
deps[depName] = outputs[depName]
|
|
195
|
-
}
|
|
256
|
+
try {
|
|
257
|
+
const output = await scopeHandle.run(async () => seeder.run(activeCtx, deps))
|
|
258
|
+
const durationMs = Math.round(performance.now() - startTime)
|
|
196
259
|
|
|
197
|
-
|
|
260
|
+
outputs[name] = output
|
|
261
|
+
results[idx] = {
|
|
262
|
+
name,
|
|
263
|
+
status: 'completed',
|
|
264
|
+
durationMs,
|
|
265
|
+
output,
|
|
266
|
+
cachedSteps: scopeHandle.record?.cachedSteps ?? [],
|
|
267
|
+
}
|
|
198
268
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
269
|
+
if (verbose) {
|
|
270
|
+
if (concurrent) {
|
|
271
|
+
printConcurrentComplete(name, durationMs, true)
|
|
272
|
+
} else {
|
|
273
|
+
printSeederComplete(durationMs, true)
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return true
|
|
277
|
+
} catch (error) {
|
|
278
|
+
const durationMs = Math.round(performance.now() - startTime)
|
|
279
|
+
const err = error instanceof Error ? error : new Error(String(error))
|
|
280
|
+
const failedStep =
|
|
281
|
+
scopeHandle.record?.failed && scopeHandle.record.failed.error === error
|
|
282
|
+
? scopeHandle.record.failed.step
|
|
283
|
+
: undefined
|
|
284
|
+
|
|
285
|
+
results[idx] = {
|
|
286
|
+
name,
|
|
287
|
+
status: 'failed',
|
|
288
|
+
durationMs,
|
|
289
|
+
error: err,
|
|
290
|
+
...(failedStep !== undefined ? { failedStep } : {}),
|
|
291
|
+
cachedSteps: scopeHandle.record?.cachedSteps ?? [],
|
|
292
|
+
}
|
|
293
|
+
failedSeeders.add(name)
|
|
202
294
|
|
|
203
|
-
|
|
204
|
-
|
|
295
|
+
if (verbose) {
|
|
296
|
+
if (concurrent) {
|
|
297
|
+
printConcurrentComplete(name, durationMs, false)
|
|
298
|
+
} else {
|
|
299
|
+
printSeederComplete(durationMs, false)
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return false
|
|
303
|
+
}
|
|
304
|
+
}
|
|
205
305
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
306
|
+
if (!concurrent) {
|
|
307
|
+
// Sequential execution in topological order
|
|
308
|
+
for (let i = 0; i < orderedNames.length; i++) {
|
|
309
|
+
const name = orderedNames[i]
|
|
310
|
+
if (!name) continue
|
|
311
|
+
const seeder = seederMap.get(name)
|
|
312
|
+
if (!seeder) continue
|
|
313
|
+
|
|
314
|
+
if (continueOnFailure) {
|
|
315
|
+
const failedDeps = seeder.dependsOn.filter((dep) => failedSeeders.has(dep))
|
|
316
|
+
if (failedDeps.length > 0) {
|
|
317
|
+
results[i] = { name, status: 'skipped', failedDeps }
|
|
318
|
+
failedSeeders.add(name)
|
|
319
|
+
if (verbose) {
|
|
320
|
+
printSeederHeader(name, i, orderedNames.length, seeder.description)
|
|
321
|
+
printSeederSkipped(failedDeps)
|
|
322
|
+
}
|
|
323
|
+
continue
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (verbose) {
|
|
328
|
+
printSeederHeader(name, i, orderedNames.length, seeder.description)
|
|
329
|
+
}
|
|
211
330
|
|
|
212
|
-
|
|
213
|
-
|
|
331
|
+
const ok = await executeSeeder(name)
|
|
332
|
+
if (!ok && !continueOnFailure) {
|
|
333
|
+
break
|
|
334
|
+
}
|
|
214
335
|
}
|
|
215
|
-
}
|
|
216
|
-
//
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
336
|
+
} else if (orderedNames.length > 0) {
|
|
337
|
+
// Parallel DAG worker pool with critical-path (longest-downstream-chain-first) scheduling
|
|
338
|
+
const remainingDeps = new Map<string, number>()
|
|
339
|
+
const dependents = new Map<string, string[]>()
|
|
340
|
+
|
|
341
|
+
for (const name of orderedNames) {
|
|
342
|
+
remainingDeps.set(name, 0)
|
|
343
|
+
dependents.set(name, [])
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
for (const name of orderedNames) {
|
|
347
|
+
const seeder = seederMap.get(name)
|
|
348
|
+
if (!seeder) continue
|
|
349
|
+
const uniqueDeps = new Set(seeder.dependsOn)
|
|
350
|
+
remainingDeps.set(name, uniqueDeps.size)
|
|
351
|
+
for (const dep of uniqueDeps) {
|
|
352
|
+
dependents.get(dep)?.push(name)
|
|
353
|
+
}
|
|
223
354
|
}
|
|
224
|
-
failedSeeders.add(name)
|
|
225
355
|
|
|
226
|
-
|
|
227
|
-
|
|
356
|
+
// Compute critical-path height (longest chain of downstream dependents) in reverse topo order
|
|
357
|
+
const criticalHeight = new Map<string, number>()
|
|
358
|
+
for (let i = orderedNames.length - 1; i >= 0; i--) {
|
|
359
|
+
const name = orderedNames[i]!
|
|
360
|
+
let maxHeight = 0
|
|
361
|
+
for (const dependent of dependents.get(name) ?? []) {
|
|
362
|
+
const h = (criticalHeight.get(dependent) ?? 0) + 1
|
|
363
|
+
if (h > maxHeight) maxHeight = h
|
|
364
|
+
}
|
|
365
|
+
criticalHeight.set(name, maxHeight)
|
|
228
366
|
}
|
|
229
367
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
368
|
+
const ready: string[] = []
|
|
369
|
+
for (const name of orderedNames) {
|
|
370
|
+
if (remainingDeps.get(name) === 0) {
|
|
371
|
+
ready.push(name)
|
|
372
|
+
}
|
|
233
373
|
}
|
|
374
|
+
|
|
375
|
+
const popHighestPriority = (): string => {
|
|
376
|
+
let bestIdx = 0
|
|
377
|
+
let bestHeight = criticalHeight.get(ready[0]!) ?? 0
|
|
378
|
+
let bestOrder = resultIndex.get(ready[0]!) ?? 0
|
|
379
|
+
for (let i = 1; i < ready.length; i++) {
|
|
380
|
+
const candidate = ready[i]!
|
|
381
|
+
const h = criticalHeight.get(candidate) ?? 0
|
|
382
|
+
const order = resultIndex.get(candidate) ?? 0
|
|
383
|
+
if (h > bestHeight || (h === bestHeight && order < bestOrder)) {
|
|
384
|
+
bestIdx = i
|
|
385
|
+
bestHeight = h
|
|
386
|
+
bestOrder = order
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return ready.splice(bestIdx, 1)[0]!
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
await new Promise<void>((resolve) => {
|
|
393
|
+
let inFlight = 0
|
|
394
|
+
let stopped = false
|
|
395
|
+
|
|
396
|
+
const markSettled = (settledName: string) => {
|
|
397
|
+
const deps = dependents.get(settledName)
|
|
398
|
+
if (!deps) return
|
|
399
|
+
for (const dependent of deps) {
|
|
400
|
+
const next = (remainingDeps.get(dependent) ?? 1) - 1
|
|
401
|
+
remainingDeps.set(dependent, next)
|
|
402
|
+
if (next !== 0 || stopped) continue
|
|
403
|
+
|
|
404
|
+
const seeder = seederMap.get(dependent)
|
|
405
|
+
if (!seeder) continue
|
|
406
|
+
|
|
407
|
+
const failedDeps = seeder.dependsOn.filter((d) => failedSeeders.has(d))
|
|
408
|
+
if (failedDeps.length > 0) {
|
|
409
|
+
const idx = resultIndex.get(dependent)
|
|
410
|
+
if (idx !== undefined) {
|
|
411
|
+
results[idx] = { name: dependent, status: 'skipped', failedDeps }
|
|
412
|
+
}
|
|
413
|
+
failedSeeders.add(dependent)
|
|
414
|
+
if (verbose) {
|
|
415
|
+
printConcurrentSkipped(dependent, failedDeps)
|
|
416
|
+
}
|
|
417
|
+
markSettled(dependent)
|
|
418
|
+
} else {
|
|
419
|
+
ready.push(dependent)
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const schedule = () => {
|
|
425
|
+
while (!stopped && inFlight < concurrency && ready.length > 0) {
|
|
426
|
+
const name = popHighestPriority()
|
|
427
|
+
inFlight++
|
|
428
|
+
void executeSeeder(name).then((ok) => {
|
|
429
|
+
inFlight--
|
|
430
|
+
if (!ok && !continueOnFailure) {
|
|
431
|
+
stopped = true
|
|
432
|
+
} else {
|
|
433
|
+
markSettled(name)
|
|
434
|
+
}
|
|
435
|
+
schedule()
|
|
436
|
+
})
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
if (inFlight === 0 && (stopped || ready.length === 0)) {
|
|
440
|
+
resolve()
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
schedule()
|
|
445
|
+
})
|
|
234
446
|
}
|
|
235
|
-
}
|
|
236
447
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
448
|
+
if (printResults) {
|
|
449
|
+
printResultsTable(results)
|
|
450
|
+
printErrors(results)
|
|
451
|
+
printSummary(results, performance.now() - runStartTime, concurrency)
|
|
452
|
+
}
|
|
453
|
+
} finally {
|
|
454
|
+
try {
|
|
455
|
+
if (config.flushCache) {
|
|
456
|
+
await config.flushCache()
|
|
457
|
+
}
|
|
458
|
+
} finally {
|
|
459
|
+
if (config.onAfterAll) {
|
|
460
|
+
await config.onAfterAll(ctx)
|
|
461
|
+
}
|
|
462
|
+
}
|
|
242
463
|
}
|
|
243
464
|
} finally {
|
|
244
|
-
|
|
245
|
-
await config.onAfterAll()
|
|
246
|
-
}
|
|
465
|
+
exitSeeding()
|
|
247
466
|
}
|
|
248
467
|
|
|
249
|
-
const
|
|
250
|
-
|
|
468
|
+
const hasFailure = results.some((r) => r.status === 'failed')
|
|
469
|
+
if (hasFailure) {
|
|
470
|
+
return {
|
|
471
|
+
success: false,
|
|
472
|
+
results: results as SeederResultFor<TSeeders>[],
|
|
473
|
+
outputs: outputs as Partial<Record<string, unknown>>,
|
|
474
|
+
} as RunSeedersResult<TSeeders, TOptions>
|
|
475
|
+
}
|
|
476
|
+
return {
|
|
477
|
+
success: true,
|
|
478
|
+
results: results as SeederResultFor<TSeeders>[],
|
|
479
|
+
outputs,
|
|
480
|
+
} as RunSeedersResult<TSeeders, TOptions>
|
|
251
481
|
}
|
package/src/seeding-state.ts
CHANGED
|
@@ -8,12 +8,13 @@
|
|
|
8
8
|
* - Syncing to external services
|
|
9
9
|
*
|
|
10
10
|
* The flag lives on `globalThis`, so `plantr` and `plantr/state` share it even when
|
|
11
|
-
* they are loaded as separate bundles.
|
|
12
|
-
*
|
|
11
|
+
* they are loaded as separate bundles. Overlapping runs are reference-counted so
|
|
12
|
+
* one finishing run does not clear the flag while another is still active.
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
const key = Symbol.for('plantr.seedingActive')
|
|
16
|
-
const
|
|
16
|
+
const countKey = Symbol.for('plantr.seedingCount')
|
|
17
|
+
const state = globalThis as { [key]?: boolean; [countKey]?: number }
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
20
|
* Check if seeding is currently active
|
|
@@ -23,9 +24,30 @@ export function isSeedingActive(): boolean {
|
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
|
-
* Set the seeding state.
|
|
27
|
-
*
|
|
27
|
+
* Set the seeding state directly.
|
|
28
|
+
* Resets the active run count to 1 (true) or 0 (false).
|
|
28
29
|
*/
|
|
29
30
|
export function setSeedingActive(active: boolean): void {
|
|
31
|
+
state[countKey] = active ? 1 : 0
|
|
30
32
|
state[key] = active
|
|
31
33
|
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Increment the active seeding run counter and mark seeding active.
|
|
37
|
+
* Called automatically by `runSeeders` when execution begins.
|
|
38
|
+
*/
|
|
39
|
+
export function enterSeeding(): void {
|
|
40
|
+
const next = (state[countKey] ?? 0) + 1
|
|
41
|
+
state[countKey] = next
|
|
42
|
+
state[key] = true
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Decrement the active seeding run counter and clear the flag when no runs remain.
|
|
47
|
+
* Called automatically by `runSeeders` when execution finishes.
|
|
48
|
+
*/
|
|
49
|
+
export function exitSeeding(): void {
|
|
50
|
+
const next = Math.max(0, (state[countKey] ?? 0) - 1)
|
|
51
|
+
state[countKey] = next
|
|
52
|
+
state[key] = next > 0
|
|
53
|
+
}
|