@defrex/autobuild-hosted-dispatcher 1.3.0 → 1.4.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/README.md +5 -0
- package/package.json +1 -1
- package/src/dispatcher.ts +50 -56
package/README.md
CHANGED
|
@@ -46,6 +46,11 @@ Dispatcher variables (server-only, like every secret; none reach the browser):
|
|
|
46
46
|
a project stops subsequent ticks, without cancelling already-started work.
|
|
47
47
|
Registry failures fail closed. Configure `AB_POSTGRES_URL` or `DATABASE_URL`;
|
|
48
48
|
the registry pool is reused while membership is queried each invocation.
|
|
49
|
+
Each invocation synchronizes durable placement for included and excluded
|
|
50
|
+
registered projects before dispatch; a failed synchronization prevents that
|
|
51
|
+
project's dispatch. Project reads publish the same selection before first cron.
|
|
52
|
+
Unset or blank `CRON_SECRET` disables hosted responsibility; reads publish
|
|
53
|
+
`hosted: false` without requiring an origin.
|
|
49
54
|
- `AB_DISPATCHER_BUDGET_SECONDS`: per-invocation work budget (default 240,
|
|
50
55
|
clamped 10–780); pair it with the route's `maxDuration` as described in the
|
|
51
56
|
operator procedure.
|
package/package.json
CHANGED
package/src/dispatcher.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { openPostgresBuildStoreFromEnv } from '@defrex/autobuild-postgres-store'
|
|
2
|
+
import type { DispatchPlacementFact } from '@defrex/autobuild/remote-store'
|
|
1
3
|
import { ProjectSecretError } from '@defrex/autobuild/project-secrets'
|
|
2
4
|
import {
|
|
3
5
|
openPostgresProjectSecretsFromEnv,
|
|
@@ -43,7 +45,14 @@ import { createHash, randomUUID, timingSafeEqual } from 'node:crypto'
|
|
|
43
45
|
import type { Clock } from '@defrex/autobuild/plugin-sdk'
|
|
44
46
|
import { spawnExec } from '@defrex/autobuild/git-worktree'
|
|
45
47
|
import { abDispatch, type DispatchOpts } from '@defrex/autobuild/cli'
|
|
46
|
-
import {
|
|
48
|
+
import {
|
|
49
|
+
parseHostedStoreEnv,
|
|
50
|
+
parseDispatcherRepositories,
|
|
51
|
+
dispatcherSelection,
|
|
52
|
+
dispatcherEnabled,
|
|
53
|
+
dispatcherOrigin,
|
|
54
|
+
type HostedStoreEnv,
|
|
55
|
+
} from '@defrex/autobuild-hosted-store-service'
|
|
47
56
|
|
|
48
57
|
/** The service environment the dispatcher reads. Secrets are only ever taken
|
|
49
58
|
* through `parseHostedStoreEnv`'s already-validated fields. */
|
|
@@ -81,12 +90,6 @@ const MAX_BUDGET_SECONDS = 780
|
|
|
81
90
|
const DEFAULT_TOKEN_TTL_SECONDS = 604800
|
|
82
91
|
const MIN_TOKEN_TTL_SECONDS = 3600
|
|
83
92
|
|
|
84
|
-
function required(env: HostedDispatcherEnv, name: string): string {
|
|
85
|
-
const value = env[name]?.trim()
|
|
86
|
-
if (!value) throw new Error(`${name} is required and must be nonblank`)
|
|
87
|
-
return value
|
|
88
|
-
}
|
|
89
|
-
|
|
90
93
|
function integerSetting(env: HostedDispatcherEnv, name: string, fallback: number): number {
|
|
91
94
|
const raw = env[name]?.trim()
|
|
92
95
|
if (raw === undefined || raw === '') return fallback
|
|
@@ -95,26 +98,6 @@ function integerSetting(env: HostedDispatcherEnv, name: string, fallback: number
|
|
|
95
98
|
return value
|
|
96
99
|
}
|
|
97
100
|
|
|
98
|
-
/** Parse one repository-CSV variable into normalized https identities. */
|
|
99
|
-
function repositoryList(raw: string, name: string): string[] {
|
|
100
|
-
const values = raw.split(',').map((value) => value.trim())
|
|
101
|
-
|
|
102
|
-
const repositories: string[] = []
|
|
103
|
-
for (const value of values) {
|
|
104
|
-
// Entries are repository identities — normalized https origins — so an
|
|
105
|
-
// operator-spelled `git@github.com:acme/app.git` still matches the Store's
|
|
106
|
-
// identity for this repository (same rule as AB_WEB_REPOSITORIES).
|
|
107
|
-
let normalized: string
|
|
108
|
-
try {
|
|
109
|
-
normalized = normalizeProjectRepository(value)
|
|
110
|
-
} catch {
|
|
111
|
-
throw new Error(`${name} contains an unsafe repository name`)
|
|
112
|
-
}
|
|
113
|
-
if (!repositories.includes(normalized)) repositories.push(normalized)
|
|
114
|
-
}
|
|
115
|
-
return repositories
|
|
116
|
-
}
|
|
117
|
-
|
|
118
101
|
/** Parse the optional per-repository forge-credential override map
|
|
119
102
|
* (`AB_DISPATCHER_GITHUB_TOKENS`): a JSON object mapping repository identities
|
|
120
103
|
* (same normalization as the repository set, so an operator may spell either
|
|
@@ -185,33 +168,10 @@ export function parseHostedDispatcherEnv(env: HostedDispatcherEnv): HostedDispat
|
|
|
185
168
|
// AB_STORE_SECRET, validated once here together with the rest of the store
|
|
186
169
|
// configuration.
|
|
187
170
|
const secret = parseHostedStoreEnv(env).secret
|
|
188
|
-
const
|
|
189
|
-
let origin: URL
|
|
190
|
-
try {
|
|
191
|
-
origin = new URL(rawOrigin)
|
|
192
|
-
} catch {
|
|
193
|
-
throw new Error('AB_DISPATCHER_ORIGIN must be an absolute http(s) origin')
|
|
194
|
-
}
|
|
195
|
-
if (
|
|
196
|
-
!['http:', 'https:'].includes(origin.protocol) ||
|
|
197
|
-
origin.username ||
|
|
198
|
-
origin.password ||
|
|
199
|
-
origin.pathname !== '/' ||
|
|
200
|
-
origin.search ||
|
|
201
|
-
origin.hash
|
|
202
|
-
) {
|
|
203
|
-
throw new Error(
|
|
204
|
-
'AB_DISPATCHER_ORIGIN must be an absolute http(s) origin without credentials or a path',
|
|
205
|
-
)
|
|
206
|
-
}
|
|
207
|
-
if (env.NODE_ENV === 'production' && origin.protocol !== 'https:') {
|
|
208
|
-
throw new Error('AB_DISPATCHER_ORIGIN must use https in production')
|
|
209
|
-
}
|
|
171
|
+
const origin = dispatcherOrigin(env)
|
|
210
172
|
const rawRepositories = env.AB_DISPATCHER_REPOSITORIES
|
|
211
173
|
const repositories =
|
|
212
|
-
rawRepositories === undefined
|
|
213
|
-
? undefined
|
|
214
|
-
: repositoryList(rawRepositories, 'AB_DISPATCHER_REPOSITORIES')
|
|
174
|
+
rawRepositories === undefined ? undefined : parseDispatcherRepositories(rawRepositories)
|
|
215
175
|
const rawBudget = integerSetting(env, 'AB_DISPATCHER_BUDGET_SECONDS', DEFAULT_BUDGET_SECONDS)
|
|
216
176
|
const budgetSeconds = Math.min(MAX_BUDGET_SECONDS, Math.max(MIN_BUDGET_SECONDS, rawBudget))
|
|
217
177
|
const tokenTtlSeconds = integerSetting(
|
|
@@ -229,7 +189,7 @@ export function parseHostedDispatcherEnv(env: HostedDispatcherEnv): HostedDispat
|
|
|
229
189
|
const cronSecret = env.CRON_SECRET?.trim()
|
|
230
190
|
return {
|
|
231
191
|
secret,
|
|
232
|
-
origin
|
|
192
|
+
origin,
|
|
233
193
|
...(repositories !== undefined ? { repositories } : {}),
|
|
234
194
|
githubTokenOverrides: githubTokenOverridesMap,
|
|
235
195
|
budgetSeconds,
|
|
@@ -258,6 +218,7 @@ export interface HostedDispatcherTickSummary {
|
|
|
258
218
|
}
|
|
259
219
|
|
|
260
220
|
export interface HostedDispatcherOptions {
|
|
221
|
+
syncPlacement?: (repo: string, desired: DispatchPlacementFact) => Promise<unknown>
|
|
261
222
|
openSecrets?: (env: HostedDispatcherEnv) => Promise<ProjectSecretsResolver>
|
|
262
223
|
/** Per-repository credential lifecycle; child grants are independent of these rows. */
|
|
263
224
|
openDispatcherCredentials?: (
|
|
@@ -359,6 +320,16 @@ export function createHostedDispatcher(options: HostedDispatcherOptions = {}): {
|
|
|
359
320
|
return projectPromise
|
|
360
321
|
}
|
|
361
322
|
|
|
323
|
+
let placementStore: ReturnType<typeof openPostgresBuildStoreFromEnv> | undefined
|
|
324
|
+
const syncPlacement =
|
|
325
|
+
options.syncPlacement ??
|
|
326
|
+
(async (repo: string, desired: DispatchPlacementFact) => {
|
|
327
|
+
placementStore ??= openPostgresBuildStoreFromEnv(env).catch((error) => {
|
|
328
|
+
placementStore = undefined
|
|
329
|
+
throw error
|
|
330
|
+
})
|
|
331
|
+
return (await placementStore).syncDispatchPlacement(repo, desired)
|
|
332
|
+
})
|
|
362
333
|
let secrets: Promise<ProjectSecretsResolver> | undefined
|
|
363
334
|
const openSecrets = () =>
|
|
364
335
|
(secrets ??= (options.openSecrets ?? openPostgresProjectSecretsFromEnv)(env).catch((error) => {
|
|
@@ -376,8 +347,7 @@ export function createHostedDispatcher(options: HostedDispatcherOptions = {}): {
|
|
|
376
347
|
): Promise<HostedDispatcherTickSummary> {
|
|
377
348
|
const config = parseHostedDispatcherEnv(env)
|
|
378
349
|
const registered = (await (await openProjects()).list()).map((entry) => entry.repo)
|
|
379
|
-
const selected =
|
|
380
|
-
config.repositories?.filter((repo) => registered.includes(repo)) ?? registered
|
|
350
|
+
const selected = dispatcherSelection(config.repositories).filter(registered)
|
|
381
351
|
const now = clock().getTime()
|
|
382
352
|
const deadlineAt = now + config.budgetSeconds * 1000
|
|
383
353
|
const sandboxAuth = sandboxAuthSource(env, credentials)
|
|
@@ -385,7 +355,30 @@ export function createHostedDispatcher(options: HostedDispatcherOptions = {}): {
|
|
|
385
355
|
`${LOG_PREFIX} ${invocation}: tick start repositories=${selected.length} budgetSeconds=${config.budgetSeconds} origin=${config.origin} sandboxAuth=${sandboxAuth}`,
|
|
386
356
|
)
|
|
387
357
|
const repositories: HostedDispatcherRepositoryOutcome[] = []
|
|
358
|
+
const placementFailures = new Set<string>()
|
|
359
|
+
const selection = dispatcherSelection(config.repositories)
|
|
360
|
+
for (const repository of registered) {
|
|
361
|
+
try {
|
|
362
|
+
await syncPlacement(
|
|
363
|
+
repository,
|
|
364
|
+
selection.includes(repository)
|
|
365
|
+
? { hosted: true, origin: config.origin }
|
|
366
|
+
: { hosted: false },
|
|
367
|
+
)
|
|
368
|
+
} catch (error) {
|
|
369
|
+
placementFailures.add(repository)
|
|
370
|
+
repositories.push({
|
|
371
|
+
repository,
|
|
372
|
+
outcome: 'failed',
|
|
373
|
+
error: 'placement synchronization failed',
|
|
374
|
+
})
|
|
375
|
+
logError(
|
|
376
|
+
`${LOG_PREFIX} ${invocation}: ${repository} placement synchronization failed: ${errorDetail(error)}`,
|
|
377
|
+
)
|
|
378
|
+
}
|
|
379
|
+
}
|
|
388
380
|
for (const repository of selected) {
|
|
381
|
+
if (placementFailures.has(repository)) continue
|
|
389
382
|
if (clock().getTime() + MIN_REMAINING_MS > deadlineAt) {
|
|
390
383
|
repositories.push({ repository, outcome: 'skipped' })
|
|
391
384
|
log(`${LOG_PREFIX} ${invocation}: ${repository} skipped (invocation budget exhausted)`)
|
|
@@ -454,6 +447,7 @@ export function createHostedDispatcher(options: HostedDispatcherOptions = {}): {
|
|
|
454
447
|
const childEnv: Record<string, string | undefined> = {
|
|
455
448
|
...env,
|
|
456
449
|
...snapshot.env,
|
|
450
|
+
AB_PROJECT_ACCOUNT_ENV: JSON.stringify(snapshot.env),
|
|
457
451
|
AB_STORE: config.origin,
|
|
458
452
|
AB_TOKEN: token,
|
|
459
453
|
...(resolutionFailed
|
|
@@ -590,7 +584,7 @@ export function createDispatcherEndpoint(options: HostedDispatcherOptions = {}):
|
|
|
590
584
|
}
|
|
591
585
|
// Endpoint disabled: never run work without authorization configured.
|
|
592
586
|
const cronSecret = env.CRON_SECRET?.trim()
|
|
593
|
-
if (
|
|
587
|
+
if (!dispatcherEnabled(env)) {
|
|
594
588
|
return reject(
|
|
595
589
|
403,
|
|
596
590
|
'disabled',
|