@ultimat3/cli 31.48.0 → 31.49.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/CLAUDE.md +30 -23
- package/README.md +96 -4
- package/package.json +29 -29
- package/src/api-registration.ts +28 -14
- package/src/app-load.ts +6 -1
- package/src/app-root.ts +7 -1
- package/src/clean-services.ts +142 -0
- package/src/cmd-auth.ts +3 -0
- package/src/cmd-clean-spec.ts +10 -3
- package/src/cmd-clean.ts +23 -9
- package/src/cmd-db-backfill.ts +6 -0
- package/src/cmd-db-branch.ts +10 -4
- package/src/cmd-db-recount.ts +5 -12
- package/src/cmd-db-rollback.ts +77 -0
- package/src/cmd-db-spec.ts +18 -2
- package/src/cmd-db-status.ts +5 -10
- package/src/cmd-db.ts +105 -33
- package/src/cmd-deploy-verify.ts +14 -6
- package/src/cmd-docs-topic.ts +94 -0
- package/src/cmd-docs.ts +15 -1
- package/src/cmd-doctor.ts +109 -41
- package/src/cmd-drill.ts +80 -18
- package/src/cmd-env.ts +30 -8
- package/src/cmd-errors-app.ts +63 -0
- package/src/cmd-errors.ts +50 -14
- package/src/cmd-generate.ts +17 -4
- package/src/cmd-heavy-spec.ts +9 -1
- package/src/cmd-heavy.ts +111 -27
- package/src/cmd-jobs.ts +27 -1
- package/src/cmd-merge-driver-spec.ts +12 -0
- package/src/cmd-merge-driver.ts +95 -0
- package/src/cmd-new.ts +23 -3
- package/src/cmd-policy-spec.ts +9 -1
- package/src/cmd-policy.ts +72 -17
- package/src/cmd-stop.ts +18 -4
- package/src/cmd-tasks-run.ts +87 -0
- package/src/cmd-tasks-spec.ts +10 -3
- package/src/cmd-tasks.ts +8 -5
- package/src/cmd-test.ts +2 -1
- package/src/cmd-upgrade-spec.ts +8 -1
- package/src/cmd-upgrade.ts +15 -1
- package/src/cmd-verify.ts +9 -41
- package/src/codemods/agents-doc-row.ts +73 -0
- package/src/codemods/define-error-leaf.ts +37 -12
- package/src/codemods/docs-many-agents.ts +11 -15
- package/src/codemods/docs-worker-commands.ts +26 -0
- package/src/codemods/index.ts +4 -0
- package/src/codemods/merge-drivers.ts +66 -0
- package/src/command.ts +3 -0
- package/src/db-applied-edits.ts +117 -0
- package/src/db-dev-wire.ts +18 -0
- package/src/db-diagnostics.ts +5 -10
- package/src/db-generate.ts +82 -7
- package/src/db-migrate-pass.ts +51 -0
- package/src/db-migrate-through-dev.ts +209 -0
- package/src/db-rollback-consent.ts +128 -0
- package/src/db-rollback-pass.ts +76 -0
- package/src/db-seed-generate.ts +5 -1
- package/src/db-seed.ts +7 -1
- package/src/deploy-verify-findings.ts +3 -0
- package/src/deploy-verify-grade.ts +3 -0
- package/src/deploy-verify-processes.ts +32 -0
- package/src/dev-boot.ts +3 -0
- package/src/dev-db-migrate.ts +155 -0
- package/src/dev-db-rollback.ts +33 -0
- package/src/dev-db-settled.ts +22 -0
- package/src/dev-lock-errors.ts +78 -0
- package/src/dev-lock-holder-dir.ts +64 -0
- package/src/dev-lock-live.ts +44 -0
- package/src/dev-lock.ts +59 -84
- package/src/dev-policy.ts +11 -8
- package/src/dev-rebuild-schema.ts +116 -13
- package/src/dev-rebuild.ts +13 -4
- package/src/dev-route-table.ts +7 -0
- package/src/dispatch.ts +21 -1
- package/src/doctor-api-listing.ts +114 -0
- package/src/doctor-guarded.ts +41 -0
- package/src/doctor-port.ts +90 -17
- package/src/doctor-probe.ts +36 -5
- package/src/doctor-processes-probe.ts +127 -0
- package/src/doctor-processes.ts +136 -0
- package/src/doctor-warnings.ts +17 -0
- package/src/error-codes-agents.ts +35 -0
- package/src/error-codes-processes.ts +30 -0
- package/src/error-codes-tooling.ts +2 -0
- package/src/error-codes.ts +12 -13
- package/src/errors-suite.ts +27 -0
- package/src/errors.ts +18 -22
- package/src/exercise-registration.ts +4 -3
- package/src/framework-env.ts +7 -3
- package/src/generate-files.ts +1 -0
- package/src/generate-grant-findings.ts +7 -5
- package/src/generate-grants.ts +9 -8
- package/src/generate-kinds.ts +30 -6
- package/src/generate-roles-file.ts +30 -0
- package/src/generate-slice-policy.ts +3 -3
- package/src/guards.ts +2 -2
- package/src/heavy-env.ts +2 -0
- package/src/heavy-errors.ts +18 -0
- package/src/heavy-holders.ts +78 -0
- package/src/heavy-ledger.ts +56 -14
- package/src/heavy-pressure.ts +104 -0
- package/src/heavy.ts +31 -13
- package/src/i18n-registration.ts +22 -2
- package/src/island-harness.ts +2 -2
- package/src/job-budget.ts +4 -2
- package/src/job-registration.ts +64 -8
- package/src/jobs-queue-known.ts +43 -0
- package/src/mcp-errors-agents.ts +20 -0
- package/src/mcp-errors-doctor.ts +27 -0
- package/src/mcp-errors-processes.ts +23 -0
- package/src/mcp-errors.ts +15 -29
- package/src/mcp-host.ts +2 -2
- package/src/merge-driver-catalog.ts +58 -0
- package/src/merge-driver-config.ts +43 -0
- package/src/merge-driver-lines.ts +140 -0
- package/src/merge-state.ts +105 -0
- package/src/messages-db.ts +22 -1
- package/src/messages-processes.ts +13 -0
- package/src/messages-upgrade.ts +6 -0
- package/src/messages.ts +54 -4
- package/src/output-findings.ts +48 -21
- package/src/parse.ts +37 -6
- package/src/policy-facts.ts +38 -21
- package/src/policy-row.ts +99 -0
- package/src/port-pair-free.ts +28 -0
- package/src/registry-lines.ts +110 -0
- package/src/registry.ts +2 -0
- package/src/retain-sweep.ts +1 -1
- package/src/role-replicator.ts +4 -1
- package/src/role-select.ts +86 -0
- package/src/role-start.ts +13 -55
- package/src/role-sync.ts +10 -5
- package/src/runtime-cache-frame.ts +46 -0
- package/src/runtime-cache-ttl.ts +23 -0
- package/src/runtime-cache.ts +33 -16
- package/src/runtime-database-ssl.ts +84 -0
- package/src/runtime-idempotency-scope.ts +16 -2
- package/src/runtime-jobs-share.ts +52 -0
- package/src/runtime-jobs.ts +43 -12
- package/src/runtime-queue.ts +18 -3
- package/src/runtime-services.ts +8 -1
- package/src/schema-drift.ts +19 -2
- package/src/schema-dump-drift.ts +4 -1
- package/src/serve-boot-probe.ts +30 -2
- package/src/serve-boot.ts +11 -1
- package/src/serve-env.ts +39 -3
- package/src/serve.ts +61 -10
- package/src/stop-run-start.ts +12 -0
- package/src/supervise-boot.ts +61 -0
- package/src/supervise-cgroup.ts +84 -0
- package/src/supervise-child.ts +206 -0
- package/src/supervise-count.ts +244 -0
- package/src/supervise-errors.ts +83 -0
- package/src/supervise-facts.ts +40 -0
- package/src/supervise-messages.ts +84 -0
- package/src/supervise-policy.ts +46 -0
- package/src/supervise-pool.ts +10 -0
- package/src/supervise-probe.ts +158 -0
- package/src/supervise-run.ts +54 -0
- package/src/supervise.ts +383 -0
- package/src/templates/generate-purge.ts +6 -6
- package/src/templates/guard-animated-layout-property.ts +2 -2
- package/src/templates/guard-bare-error.ts +1 -1
- package/src/templates/guard-error-declared.ts +2 -2
- package/src/templates/guard-focus-visible.ts +1 -1
- package/src/templates/guard-image-dimensions.ts +2 -2
- package/src/templates/guard-island-mount-clears.ts +1 -1
- package/src/templates/guard-native-confirm.ts +1 -1
- package/src/templates/guard-raw-breakpoint.ts +2 -2
- package/src/templates/guard-raw-colour.ts +2 -2
- package/src/templates/guard-raw-fetch.ts +1 -1
- package/src/templates/guard-raw-interval.ts +1 -1
- package/src/templates/guard-raw-length.ts +3 -3
- package/src/templates/guard-raw-motion.ts +1 -1
- package/src/templates/guard-raw-shadow.ts +1 -1
- package/src/templates/guard-raw-z-index.ts +1 -1
- package/src/templates/guard-released-forms.ts +54 -8
- package/src/templates/guard-repo-await-in-loop.ts +1 -1
- package/src/templates/guard-repo-raw-sql.ts +4 -4
- package/src/templates/guard-repo-unbounded.ts +1 -1
- package/src/templates/guard-runtime-pin.ts +1 -1
- package/src/templates/guard-semantic-interactive.ts +2 -2
- package/src/templates/guard-unbounded-fanout.ts +1 -1
- package/src/templates/guard-undeclared-custom-property.ts +3 -3
- package/src/templates/guard-undefined-style-class.ts +3 -3
- package/src/templates/guard-unzoned-date.ts +3 -3
- package/src/templates/query-slice.ts +82 -0
- package/src/templates/query.ts +76 -30
- package/src/templates/resource-page.ts +3 -2
- package/src/templates/scaffold-bin.ts +2 -0
- package/src/templates/scaffold-claude-commands.ts +6 -0
- package/src/templates/scaffold-claude.ts +4 -4
- package/src/templates/scaffold-container-compose.ts +47 -7
- package/src/templates/scaffold-docs.ts +7 -3
- package/src/templates/scaffold-gitattributes.ts +18 -1
- package/src/templates/scaffold-helm-templates.ts +9 -0
- package/src/templates/scaffold-repo.ts +4 -1
- package/src/templates/scaffold-test-services.ts +33 -11
- package/src/templates/shapes/offline.ts +0 -7
- package/src/templates/shapes/realtime.ts +2 -6
- package/src/templates/sign-in-action-tests.ts +6 -4
- package/src/templates/sign-in-challenge.ts +62 -0
- package/src/templates/sign-in-oidc-mfa.ts +31 -0
- package/src/templates/sign-in-oidc.ts +11 -3
- package/src/templates/sign-in-password.ts +18 -56
- package/src/test-shards.ts +114 -34
- package/src/test-verdict.ts +79 -0
- package/src/test-workers.ts +15 -0
- package/src/upgrade-gate-codemods.ts +5 -3
- package/src/upgrade-gate.ts +19 -1
- package/src/upgrade-merge-drivers.ts +25 -0
- package/src/upgrade-report.ts +18 -4
- package/src/upgrade-scope.ts +62 -0
- package/src/verify-checks.ts +2 -8
- package/src/verify-coverage-run.ts +4 -1
- package/src/verify-live.ts +38 -14
- package/src/verify-only.ts +67 -0
- package/src/verify-progress.ts +7 -3
- package/src/verify-run.ts +20 -2
- package/src/verify-services-stack.ts +165 -0
- package/src/verify-services.ts +84 -2
- package/src/verify-test-run.ts +6 -7
- package/src/verify-tests.ts +8 -2
- package/src/verify-typecheck.ts +25 -2
- package/wiki/Actions.md +2 -1
- package/wiki/Admin-Dashboard.md +2 -2
- package/wiki/Agents.md +5 -5
- package/wiki/Auth.md +15 -5
- package/wiki/Bare-VM.md +1 -1
- package/wiki/CI-Parallel-Gate.md +2 -1
- package/wiki/CLI-Reference.md +107 -40
- package/wiki/Caching-And-Invalidation.md +1 -1
- package/wiki/Client-Navigation.md +1 -1
- package/wiki/Configuration.md +10 -9
- package/wiki/Deployment.md +44 -4
- package/wiki/Entities-And-Migrations.md +5 -1
- package/wiki/FAQ.md +1 -1
- package/wiki/Getting-Started.md +2 -2
- package/wiki/Home.md +1 -1
- package/wiki/I18n.md +5 -3
- package/wiki/Installation.md +12 -11
- package/wiki/Jobs-And-Workflows.md +24 -4
- package/wiki/Known-Gaps.md +1 -1
- package/wiki/MCP-And-AI.md +8 -5
- package/wiki/Many-Agents.md +69 -2
- package/wiki/Migrations-And-Backfills.md +18 -0
- package/wiki/Notify.md +1 -1
- package/wiki/Observability.md +13 -0
- package/wiki/Queries-And-Live-Queries.md +2 -2
- package/wiki/Realtime.md +10 -1
- package/wiki/Routes-And-Render-Modes.md +1 -1
- package/wiki/Scheduled-Tasks.md +4 -1
- package/wiki/Testing.md +7 -4
- package/wiki/Timezones-And-Dates.md +5 -1
- package/wiki/Troubleshooting.md +1 -0
- package/wiki/Tutorial-01-First-App.md +3 -3
- package/wiki/Tutorial-02-First-Feature.md +1 -1
- package/wiki/Tutorial-03-Auth-And-Admin.md +7 -6
- package/wiki/Upgrading.md +37 -0
package/src/role-start.ts
CHANGED
|
@@ -6,8 +6,7 @@
|
|
|
6
6
|
// selectable but not default — it takes a replication slot on a shared database, which is not
|
|
7
7
|
// something every `x dev` in a team should do to the same server by simply starting.
|
|
8
8
|
|
|
9
|
-
import
|
|
10
|
-
import { ctxOf, isRole, logger, markReady, ROLES } from '@ultimat3/core';
|
|
9
|
+
import { ctxOf, logger, markReady } from '@ultimat3/core';
|
|
11
10
|
import { dbExecutor } from '@ultimat3/db';
|
|
12
11
|
import type { RateLimitStore, Route, ServerHandle, WebSocketMount } from '@ultimat3/http';
|
|
13
12
|
import {
|
|
@@ -31,7 +30,7 @@ import {
|
|
|
31
30
|
import type { SyncWs } from '@ultimat3/realtime/server';
|
|
32
31
|
import { loadAppConfig } from './app-config-load';
|
|
33
32
|
import { errorPageHook } from './error-pages';
|
|
34
|
-
import {
|
|
33
|
+
import { RuntimeDriverSplitError } from './errors';
|
|
35
34
|
import {
|
|
36
35
|
DEFAULT_METRICS_PORT,
|
|
37
36
|
rolePorts,
|
|
@@ -41,9 +40,9 @@ import {
|
|
|
41
40
|
import { startRolePgStats } from './role-pg-stats';
|
|
42
41
|
import { rolesUnderRealtime } from './role-realtime';
|
|
43
42
|
import { startReplicator } from './role-replicator';
|
|
43
|
+
import { rolesDeclaredForDev } from './role-select';
|
|
44
44
|
import { type DeviceMount, hostDeviceSockets } from './role-start-devices';
|
|
45
45
|
import type { RunningRoles, StartRolesOptions } from './role-start-types';
|
|
46
|
-
import { DEV_ROLES } from './role-start-types';
|
|
47
46
|
import { prepareSync, type RunningSync, syncReadsChanges } from './role-sync';
|
|
48
47
|
import { startWorkerWake } from './role-wake';
|
|
49
48
|
import { installRoleBootWarnings } from './runtime-boot-warnings';
|
|
@@ -54,62 +53,22 @@ import { startLiveFeed } from './runtime-live-feed';
|
|
|
54
53
|
import { type RunningServices, releaseOrThrow } from './runtime-services';
|
|
55
54
|
import { inlineScriptSources } from './script-csp';
|
|
56
55
|
import { bootWarningConcernsAny } from './serve-boot-warnings';
|
|
57
|
-
import { quoteArg } from './shell-quote';
|
|
58
56
|
import { inlineStyleSources } from './style-csp';
|
|
59
57
|
import { syncConnectSources } from './sync-url';
|
|
60
58
|
import { trustedHopsFromEnv } from './trusted-hops';
|
|
61
59
|
import { DEV_BINDING } from './web-binding';
|
|
62
60
|
|
|
63
|
-
//
|
|
64
|
-
export {
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* What `--role` accepts. The replicator is here but not in `DEV_ROLES`: opt-in, because it takes
|
|
68
|
-
* the one replication slot a database has, and a default that did that would mean two developers
|
|
69
|
-
* pointed at one staging database silently fighting over it.
|
|
70
|
-
*/
|
|
71
|
-
export const SELECTABLE_ROLES: readonly Role[] = [...DEV_ROLES, 'replicator'];
|
|
72
|
-
|
|
61
|
+
// `--role`'s reading, in a file of its own at the line ceiling.
|
|
62
|
+
export { rolesDeclaredForDev, SELECTABLE_ROLES, selectRoles } from './role-select';
|
|
73
63
|
// The two shapes this module takes and answers, in a file of their own at the line ceiling.
|
|
74
64
|
export type { RunningRoles, StartRolesOptions } from './role-start-types';
|
|
65
|
+
// Declared beside the option types so `x dev`'s spec reads it without loading this module.
|
|
66
|
+
export { DEV_ROLES } from './role-start-types';
|
|
75
67
|
|
|
76
68
|
// Re-exported, not re-declared: `web-binding.ts` is a leaf so `role-sync` can read the default
|
|
77
69
|
// without importing this module, which imports it.
|
|
78
70
|
export { DEV_BINDING, type WebBinding } from './web-binding';
|
|
79
71
|
|
|
80
|
-
/**
|
|
81
|
-
* `--role web,worker` picks a subset. An unknown or out-of-scope role is a flag error with the
|
|
82
|
-
* working invocation in the fix line, never a silently ignored value — which is what it was.
|
|
83
|
-
*/
|
|
84
|
-
export function selectRoles(flag: string | undefined): readonly Role[] {
|
|
85
|
-
if (flag === undefined || flag.trim().length === 0) return DEV_ROLES;
|
|
86
|
-
const wanted = flag
|
|
87
|
-
.split(',')
|
|
88
|
-
.map((part) => part.trim())
|
|
89
|
-
.filter((part) => part.length > 0);
|
|
90
|
-
const selected: Role[] = [];
|
|
91
|
-
for (const name of wanted) {
|
|
92
|
-
if (!isRole(name)) {
|
|
93
|
-
throw new BadFlagError({
|
|
94
|
-
flag: 'role',
|
|
95
|
-
command: 'dev',
|
|
96
|
-
reason: `"${name}" is not a role (known: ${ROLES.join(', ')})`,
|
|
97
|
-
fix: `x dev --role ${quoteArg(DEV_ROLES.join(','))}`,
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
if (!SELECTABLE_ROLES.includes(name)) {
|
|
101
|
-
throw new BadFlagError({
|
|
102
|
-
flag: 'role',
|
|
103
|
-
command: 'dev',
|
|
104
|
-
reason: `"${name}" does not run under x dev (it runs once, as \`x db migrate\`)`,
|
|
105
|
-
fix: `x dev --role ${quoteArg(DEV_ROLES.join(','))}`,
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
if (!selected.includes(name)) selected.push(name);
|
|
109
|
-
}
|
|
110
|
-
return SELECTABLE_ROLES.filter((role) => selected.includes(role));
|
|
111
|
-
}
|
|
112
|
-
|
|
113
72
|
/**
|
|
114
73
|
* A server whose route table demands an identity it has no way to resolve.
|
|
115
74
|
*
|
|
@@ -268,12 +227,12 @@ function assertOneJobDriver(runtime: RunningServices): void {
|
|
|
268
227
|
}
|
|
269
228
|
|
|
270
229
|
export async function startRoles(options: StartRolesOptions): Promise<RunningRoles> {
|
|
271
|
-
|
|
272
|
-
const
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
);
|
|
230
|
+
const dev = (options.http ?? DEV_BINDING).dev;
|
|
231
|
+
const config = options.root === undefined ? undefined : await loadAppConfig(options.root);
|
|
232
|
+
// `realtime.enabled` and `roles` read here, before anything binds: a refusal leaves nothing to
|
|
233
|
+
// unwind. `roles` for `x dev` only — a container's `ROLE` was refused against it at boot.
|
|
234
|
+
const realtime = rolesUnderRealtime(options.roles, options.runtime.realtime, dev);
|
|
235
|
+
const selected = dev ? rolesDeclaredForDev(realtime, config) : realtime;
|
|
277
236
|
assertOneJobDriver(options.runtime);
|
|
278
237
|
// Once per process, and only where a role in it can act on the line (`serve-boot-warnings.ts`):
|
|
279
238
|
// `rateLimitStoreFor` is asked twice in a web boot, and a scheduler spends no limit at all.
|
|
@@ -297,7 +256,6 @@ export async function startRoles(options: StartRolesOptions): Promise<RunningRol
|
|
|
297
256
|
// these roles takes back exactly this install, whatever else stacked over or under it.
|
|
298
257
|
// Once, before any role serves: which roles core's `bootWarning` concerns, and its per-build
|
|
299
258
|
// marker store (`runtime-boot-warnings.ts`). Released by the stop, like every frame here.
|
|
300
|
-
const config = options.root === undefined ? undefined : await loadAppConfig(options.root);
|
|
301
259
|
const releaseWarnings = installRoleBootWarnings({
|
|
302
260
|
roles: selected,
|
|
303
261
|
config,
|
package/src/role-sync.ts
CHANGED
|
@@ -23,7 +23,8 @@ import {
|
|
|
23
23
|
SocketRegistry,
|
|
24
24
|
syncNode,
|
|
25
25
|
} from '@ultimat3/realtime/server';
|
|
26
|
-
import {
|
|
26
|
+
import { syncPortFor } from './flag-number';
|
|
27
|
+
import { freeDevPortPair } from './port-pair-free';
|
|
27
28
|
import { portFree } from './port-probe';
|
|
28
29
|
import type { StartRolesOptions } from './role-start';
|
|
29
30
|
import { startOrWaitForBus } from './role-sync-bus-wait';
|
|
@@ -48,15 +49,15 @@ import { DEV_BINDING } from './web-binding';
|
|
|
48
49
|
* "no findings — environment is shippable" when run (#F5).
|
|
49
50
|
*/
|
|
50
51
|
class SyncPortInUseError extends UltimateError {
|
|
51
|
-
constructor(input: { port: number; webPort: number }) {
|
|
52
|
+
constructor(input: { port: number; webPort: number; suggested: number }) {
|
|
52
53
|
super({
|
|
53
54
|
code: 'X_PORT_IN_USE',
|
|
54
55
|
cause: `the sync role binds PORT + 1, so \`x dev --port ${input.webPort}\` needs port ${input.port} and something is already listening on it`,
|
|
55
|
-
// `
|
|
56
|
+
// `freeDevPortPair`, never `neighbouringPort`: `x dev` binds a PAIR, so the neighbour of the
|
|
56
57
|
// web port IS the sync port this refusal is about — the fix said `x dev --port 4000` for a
|
|
57
58
|
// run that had just died on 4000, and a test named "its fix is a command that ends the
|
|
58
59
|
// failure" pinned it.
|
|
59
|
-
fix: `x dev --port ${quoteArg(String(
|
|
60
|
+
fix: `x dev --port ${quoteArg(String(input.suggested))} # or free port ${input.port}: lsof -nP -iTCP:${input.port} -sTCP:LISTEN`,
|
|
60
61
|
meta: { port: input.port, webPort: input.webPort },
|
|
61
62
|
});
|
|
62
63
|
}
|
|
@@ -77,7 +78,11 @@ export async function syncBindRefusal(
|
|
|
77
78
|
probe: (value: number) => Promise<boolean> = portFree,
|
|
78
79
|
): Promise<UltimateError | undefined> {
|
|
79
80
|
if (await probe(port)) return undefined;
|
|
80
|
-
return new SyncPortInUseError({
|
|
81
|
+
return new SyncPortInUseError({
|
|
82
|
+
port,
|
|
83
|
+
webPort,
|
|
84
|
+
suggested: await freeDevPortPair(webPort, probe),
|
|
85
|
+
});
|
|
81
86
|
}
|
|
82
87
|
|
|
83
88
|
// Its rule lives in `flag-number.ts`, where the scrape port's reservation reads it too (`rolePorts`).
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// The bust frame on `x.cache.invalidate`: the wire tags, then — when the sender's bust cleared a
|
|
2
|
+
// shared tier — one `{ scopes }` object naming what it cleared. Every receiver since v30 keeps the
|
|
3
|
+
// array's strings only, so an older pod reads the tags and busts every tier, as it always did.
|
|
4
|
+
|
|
5
|
+
import type { SharedTierScopes } from '@ultimat3/cache';
|
|
6
|
+
|
|
7
|
+
/** What a receiver reads off one frame: the tags, and the sender's scopes when it named any. */
|
|
8
|
+
export interface BustFrame {
|
|
9
|
+
readonly wire: readonly string[];
|
|
10
|
+
readonly sender?: SharedTierScopes;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** `CACHE_FLUSH_ALL`, parsed: a peer had more refused busts than it could keep. */
|
|
14
|
+
export const isFlushAll = (parsed: unknown): boolean =>
|
|
15
|
+
typeof parsed === 'object' &&
|
|
16
|
+
parsed !== null &&
|
|
17
|
+
!Array.isArray(parsed) &&
|
|
18
|
+
Reflect.get(parsed, 'flush') === 'all';
|
|
19
|
+
|
|
20
|
+
/** The payload a bust publishes. No shared tier cleared, it is the bare tag array of old. */
|
|
21
|
+
export function encodeBustFrame(wire: readonly string[], sender?: SharedTierScopes): string {
|
|
22
|
+
if (sender === undefined || Object.keys(sender).length === 0) return JSON.stringify(wire);
|
|
23
|
+
return JSON.stringify([...wire, { scopes: sender }]);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A parsed frame array, read. A scopes object that is malformed in any part is read as NO scopes —
|
|
28
|
+
* every tier is busted — because a skipped bust is a stale row for the whole TTL.
|
|
29
|
+
*/
|
|
30
|
+
export function decodeBustFrame(parsed: readonly unknown[]): BustFrame {
|
|
31
|
+
const wire = parsed.filter((value): value is string => typeof value === 'string');
|
|
32
|
+
const sender = parsed.map(scopesOf).find((scopes) => scopes !== undefined);
|
|
33
|
+
return sender === undefined ? { wire } : { wire, sender };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function scopesOf(value: unknown): SharedTierScopes | undefined {
|
|
37
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) return undefined;
|
|
38
|
+
const scopes: unknown = Reflect.get(value, 'scopes');
|
|
39
|
+
if (typeof scopes !== 'object' || scopes === null || Array.isArray(scopes)) return undefined;
|
|
40
|
+
const read: Record<string, string> = {};
|
|
41
|
+
for (const [name, scope] of Object.entries(scopes)) {
|
|
42
|
+
if (typeof scope !== 'string') return undefined;
|
|
43
|
+
read[name] = scope;
|
|
44
|
+
}
|
|
45
|
+
return read;
|
|
46
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// `cache.defaultTtlMs` as the boot hands it to the rungs it builds — refused here under the key
|
|
2
|
+
// the app wrote, never by a rung under the name of an option the app never set.
|
|
3
|
+
|
|
4
|
+
import { ConfigInvalidError } from '@ultimat3/core';
|
|
5
|
+
|
|
6
|
+
/** `describeValue` answers "a number" for 0 and 2.5 alike; a number is printed as itself. */
|
|
7
|
+
const shown = (value: number): string => `${value}`;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The lease option for a built rung, or nothing so the rung keeps its own default. A value that is
|
|
11
|
+
* not a whole number of milliseconds above zero is `X_CONFIG_INVALID` naming `cache.defaultTtlMs`:
|
|
12
|
+
* `lruTier` refuses the same value as `X_CACHE_LIMIT_INVALID` about "lru.defaultTtlMs", with a
|
|
13
|
+
* fix that points at `runtime.ts` — the wrong file for a key written in `app.config.ts`.
|
|
14
|
+
*/
|
|
15
|
+
export function defaultTtlOf(value: number | undefined): { defaultTtlMs?: number } {
|
|
16
|
+
if (value === undefined) return {};
|
|
17
|
+
if (Number.isSafeInteger(value) && value > 0) return { defaultTtlMs: value };
|
|
18
|
+
throw new ConfigInvalidError({
|
|
19
|
+
cause: `cache.defaultTtlMs must be a whole number of milliseconds of at least 1, not ${shown(value)}`,
|
|
20
|
+
fix: 'defineConfig({ cache: { defaultTtlMs: 60_000 } }) // in app.config.ts: the lease of a cached value set with no ttlMs',
|
|
21
|
+
meta: { key: 'cache.defaultTtlMs', value: shown(value) },
|
|
22
|
+
});
|
|
23
|
+
}
|
package/src/runtime-cache.ts
CHANGED
|
@@ -42,6 +42,8 @@ import { redisClient, redisUrlSource } from '@ultimat3/core/redis';
|
|
|
42
42
|
import type { Transport } from '@ultimat3/realtime/server';
|
|
43
43
|
import type { Env } from './runtime-bindings';
|
|
44
44
|
import { logCacheBus, selectCacheBus } from './runtime-cache-bus';
|
|
45
|
+
import { decodeBustFrame, encodeBustFrame, isFlushAll } from './runtime-cache-frame';
|
|
46
|
+
import { defaultTtlOf } from './runtime-cache-ttl';
|
|
45
47
|
|
|
46
48
|
/**
|
|
47
49
|
* The subject every replica of every app publishes tag invalidations on. One subject and not one
|
|
@@ -74,10 +76,17 @@ export interface CacheTiersOptions {
|
|
|
74
76
|
* a convention to remember. `cacheTiersOf` is what a boot holding the loaded config calls for it.
|
|
75
77
|
*/
|
|
76
78
|
readonly tiers: readonly CacheTierName[];
|
|
79
|
+
/**
|
|
80
|
+
* `config.cache.defaultTtlMs`: the lease of a value set with no `ttlMs`, on the `lru` and `redis`
|
|
81
|
+
* rungs the boot builds. Omitted, each rung keeps its own default. A supplied rung keeps whatever
|
|
82
|
+
* it was built with: the replacement is the whole rung.
|
|
83
|
+
*/
|
|
84
|
+
readonly defaultTtlMs?: number | undefined;
|
|
77
85
|
/**
|
|
78
86
|
* `runtime.ts`'s `cacheTiers`: each replaces the rung of its own `name`, and it is the ONE way
|
|
79
87
|
* to set a rung's limits (`lruTier({ maxBytes })`, `redisTier({ maxValueBytes, jitterFraction })`):
|
|
80
|
-
* `buildTier` passes none
|
|
88
|
+
* `buildTier` passes none but `defaultTtlMs`, and a config key would be a second spelling of
|
|
89
|
+
* this replacement. A
|
|
81
90
|
* supplied `redis` tier holding its own `client` needs no `REDIS_URL`; one without is
|
|
82
91
|
* `warnDefaultRedis`'s. A supplied tier `tiers` does not name is logged (`cache.tier.unnamed`) and
|
|
83
92
|
* never registered: the declaration decides the ladder.
|
|
@@ -145,6 +154,10 @@ const lazyRedis = (url: string, client: (url: string) => RedisLike): RedisLike =
|
|
|
145
154
|
send: (command, args) => client(url).send(command, args),
|
|
146
155
|
});
|
|
147
156
|
|
|
157
|
+
/** The boot's default lease, or nothing so the rung keeps its own. */
|
|
158
|
+
const ttlOf = (options: CacheTiersOptions): { defaultTtlMs?: number } =>
|
|
159
|
+
defaultTtlOf(options.defaultTtlMs);
|
|
160
|
+
|
|
148
161
|
/**
|
|
149
162
|
* One rung, or a refusal. A process that cannot build what it was configured to build must not
|
|
150
163
|
* start — `assertRateLimitScope`'s rule (`@ultimat3/http`), applied to the ladder: a fleet reading
|
|
@@ -160,7 +173,7 @@ function buildTier(name: CacheTierName, options: CacheTiersOptions): CacheTier {
|
|
|
160
173
|
case 'request-memo':
|
|
161
174
|
return memoTier();
|
|
162
175
|
case 'lru':
|
|
163
|
-
return lruTier();
|
|
176
|
+
return lruTier(ttlOf(options));
|
|
164
177
|
case 'redis': {
|
|
165
178
|
// `REDIS_URL` then `VALKEY_URL`, out of the boot's env: core's own order (`redisUrlSource`),
|
|
166
179
|
// so the URL checked here is the URL the client dials.
|
|
@@ -173,7 +186,10 @@ function buildTier(name: CacheTierName, options: CacheTiersOptions): CacheTier {
|
|
|
173
186
|
fix: 'REDIS_URL=redis://localhost:6379 # in .env — or drop the redis tier from cache.tiers in app.config.ts',
|
|
174
187
|
});
|
|
175
188
|
}
|
|
176
|
-
return redisTier({
|
|
189
|
+
return redisTier({
|
|
190
|
+
client: lazyRedis(source.url, options.redisClient ?? redisClient),
|
|
191
|
+
...ttlOf(options),
|
|
192
|
+
});
|
|
177
193
|
}
|
|
178
194
|
case 'cdn':
|
|
179
195
|
// A noop tier would put a `cdn` line in every invalidation report claiming keys an edge that
|
|
@@ -234,8 +250,12 @@ function warnDefaultRedis(built: readonly CacheTier[], env: Env): void {
|
|
|
234
250
|
*
|
|
235
251
|
* The outbound half publishes the wire tags this process just dropped; the inbound half applies
|
|
236
252
|
* another instance's. A message this process published is delivered back to it on every real bus
|
|
237
|
-
* and is applied again — deliberately, with no node-id filter
|
|
238
|
-
*
|
|
253
|
+
* and is applied again — deliberately, with no node-id filter — to the tiers this process OWNS
|
|
254
|
+
* (the in-process rungs): there, dropping an already-dropped key is idempotent and free, while a
|
|
255
|
+
* dedup table is state that can be wrong. A SHARED rung is not free: the frame names the scope
|
|
256
|
+
* (Redis namespace, per build) the sender's bust cleared, and a receiver skips its shared rung
|
|
257
|
+
* only for that same scope — another build's pod on a rolling deploy, or an older pod's frame
|
|
258
|
+
* naming none, still busts it (`runtime-cache-frame.ts`). Re-emit is impossible by
|
|
239
259
|
* construction, not by a flag: `receiveInvalidationBroadcast` is the only entry point that
|
|
240
260
|
* suppresses it, and `emit` is not a public parameter.
|
|
241
261
|
*/
|
|
@@ -247,6 +267,8 @@ export function startCacheTiers(options: CacheTiersOptions): () => Promise<void>
|
|
|
247
267
|
'firstDialGraceMs',
|
|
248
268
|
options.firstDialGraceMs ?? FIRST_DIAL_GRACE_MS,
|
|
249
269
|
);
|
|
270
|
+
// Whatever the ladder builds: the key is wrong in `app.config.ts`, and is named as that key.
|
|
271
|
+
defaultTtlOf(options.defaultTtlMs);
|
|
250
272
|
// Built before ANY of them is registered: a refusal halfway through a list would leave the
|
|
251
273
|
// process-global registry holding the rungs that came first, with no release returned to drop
|
|
252
274
|
// them. Registration order does not decide read order — `sortTiers` does, by `CACHE_TIERS`.
|
|
@@ -300,6 +322,8 @@ export function startCacheTiers(options: CacheTiersOptions): () => Promise<void>
|
|
|
300
322
|
return;
|
|
301
323
|
}
|
|
302
324
|
if (deferred.size === 0) return;
|
|
325
|
+
// Named no scope: the batch spans busts whose shared rungs may have refused, so every peer
|
|
326
|
+
// busts every rung — the cost of a missed skip is one load, of a wrong one a stale TTL.
|
|
303
327
|
const batch = [...deferred];
|
|
304
328
|
deferred.clear();
|
|
305
329
|
try {
|
|
@@ -348,9 +372,9 @@ export function startCacheTiers(options: CacheTiersOptions): () => Promise<void>
|
|
|
348
372
|
})();
|
|
349
373
|
};
|
|
350
374
|
|
|
351
|
-
registerInvalidationBroadcast(async (wireTags) => {
|
|
375
|
+
registerInvalidationBroadcast(async (wireTags, sender) => {
|
|
352
376
|
try {
|
|
353
|
-
await send(
|
|
377
|
+
await send(encodeBustFrame(wireTags, sender));
|
|
354
378
|
} catch (error) {
|
|
355
379
|
defer(wireTags);
|
|
356
380
|
// Still thrown: the report must say the peers have not been told YET.
|
|
@@ -447,13 +471,6 @@ export function startCacheTiers(options: CacheTiersOptions): () => Promise<void>
|
|
|
447
471
|
*/
|
|
448
472
|
export const broadcastErrorText = (error: unknown): string => renderThrowable(error);
|
|
449
473
|
|
|
450
|
-
/** `CACHE_FLUSH_ALL`, parsed: a peer had more refused busts than it could keep. */
|
|
451
|
-
const isFlushAll = (parsed: unknown): boolean =>
|
|
452
|
-
typeof parsed === 'object' &&
|
|
453
|
-
parsed !== null &&
|
|
454
|
-
!Array.isArray(parsed) &&
|
|
455
|
-
Reflect.get(parsed, 'flush') === 'all';
|
|
456
|
-
|
|
457
474
|
/**
|
|
458
475
|
* A peer's wire tags, applied here. Never throws: a malformed frame or an undeclared tag must not
|
|
459
476
|
* kill the subscriber loop, because that would silently end cross-instance invalidation for the
|
|
@@ -467,8 +484,8 @@ async function applyBroadcast(payload: string): Promise<void> {
|
|
|
467
484
|
return;
|
|
468
485
|
}
|
|
469
486
|
if (!Array.isArray(parsed)) return;
|
|
470
|
-
const wire = parsed
|
|
471
|
-
if (wire.length > 0) await receiveInvalidationBroadcast(wire);
|
|
487
|
+
const { wire, sender } = decodeBustFrame(parsed);
|
|
488
|
+
if (wire.length > 0) await receiveInvalidationBroadcast(wire, sender);
|
|
472
489
|
} catch (error) {
|
|
473
490
|
logger.warn('cache.broadcast.apply-failed', { error: broadcastErrorText(error) });
|
|
474
491
|
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// `app.config.ts`'s `database.ssl`, obeyed wherever this package opens a Postgres connection:
|
|
2
|
+
// `true` requires TLS on a connection string that states no mode of its own. What the operator
|
|
3
|
+
// wrote always wins.
|
|
4
|
+
|
|
5
|
+
import { loadAppConfig } from './app-config-load';
|
|
6
|
+
import type { DevServices, Env } from './runtime-bindings';
|
|
7
|
+
|
|
8
|
+
/** libpq's parameter, which `Bun.SQL` reads off the URL (measured on bun 1.4: `require` refuses a
|
|
9
|
+
* server with no TLS, `ERR_POSTGRES_TLS_NOT_AVAILABLE`). */
|
|
10
|
+
const SSL_MODE = 'sslmode';
|
|
11
|
+
|
|
12
|
+
/** The two spellings of the mode, as the driver reads them: by name, whatever the case. */
|
|
13
|
+
const isModeKey = (key: string): boolean => {
|
|
14
|
+
const name = key.toLowerCase();
|
|
15
|
+
return name === SSL_MODE || name === 'ssl';
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* `url` with `sslmode=require`, unless a mode is already stated — in the URL (`sslmode`, or the
|
|
20
|
+
* `ssl` spelling some dashboards hand out) or by `PGSSLMODE` in the boot's env. Never an override:
|
|
21
|
+
* replacing `verify-full` with `require` would drop certificate verification, and replacing
|
|
22
|
+
* `disable` would break the sidecar or loopback hop it was written for. `require` and not
|
|
23
|
+
* `verify-full` because a managed Postgres seldom ships its CA into the image; an operator who
|
|
24
|
+
* has one says so in the URL. An EMPTY `sslmode=` states nothing (it would otherwise be read as
|
|
25
|
+
* plaintext under `database.ssl: true`) and is replaced. A value no URL parser reads is handed on as it is: `postgresClient`
|
|
26
|
+
* refuses it by name (`X_DB_UNAVAILABLE`).
|
|
27
|
+
*/
|
|
28
|
+
export function withRequiredTls(url: string, env: Env): string {
|
|
29
|
+
if ((env['PGSSLMODE'] ?? '').trim() !== '') return url;
|
|
30
|
+
let parsed: URL;
|
|
31
|
+
try {
|
|
32
|
+
parsed = new URL(url);
|
|
33
|
+
} catch {
|
|
34
|
+
return url;
|
|
35
|
+
}
|
|
36
|
+
const modes = [...parsed.searchParams].filter(([key]) => isModeKey(key));
|
|
37
|
+
if (modes.some(([, value]) => value.trim() !== '')) return url;
|
|
38
|
+
for (const [key] of modes) parsed.searchParams.delete(key);
|
|
39
|
+
parsed.searchParams.set(SSL_MODE, 'require');
|
|
40
|
+
return parsed.toString();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Whether this boot's pool must be TLS: `database.ssl` of the app at `services.root`. `false` for
|
|
45
|
+
* the embedded database (PGlite is in this process: there is no wire) without reading the config,
|
|
46
|
+
* and for a root with no `app.config.ts`.
|
|
47
|
+
*/
|
|
48
|
+
export async function databaseSslOf(services: DevServices): Promise<boolean> {
|
|
49
|
+
if (services.db.mode !== 'external') return false;
|
|
50
|
+
return (await loadAppConfig(services.root))?.database.ssl === true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The connection strings a role reads off its env rather than through `startDb`. */
|
|
54
|
+
const URL_KEYS = ['DATABASE_URL', 'REPLICATION_URL'] as const;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* `withRequiredTls` for a pool that never calls `startDb` — `x doctor`'s probes, `x db branch`,
|
|
58
|
+
* `x deploy verify`'s queue read — when the app at `root` declares `database.ssl`. Only a Postgres
|
|
59
|
+
* URL is touched: the embedded database's is a directory, and has no wire to encrypt.
|
|
60
|
+
*/
|
|
61
|
+
export async function withAppTls<U extends string | undefined>(
|
|
62
|
+
root: string | undefined,
|
|
63
|
+
url: U,
|
|
64
|
+
env: Env,
|
|
65
|
+
): Promise<U | string> {
|
|
66
|
+
if (root === undefined || url === undefined || !/^postgres(?:ql)?:\/\//i.test(url.trim())) {
|
|
67
|
+
return url;
|
|
68
|
+
}
|
|
69
|
+
if ((await loadAppConfig(root))?.database.ssl !== true) return url;
|
|
70
|
+
return withRequiredTls(url, env);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* `env` as a role that dials from it must read it (the replicator's `REPLICATION_URL` session and
|
|
75
|
+
* its advisory lock): each connection string under the same rule. Untouched without the switch.
|
|
76
|
+
*/
|
|
77
|
+
export async function envWithAppTls(root: string, env: Env): Promise<Env> {
|
|
78
|
+
const next: Record<string, string | undefined> = { ...env };
|
|
79
|
+
for (const key of URL_KEYS) {
|
|
80
|
+
const url = env[key];
|
|
81
|
+
if (url !== undefined && url.trim() !== '') next[key] = await withAppTls(root, url, env);
|
|
82
|
+
}
|
|
83
|
+
return next;
|
|
84
|
+
}
|
|
@@ -4,13 +4,27 @@
|
|
|
4
4
|
// `assertIdempotencyScope` only fires on a `'shared'` DECLARATION and nothing infers a replica count.
|
|
5
5
|
|
|
6
6
|
import { getIdempotencyStore, type IdempotencyStore } from '@ultimat3/action';
|
|
7
|
-
import { logger } from '@ultimat3/core';
|
|
7
|
+
import { logger, supervisedChild } from '@ultimat3/core';
|
|
8
|
+
import { unsharedStores } from './supervise-count';
|
|
9
|
+
import { ProcessesUnsharedStateError } from './supervise-errors';
|
|
8
10
|
|
|
9
|
-
/**
|
|
11
|
+
/**
|
|
12
|
+
* `true` when it warned. Said, not refused: one replica is a real deployment and is correct. In one
|
|
13
|
+
* of several supervised children it IS refused — the supervisor could not see a store an app module
|
|
14
|
+
* swapped in, and the siblings sharing this port would each answer a retry from their own records.
|
|
15
|
+
*/
|
|
10
16
|
export function warnIfIdempotencyProcessScoped(
|
|
11
17
|
store: IdempotencyStore = getIdempotencyStore(),
|
|
18
|
+
env: Readonly<Record<string, string | undefined>> = process.env,
|
|
12
19
|
): boolean {
|
|
13
20
|
if (store.scope === 'shared') return false;
|
|
21
|
+
const child = supervisedChild(env);
|
|
22
|
+
if (child !== undefined && child.count > 1) {
|
|
23
|
+
throw new ProcessesUnsharedStateError({
|
|
24
|
+
requested: child.count,
|
|
25
|
+
reasons: unsharedStores([{ name: 'the idempotency store', scope: store.scope }]),
|
|
26
|
+
});
|
|
27
|
+
}
|
|
14
28
|
logger.warn('X_CONFIG_INVALID', {
|
|
15
29
|
code: 'X_CONFIG_INVALID',
|
|
16
30
|
cause:
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// A supervised worker child's part of the container's job slots. `jobs.concurrency` and
|
|
2
|
+
// `jobs.maxInFlight` are per-CONTAINER totals, split across `ULTIMATE_CHILD`'s N children with
|
|
3
|
+
// core's `childShare`; a total below N is served only by children with index < total, so a
|
|
4
|
+
// `concurrency: 1` queue stays strictly serial across the container.
|
|
5
|
+
|
|
6
|
+
import type { JobsConcurrency, SupervisedChild } from '@ultimat3/core';
|
|
7
|
+
import { childShare, JOBS_CONCURRENCY_DEFAULT } from '@ultimat3/core';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* One total's share for this child. `childShare` never answers below 1 (a pool must hand every
|
|
11
|
+
* child a connection), which for slots would turn 1 into N: a child the total has no slot left for
|
|
12
|
+
* gets 0, and a queue at 0 free slots is never asked for a row (`worker-admit.ts`'s `claimAsks`).
|
|
13
|
+
*/
|
|
14
|
+
export function childSlots(total: number, child: SupervisedChild): number {
|
|
15
|
+
return child.index < total ? childShare(total, child.index, child.count) : 0;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** A queue's container total, by OWN key — `worker-options.ts`'s `slotTable` rule. */
|
|
19
|
+
const totalFor = (declared: JobsConcurrency | undefined, queue: string): number => {
|
|
20
|
+
if (typeof declared === 'number') return declared;
|
|
21
|
+
if (declared === undefined || !Object.hasOwn(declared, queue)) return JOBS_CONCURRENCY_DEFAULT;
|
|
22
|
+
return declared[queue] ?? JOBS_CONCURRENCY_DEFAULT;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export interface ChildWorkerShare {
|
|
26
|
+
/** Every served queue named, so no queue falls back to an unsplit default inside `jobWorker`. */
|
|
27
|
+
readonly concurrency: Readonly<Record<string, number>>;
|
|
28
|
+
/** Absent when the container has no cap, or when this child is past it and claims nothing. */
|
|
29
|
+
readonly maxInFlight?: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* This child's slot table and cap. A child past `maxInFlight` keeps its queues (its registry row
|
|
34
|
+
* still says what the container serves) at 0 slots each: `jobWorker` refuses a cap below 1.
|
|
35
|
+
*/
|
|
36
|
+
export function childWorkerShare(input: {
|
|
37
|
+
readonly queues: readonly string[];
|
|
38
|
+
readonly concurrency: JobsConcurrency | undefined;
|
|
39
|
+
readonly maxInFlight: number | undefined;
|
|
40
|
+
readonly child: SupervisedChild;
|
|
41
|
+
}): ChildWorkerShare {
|
|
42
|
+
const cap =
|
|
43
|
+
input.maxInFlight === undefined ? undefined : childSlots(input.maxInFlight, input.child);
|
|
44
|
+
const idle = cap === 0;
|
|
45
|
+
const concurrency = Object.fromEntries(
|
|
46
|
+
input.queues.map((queue) => [
|
|
47
|
+
queue,
|
|
48
|
+
idle ? 0 : childSlots(totalFor(input.concurrency, queue), input.child),
|
|
49
|
+
]),
|
|
50
|
+
);
|
|
51
|
+
return cap === undefined || idle ? { concurrency } : { concurrency, maxInFlight: cap };
|
|
52
|
+
}
|
package/src/runtime-jobs.ts
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
// Deployment's env
|
|
4
|
-
//
|
|
1
|
+
// What the boot reads of `jobs.*` off the app's own `app.config.ts`: the queues a `worker` serves
|
|
2
|
+
// and the per-CONTAINER slots each gets (a supervised child's share: `runtime-jobs-share.ts`),
|
|
3
|
+
// `WORKER_QUEUES` off the Deployment's env (issues #673, #676), and the retry of a job that
|
|
4
|
+
// declares none (`jobs.maxAttempts` / `jobs.backoff`), installed for every role.
|
|
5
5
|
|
|
6
|
-
import type { AppConfig, JobsConcurrency } from '@ultimat3/core';
|
|
6
|
+
import type { AppConfig, JobsConcurrency, SupervisedChild } from '@ultimat3/core';
|
|
7
7
|
import {
|
|
8
8
|
ConfigInvalidError,
|
|
9
9
|
renderCauseValue,
|
|
10
10
|
renderFixShellArg,
|
|
11
|
+
supervisedChild,
|
|
11
12
|
UltimateError,
|
|
12
13
|
} from '@ultimat3/core';
|
|
13
|
-
import { registeredJobs, type WorkerOptions } from '@ultimat3/jobs';
|
|
14
|
+
import { DEFAULT_QUEUE, registeredJobs, type WorkerOptions } from '@ultimat3/jobs';
|
|
15
|
+
import { installDefaultRetry, resetDefaultRetry } from '@ultimat3/jobs/adapter';
|
|
14
16
|
import type { Env } from './runtime-bindings';
|
|
17
|
+
import { childWorkerShare } from './runtime-jobs-share';
|
|
15
18
|
|
|
16
19
|
/** The env key a worker Deployment names its queues with: `WORKER_QUEUES=banks,banks-long`. */
|
|
17
20
|
export const WORKER_QUEUES_ENV = 'WORKER_QUEUES';
|
|
@@ -29,10 +32,13 @@ export interface WorkerConfig {
|
|
|
29
32
|
* the registered jobs' queues, the set an exact entry must belong to (`refuseUnknownQueues`).
|
|
30
33
|
*/
|
|
31
34
|
readonly configured: readonly string[];
|
|
35
|
+
/** Per CONTAINER: a supervised child runs its `child` share of each total. */
|
|
32
36
|
readonly concurrency: JobsConcurrency | undefined;
|
|
33
|
-
/** `jobs.maxInFlight`: runs across every queue
|
|
37
|
+
/** `jobs.maxInFlight`: runs across every queue the container serves. Unset: no cap. */
|
|
34
38
|
readonly maxInFlight?: number | undefined;
|
|
35
39
|
readonly visibilityTimeoutMs: number | undefined;
|
|
40
|
+
/** `ULTIMATE_CHILD`, read once: absent when this process is the container's only one. */
|
|
41
|
+
readonly child?: SupervisedChild | undefined;
|
|
36
42
|
}
|
|
37
43
|
|
|
38
44
|
/** A root with no `app.config.ts`: no queue of its own, and the worker's own timings. */
|
|
@@ -64,6 +70,20 @@ export function workerQueuesFromEnv(env: Env): readonly string[] | undefined {
|
|
|
64
70
|
});
|
|
65
71
|
}
|
|
66
72
|
|
|
73
|
+
/**
|
|
74
|
+
* `jobs.maxAttempts` and `jobs.backoff`, installed as the retry of a job that declares none. In
|
|
75
|
+
* EVERY role, never the worker alone: the process that enqueues stamps the row's `max_attempts`
|
|
76
|
+
* from the policy the worker retries by. No config is the framework's own default again.
|
|
77
|
+
*/
|
|
78
|
+
export function installJobsRetry(config: AppConfig | undefined): void {
|
|
79
|
+
if (config === undefined) resetDefaultRetry();
|
|
80
|
+
else installDefaultRetry({ attempts: config.jobs.maxAttempts, backoff: config.jobs.backoff });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* What the boot reads of `jobs.*` for the worker, once. It installs nothing: the default retry is
|
|
85
|
+
* every role's, so the boot calls `installJobsRetry` itself (`startQueue`, `runtime-queue.ts`).
|
|
86
|
+
*/
|
|
67
87
|
export function workerConfigOf(config: AppConfig | undefined, env: Env = {}): WorkerConfig {
|
|
68
88
|
const fromEnv = workerQueuesFromEnv(env);
|
|
69
89
|
const base =
|
|
@@ -77,7 +97,9 @@ export function workerConfigOf(config: AppConfig | undefined, env: Env = {}): Wo
|
|
|
77
97
|
maxInFlight: config.jobs.maxInFlight,
|
|
78
98
|
visibilityTimeoutMs: config.jobs.visibilityTimeoutMs,
|
|
79
99
|
};
|
|
80
|
-
|
|
100
|
+
const child = supervisedChild(env);
|
|
101
|
+
const read = child === undefined ? base : { ...base, child };
|
|
102
|
+
return fromEnv === undefined ? read : { ...read, queues: fromEnv, exact: true };
|
|
81
103
|
}
|
|
82
104
|
|
|
83
105
|
/**
|
|
@@ -167,13 +189,22 @@ export function workerOptionsFor(config: WorkerConfig | undefined): WorkerConfig
|
|
|
167
189
|
const exact = config?.exact === true;
|
|
168
190
|
if (exact) refuseUnknownQueues(config?.queues ?? [], config?.configured ?? [], registered);
|
|
169
191
|
const queues = workerQueuesFor(config?.queues ?? [], registered, exact);
|
|
170
|
-
//
|
|
171
|
-
// core's `JOBS_CONCURRENCY_DEFAULT
|
|
172
|
-
const
|
|
192
|
+
// Unsupervised, passed as written: `jobWorker` reads a table by own key and runs a queue it does
|
|
193
|
+
// not name at core's `JOBS_CONCURRENCY_DEFAULT`. A child gets every served queue named, its share.
|
|
194
|
+
const child = config?.child;
|
|
195
|
+
const { concurrency, maxInFlight } =
|
|
196
|
+
child === undefined
|
|
197
|
+
? { concurrency: config?.concurrency, maxInFlight: config?.maxInFlight }
|
|
198
|
+
: childWorkerShare({
|
|
199
|
+
queues: queues ?? [DEFAULT_QUEUE],
|
|
200
|
+
concurrency: config?.concurrency,
|
|
201
|
+
maxInFlight: config?.maxInFlight,
|
|
202
|
+
child,
|
|
203
|
+
});
|
|
173
204
|
return {
|
|
174
205
|
...(queues === undefined ? {} : { queues }),
|
|
175
206
|
...(concurrency === undefined ? {} : { concurrency }),
|
|
176
|
-
...(
|
|
207
|
+
...(maxInFlight === undefined ? {} : { maxInFlight }),
|
|
177
208
|
...(config?.visibilityTimeoutMs === undefined
|
|
178
209
|
? {}
|
|
179
210
|
: { visibilityTimeoutMs: config.visibilityTimeoutMs }),
|