@substrat-run/adapter-cloudflare 0.229.0 → 0.230.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/broker-lake.d.ts +26 -2
- package/dist/broker-lake.d.ts.map +1 -1
- package/dist/broker-lake.js +56 -3
- package/dist/broker-lake.js.map +1 -1
- package/dist/control-plane-do.d.ts +41 -11
- package/dist/control-plane-do.d.ts.map +1 -1
- package/dist/control-plane-do.js +100 -48
- package/dist/control-plane-do.js.map +1 -1
- package/dist/delivery-broker-do.d.ts +13 -1
- package/dist/delivery-broker-do.d.ts.map +1 -1
- package/dist/delivery-broker-do.js +43 -3
- package/dist/delivery-broker-do.js.map +1 -1
- package/dist/directory-cutover.d.ts +93 -1
- package/dist/directory-cutover.d.ts.map +1 -1
- package/dist/directory-cutover.js +85 -2
- package/dist/directory-cutover.js.map +1 -1
- package/dist/directory-objects.d.ts +12 -0
- package/dist/directory-objects.d.ts.map +1 -1
- package/dist/directory-objects.js +8 -1
- package/dist/directory-objects.js.map +1 -1
- package/dist/directory-placement.d.ts.map +1 -1
- package/dist/directory-placement.js +1 -0
- package/dist/directory-placement.js.map +1 -1
- package/dist/directory-tenant-identities.d.ts +30 -0
- package/dist/directory-tenant-identities.d.ts.map +1 -0
- package/dist/directory-tenant-identities.js +64 -0
- package/dist/directory-tenant-identities.js.map +1 -0
- package/dist/directory-tenant-tuples.d.ts +30 -3
- package/dist/directory-tenant-tuples.d.ts.map +1 -1
- package/dist/directory-tenant-tuples.js +27 -0
- package/dist/directory-tenant-tuples.js.map +1 -1
- package/dist/host.d.ts +27 -1
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +81 -9
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/peer-call-target.d.ts +16 -2
- package/dist/peer-call-target.d.ts.map +1 -1
- package/dist/peer-call-target.js +36 -6
- package/dist/peer-call-target.js.map +1 -1
- package/dist/route-read-model-publisher.d.ts.map +1 -1
- package/dist/route-read-model-publisher.js +4 -0
- package/dist/route-read-model-publisher.js.map +1 -1
- package/dist/route-resolver.d.ts +19 -2
- package/dist/route-resolver.d.ts.map +1 -1
- package/dist/route-resolver.js +25 -5
- package/dist/route-resolver.js.map +1 -1
- package/dist/scope-do.d.ts +19 -0
- package/dist/scope-do.d.ts.map +1 -1
- package/dist/scope-do.js +294 -8
- package/dist/scope-do.js.map +1 -1
- package/dist/scope-facets.d.ts +121 -0
- package/dist/scope-facets.d.ts.map +1 -0
- package/dist/scope-facets.js +98 -0
- package/dist/scope-facets.js.map +1 -0
- package/dist/scope-supervisor-do.d.ts +93 -0
- package/dist/scope-supervisor-do.d.ts.map +1 -0
- package/dist/scope-supervisor-do.js +504 -0
- package/dist/scope-supervisor-do.js.map +1 -0
- package/dist/scope-work.d.ts +1 -1
- package/dist/scope-work.js +1 -1
- package/dist/scope-work.js.map +1 -1
- package/dist/supplied-pass-box.d.ts +77 -0
- package/dist/supplied-pass-box.d.ts.map +1 -0
- package/dist/supplied-pass-box.js +265 -0
- package/dist/supplied-pass-box.js.map +1 -0
- package/dist/tenant-due-index-do.d.ts +76 -0
- package/dist/tenant-due-index-do.d.ts.map +1 -0
- package/dist/tenant-due-index-do.js +90 -0
- package/dist/tenant-due-index-do.js.map +1 -0
- package/dist/tenant-due-nudge.d.ts +66 -0
- package/dist/tenant-due-nudge.d.ts.map +1 -0
- package/dist/tenant-due-nudge.js +77 -0
- package/dist/tenant-due-nudge.js.map +1 -0
- package/dist/tenant-due-work-do.d.ts +26 -0
- package/dist/tenant-due-work-do.d.ts.map +1 -1
- package/dist/tenant-due-work-do.js +69 -5
- package/dist/tenant-due-work-do.js.map +1 -1
- package/package.json +12 -12
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* D-85 (#3752): the pass box the uploader supplies to a pushed vertical that places a job in a
|
|
3
|
+
* container, bound as `SUBSTRAT_PASSES` (`withPlatformEntry`), the way it supplies the sweeper and the
|
|
4
|
+
* delivery broker.
|
|
5
|
+
*
|
|
6
|
+
* Three things, and no kernel of its own:
|
|
7
|
+
*
|
|
8
|
+
* - **The launcher.** Published at module evaluation under `substrat.container-passes`, where the
|
|
9
|
+
* vertical's own scope host looks for one (`registeredContainerPasses`). Its placements are the
|
|
10
|
+
* ones `substrat push` wrote into the bundle's `substrat-passes.js`, which the entry imports and
|
|
11
|
+
* which publishes them under `substrat.container-pass-jobs`.
|
|
12
|
+
* - **The box**, one Durable Object per run. It starts the run's container with no internet, the
|
|
13
|
+
* job's runtime image and instance type, and one intercepted hostname, `run.substrat.internal`,
|
|
14
|
+
* which reaches the door below. An alarm at the pass's deadline fails the pass through the door and
|
|
15
|
+
* stops the container; a pass that ends stops it shortly after.
|
|
16
|
+
* - **The door.** Every request the container makes to `run.substrat.internal` is handed to the
|
|
17
|
+
* vertical's own scope host (`serveContainerPass`, through the host the vertical's
|
|
18
|
+
* `mountPlatformSurface` registered under `substrat.scope-due-host`), so the door runs the kernel
|
|
19
|
+
* the vertical shipped. The run's identity and lease travel in the door's props, which only this
|
|
20
|
+
* script can set, so the container holds no credential.
|
|
21
|
+
*
|
|
22
|
+
* - **The public read** (D-86). Every other request the container makes, HTTP or HTTPS, is
|
|
23
|
+
* intercepted and judged by contracts' `publicReadRefusal`: a job that declares `publicRead` may
|
|
24
|
+
* `GET` and `HEAD` public addresses, with credentials stripped, the body capped at its declared
|
|
25
|
+
* maximum and each read written as an `egress` line with its host and bytes. A job that declares
|
|
26
|
+
* none reaches nothing but its door. The container has no other route out.
|
|
27
|
+
*
|
|
28
|
+
* The outcome never comes from the container's exit: `monitor()` resolves some twenty seconds late
|
|
29
|
+
* (the spike, `spikes/container-passes`). It comes through the door, as the pass commits or fails.
|
|
30
|
+
*
|
|
31
|
+
* Bundled on its own by `tools/platform-entry-emit.mts` into the module the entry re-exports, so it
|
|
32
|
+
* imports nothing but `cloudflare:workers` and contracts' dependency-free public-read rule, and the keys and names it shares with contracts and the
|
|
33
|
+
* kernel are spelled out; control-plane-api's `platform-entry.test.ts` holds them equal.
|
|
34
|
+
*/
|
|
35
|
+
import { DurableObject, WorkerEntrypoint } from 'cloudflare:workers';
|
|
36
|
+
interface Placement {
|
|
37
|
+
readonly runtime: string;
|
|
38
|
+
readonly instance: string;
|
|
39
|
+
readonly passDeadlineMs: number;
|
|
40
|
+
readonly publicRead?: {
|
|
41
|
+
readonly maxBytes: number;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
interface Ticket {
|
|
45
|
+
readonly runId: string;
|
|
46
|
+
readonly moduleId: string;
|
|
47
|
+
readonly job: string;
|
|
48
|
+
readonly instance: string;
|
|
49
|
+
}
|
|
50
|
+
interface Launch {
|
|
51
|
+
readonly tenantId: string;
|
|
52
|
+
readonly scopeId: string;
|
|
53
|
+
readonly ticket: Ticket;
|
|
54
|
+
readonly placement: Placement;
|
|
55
|
+
}
|
|
56
|
+
export declare class SubstratPassBox extends DurableObject {
|
|
57
|
+
private container;
|
|
58
|
+
/** Start one pass's container. A container still running for this run is an earlier pass: it is stopped first. */
|
|
59
|
+
start(launch: Launch): Promise<void>;
|
|
60
|
+
/** The pass committed or failed: stop its container once the last reply has landed. */
|
|
61
|
+
ended(): Promise<void>;
|
|
62
|
+
alarm(): Promise<void>;
|
|
63
|
+
}
|
|
64
|
+
export declare class SubstratPassDoor extends WorkerEntrypoint {
|
|
65
|
+
fetch(request: Request): Promise<Response>;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* D-86: every request the container makes that is not its door. A job with no `publicRead` is told so;
|
|
69
|
+
* one with it reads public addresses under the rule. Redirects are handed back to the container, so
|
|
70
|
+
* each hop comes through here and is judged on its own. Every verdict is one `egress` line, the
|
|
71
|
+
* request record Workers Logs keeps for the tenant.
|
|
72
|
+
*/
|
|
73
|
+
export declare class SubstratPassEgress extends WorkerEntrypoint {
|
|
74
|
+
fetch(request: Request): Promise<Response>;
|
|
75
|
+
}
|
|
76
|
+
export {};
|
|
77
|
+
//# sourceMappingURL=supplied-pass-box.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"supplied-pass-box.d.ts","sourceRoot":"","sources":["../src/supplied-pass-box.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAoB,MAAM,oBAAoB,CAAC;AAiDvF,UAAU,SAAS;IACjB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,UAAU,CAAC,EAAE;QAAE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;CACrD;AACD,UAAU,MAAM;IACd,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B;AACD,UAAU,MAAM;IACd,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;CAC/B;AA6ED,qBAAa,eAAgB,SAAQ,aAAa;IAChD,OAAO,CAAC,SAAS;IAMjB,kHAAkH;IAC5G,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CA8BzC;IAED,uFAAuF;IACjF,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAG3B;IAEK,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAc3B;CACF;AAED,qBAAa,gBAAiB,SAAQ,gBAAgB;IACrC,KAAK,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAYxD;CACF;AAUD;;;;;GAKG;AACH,qBAAa,kBAAmB,SAAQ,gBAAgB;IACvC,KAAK,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAkExD;CACF"}
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* D-85 (#3752): the pass box the uploader supplies to a pushed vertical that places a job in a
|
|
3
|
+
* container, bound as `SUBSTRAT_PASSES` (`withPlatformEntry`), the way it supplies the sweeper and the
|
|
4
|
+
* delivery broker.
|
|
5
|
+
*
|
|
6
|
+
* Three things, and no kernel of its own:
|
|
7
|
+
*
|
|
8
|
+
* - **The launcher.** Published at module evaluation under `substrat.container-passes`, where the
|
|
9
|
+
* vertical's own scope host looks for one (`registeredContainerPasses`). Its placements are the
|
|
10
|
+
* ones `substrat push` wrote into the bundle's `substrat-passes.js`, which the entry imports and
|
|
11
|
+
* which publishes them under `substrat.container-pass-jobs`.
|
|
12
|
+
* - **The box**, one Durable Object per run. It starts the run's container with no internet, the
|
|
13
|
+
* job's runtime image and instance type, and one intercepted hostname, `run.substrat.internal`,
|
|
14
|
+
* which reaches the door below. An alarm at the pass's deadline fails the pass through the door and
|
|
15
|
+
* stops the container; a pass that ends stops it shortly after.
|
|
16
|
+
* - **The door.** Every request the container makes to `run.substrat.internal` is handed to the
|
|
17
|
+
* vertical's own scope host (`serveContainerPass`, through the host the vertical's
|
|
18
|
+
* `mountPlatformSurface` registered under `substrat.scope-due-host`), so the door runs the kernel
|
|
19
|
+
* the vertical shipped. The run's identity and lease travel in the door's props, which only this
|
|
20
|
+
* script can set, so the container holds no credential.
|
|
21
|
+
*
|
|
22
|
+
* - **The public read** (D-86). Every other request the container makes, HTTP or HTTPS, is
|
|
23
|
+
* intercepted and judged by contracts' `publicReadRefusal`: a job that declares `publicRead` may
|
|
24
|
+
* `GET` and `HEAD` public addresses, with credentials stripped, the body capped at its declared
|
|
25
|
+
* maximum and each read written as an `egress` line with its host and bytes. A job that declares
|
|
26
|
+
* none reaches nothing but its door. The container has no other route out.
|
|
27
|
+
*
|
|
28
|
+
* The outcome never comes from the container's exit: `monitor()` resolves some twenty seconds late
|
|
29
|
+
* (the spike, `spikes/container-passes`). It comes through the door, as the pass commits or fails.
|
|
30
|
+
*
|
|
31
|
+
* Bundled on its own by `tools/platform-entry-emit.mts` into the module the entry re-exports, so it
|
|
32
|
+
* imports nothing but `cloudflare:workers` and contracts' dependency-free public-read rule, and the keys and names it shares with contracts and the
|
|
33
|
+
* kernel are spelled out; control-plane-api's `platform-entry.test.ts` holds them equal.
|
|
34
|
+
*/
|
|
35
|
+
import { DurableObject, WorkerEntrypoint, env as scriptEnv } from 'cloudflare:workers';
|
|
36
|
+
import { PUBLIC_READ_DROPPED_REQUEST_HEADERS, PUBLIC_READ_DROPPED_RESPONSE_HEADERS, PUBLIC_READ_TIMEOUT_MS, publicReadRefusal, } from '@substrat-run/contracts/public-read';
|
|
37
|
+
const CONTAINER_PASSES_KEY = 'substrat.container-passes';
|
|
38
|
+
const PASS_JOBS_KEY = 'substrat.container-pass-jobs';
|
|
39
|
+
const SCOPE_DUE_HOST_KEY = 'substrat.scope-due-host';
|
|
40
|
+
const BINDING = 'SUBSTRAT_PASSES';
|
|
41
|
+
const DOOR_ENTRYPOINT = 'SubstratPassDoor';
|
|
42
|
+
const EGRESS_ENTRYPOINT = 'SubstratPassEgress';
|
|
43
|
+
/** Where the platform's base domains reach the box: a plain-text binding the uploader writes. */
|
|
44
|
+
const PLATFORM_DOMAINS_VAR = 'SUBSTRAT_PLATFORM_DOMAINS';
|
|
45
|
+
/** The certificate the HTTPS intercept signs with, which the container's Node must trust. */
|
|
46
|
+
const INTERCEPT_CA = '/etc/cloudflare/certs/cloudflare-containers-ca.crt';
|
|
47
|
+
const DOOR_HOST = 'run.substrat.internal';
|
|
48
|
+
/** The managed image `node24` runs on: Node 24, and no registry image to pull. */
|
|
49
|
+
const MANAGED_NODE24 = 'cloudflare/debian-trixie';
|
|
50
|
+
/** How long a box waits after its pass ended before it stops the container, so the last reply lands. */
|
|
51
|
+
const ENDED_GRACE_MS = 15_000;
|
|
52
|
+
/**
|
|
53
|
+
* What the container runs: fetch the pass's bundle from the door, import it, and call its default
|
|
54
|
+
* export (`containerPass(handler)`) with the door. The pass itself commits or fails through the door;
|
|
55
|
+
* this only covers a bundle that never started.
|
|
56
|
+
*/
|
|
57
|
+
const BOOT = `
|
|
58
|
+
const door = 'http://${DOOR_HOST}';
|
|
59
|
+
(async () => {
|
|
60
|
+
const res = await fetch(door + '/bundle');
|
|
61
|
+
if (!res.ok) throw new Error('the door served no bundle (' + res.status + ')');
|
|
62
|
+
const fs = await import('node:fs');
|
|
63
|
+
fs.writeFileSync('/tmp/substrat-pass.mjs', await res.text());
|
|
64
|
+
const pass = await import('/tmp/substrat-pass.mjs');
|
|
65
|
+
await pass.default({ door, fetch: globalThis.fetch.bind(globalThis) });
|
|
66
|
+
process.exit(0);
|
|
67
|
+
})().catch(async (err) => {
|
|
68
|
+
console.error(err);
|
|
69
|
+
try {
|
|
70
|
+
await fetch(door + '/fail', { method: 'POST', headers: { 'content-type': 'application/json' },
|
|
71
|
+
body: JSON.stringify({ error: 'the pass did not start: ' + String((err && err.message) || err) }) });
|
|
72
|
+
} catch {}
|
|
73
|
+
process.exit(1);
|
|
74
|
+
});
|
|
75
|
+
`;
|
|
76
|
+
const globals = globalThis;
|
|
77
|
+
function passJobs() {
|
|
78
|
+
return globals[Symbol.for(PASS_JOBS_KEY)] ?? {};
|
|
79
|
+
}
|
|
80
|
+
function boxOf(env, runId) {
|
|
81
|
+
const ns = env[BINDING];
|
|
82
|
+
if (!ns)
|
|
83
|
+
throw new Error(`this deployment has no ${BINDING} binding for its container passes`);
|
|
84
|
+
return ns.getByName(runId);
|
|
85
|
+
}
|
|
86
|
+
function hostOf(env) {
|
|
87
|
+
const hostFor = globals[Symbol.for(SCOPE_DUE_HOST_KEY)];
|
|
88
|
+
const host = typeof hostFor === 'function' ? hostFor(env) : undefined;
|
|
89
|
+
if (typeof host?.serveContainerPass !== 'function') {
|
|
90
|
+
throw new Error("this deployment's scope host serves no container pass door: update @substrat-run/adapter-cloudflare and @substrat-run/vertical-host and push again");
|
|
91
|
+
}
|
|
92
|
+
return host;
|
|
93
|
+
}
|
|
94
|
+
const doorRequest = (path, body) => new Request(`http://${DOOR_HOST}${path}`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) });
|
|
95
|
+
globals[Symbol.for(CONTAINER_PASSES_KEY)] ??= {
|
|
96
|
+
placementOf: (_moduleId, job) => passJobs()[job]?.placement,
|
|
97
|
+
launch: async (launch) => {
|
|
98
|
+
const { tenantId, scopeId, ticket, placement } = launch;
|
|
99
|
+
await boxOf(scriptEnv, ticket.runId).start({
|
|
100
|
+
tenantId,
|
|
101
|
+
scopeId,
|
|
102
|
+
ticket,
|
|
103
|
+
placement: {
|
|
104
|
+
runtime: placement.runtime,
|
|
105
|
+
instance: placement.instance,
|
|
106
|
+
passDeadlineMs: placement.passDeadlineMs,
|
|
107
|
+
...(placement.publicRead ? { publicRead: { maxBytes: placement.publicRead.maxBytes } } : {}),
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
export class SubstratPassBox extends DurableObject {
|
|
113
|
+
container() {
|
|
114
|
+
const c = this.ctx.container;
|
|
115
|
+
if (!c)
|
|
116
|
+
throw new Error('this deployment carries no container for its passes');
|
|
117
|
+
return c;
|
|
118
|
+
}
|
|
119
|
+
/** Start one pass's container. A container still running for this run is an earlier pass: it is stopped first. */
|
|
120
|
+
async start(launch) {
|
|
121
|
+
const c = this.container();
|
|
122
|
+
if (c.running)
|
|
123
|
+
await c.destroy('a later pass of this run took over').catch(() => undefined);
|
|
124
|
+
const image = launch.placement.runtime === 'node24' ? MANAGED_NODE24 : c.images?.[launch.placement.runtime];
|
|
125
|
+
if (!image)
|
|
126
|
+
throw new Error(`this deployment carries no image for the runtime '${launch.placement.runtime}'`);
|
|
127
|
+
await this.ctx.storage.delete('ended');
|
|
128
|
+
await this.ctx.storage.put('launch', launch);
|
|
129
|
+
const exports = this.ctx.exports;
|
|
130
|
+
const props = { tenantId: launch.tenantId, scopeId: launch.scopeId, ticket: launch.ticket };
|
|
131
|
+
// The door first: a hostname intercept registered after the catch-all would receive nothing.
|
|
132
|
+
await c.interceptOutboundHttp(DOOR_HOST, exports[DOOR_ENTRYPOINT]({ props }));
|
|
133
|
+
const egress = exports[EGRESS_ENTRYPOINT]({
|
|
134
|
+
props: { ...props, maxBytes: launch.placement.publicRead?.maxBytes ?? null },
|
|
135
|
+
});
|
|
136
|
+
await c.interceptAllOutboundHttp(egress);
|
|
137
|
+
await c.interceptOutboundHttps('*', egress);
|
|
138
|
+
c.start({
|
|
139
|
+
image,
|
|
140
|
+
instance: launch.placement.instance,
|
|
141
|
+
enableInternet: false,
|
|
142
|
+
entrypoint: ['node', '-e', BOOT],
|
|
143
|
+
env: { NODE_EXTRA_CA_CERTS: INTERCEPT_CA },
|
|
144
|
+
labels: {
|
|
145
|
+
'substrat-tenant': launch.tenantId,
|
|
146
|
+
'substrat-scope': launch.scopeId,
|
|
147
|
+
'substrat-job': launch.ticket.job,
|
|
148
|
+
'substrat-run': launch.ticket.runId,
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
await this.ctx.storage.setAlarm(Date.now() + launch.placement.passDeadlineMs);
|
|
152
|
+
}
|
|
153
|
+
/** The pass committed or failed: stop its container once the last reply has landed. */
|
|
154
|
+
async ended() {
|
|
155
|
+
await this.ctx.storage.put('ended', true);
|
|
156
|
+
await this.ctx.storage.setAlarm(Date.now() + ENDED_GRACE_MS);
|
|
157
|
+
}
|
|
158
|
+
async alarm() {
|
|
159
|
+
const launch = await this.ctx.storage.get('launch');
|
|
160
|
+
const ended = (await this.ctx.storage.get('ended')) === true;
|
|
161
|
+
if (launch && !ended) {
|
|
162
|
+
// The deadline: fail the pass on its record. A pass that lost its lease since answers 409, which
|
|
163
|
+
// changes nothing, and the run's next pass is already some other box's business.
|
|
164
|
+
const deadline = `the pass ran past its ${Math.round(launch.placement.passDeadlineMs / 60_000)}-minute deadline`;
|
|
165
|
+
await hostOf(this.env)
|
|
166
|
+
.serveContainerPass(launch.tenantId, launch.scopeId, launch.ticket, doorRequest('/fail', { error: deadline }))
|
|
167
|
+
.catch((err) => console.error(JSON.stringify({ substrat: 'container-pass', runId: launch.ticket.runId, deadline: String(err) })));
|
|
168
|
+
}
|
|
169
|
+
const c = this.ctx.container;
|
|
170
|
+
if (c?.running)
|
|
171
|
+
await c.destroy(ended ? 'the pass ended' : 'the pass ran past its deadline').catch(() => undefined);
|
|
172
|
+
await this.ctx.storage.deleteAll();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
export class SubstratPassDoor extends WorkerEntrypoint {
|
|
176
|
+
async fetch(request) {
|
|
177
|
+
const { tenantId, scopeId, ticket } = this.ctx.props;
|
|
178
|
+
return hostOf(this.env).serveContainerPass(tenantId, scopeId, ticket, request, {
|
|
179
|
+
bundle: async () => {
|
|
180
|
+
const job = passJobs()[ticket.job];
|
|
181
|
+
if (!job)
|
|
182
|
+
throw new Error(`this deployment carries no code for the job '${ticket.job}'`);
|
|
183
|
+
return job.source;
|
|
184
|
+
},
|
|
185
|
+
ended: async () => {
|
|
186
|
+
await boxOf(this.env, ticket.runId).ended();
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* D-86: every request the container makes that is not its door. A job with no `publicRead` is told so;
|
|
193
|
+
* one with it reads public addresses under the rule. Redirects are handed back to the container, so
|
|
194
|
+
* each hop comes through here and is judged on its own. Every verdict is one `egress` line, the
|
|
195
|
+
* request record Workers Logs keeps for the tenant.
|
|
196
|
+
*/
|
|
197
|
+
export class SubstratPassEgress extends WorkerEntrypoint {
|
|
198
|
+
async fetch(request) {
|
|
199
|
+
const props = this.ctx.props;
|
|
200
|
+
const url = new URL(request.url);
|
|
201
|
+
const line = (verdict, extra = {}) => console.log(JSON.stringify({
|
|
202
|
+
substrat: 'egress',
|
|
203
|
+
verdict,
|
|
204
|
+
tenant: props.tenantId,
|
|
205
|
+
scope: props.scopeId,
|
|
206
|
+
job: props.ticket.job,
|
|
207
|
+
run: props.ticket.runId,
|
|
208
|
+
host: url.hostname,
|
|
209
|
+
method: request.method,
|
|
210
|
+
...extra,
|
|
211
|
+
}));
|
|
212
|
+
const refuse = (status, detail) => {
|
|
213
|
+
line('refused', { detail });
|
|
214
|
+
return Response.json({ type: 'about:blank', title: 'public read refused', status, detail }, { status, headers: { 'content-type': 'application/problem+json' } });
|
|
215
|
+
};
|
|
216
|
+
if (props.maxBytes === null) {
|
|
217
|
+
return refuse(403, `the job '${props.ticket.job}' declares no public read, so its container reaches nothing but its door. Declare publicRead on the job in package.json substrat.jobs (D-86)`);
|
|
218
|
+
}
|
|
219
|
+
const domains = String(this.env[PLATFORM_DOMAINS_VAR] ?? '').split(',');
|
|
220
|
+
const why = publicReadRefusal({ method: request.method, url: request.url, hasBody: request.body !== null }, domains);
|
|
221
|
+
if (why)
|
|
222
|
+
return refuse(403, why);
|
|
223
|
+
const headers = new Headers(request.headers);
|
|
224
|
+
for (const h of PUBLIC_READ_DROPPED_REQUEST_HEADERS)
|
|
225
|
+
headers.delete(h);
|
|
226
|
+
let upstream;
|
|
227
|
+
try {
|
|
228
|
+
upstream = await fetch(url, { method: request.method, headers, redirect: 'manual', signal: AbortSignal.timeout(PUBLIC_READ_TIMEOUT_MS) });
|
|
229
|
+
}
|
|
230
|
+
catch (err) {
|
|
231
|
+
line('failed', { detail: String(err) });
|
|
232
|
+
return Response.json({ type: 'about:blank', title: 'public read failed', status: 502, detail: String(err) }, { status: 502, headers: { 'content-type': 'application/problem+json' } });
|
|
233
|
+
}
|
|
234
|
+
const out = new Headers(upstream.headers);
|
|
235
|
+
for (const h of PUBLIC_READ_DROPPED_RESPONSE_HEADERS)
|
|
236
|
+
out.delete(h);
|
|
237
|
+
const declared = Number(upstream.headers.get('content-length') ?? NaN);
|
|
238
|
+
if (Number.isFinite(declared) && declared > props.maxBytes) {
|
|
239
|
+
await upstream.body?.cancel();
|
|
240
|
+
return refuse(413, `'${url.hostname}' answered with ${declared} bytes, past the ${props.maxBytes} this job's public read may carry`);
|
|
241
|
+
}
|
|
242
|
+
if (!upstream.body) {
|
|
243
|
+
line('public-read', { status: upstream.status, bytes: 0 });
|
|
244
|
+
return new Response(null, { status: upstream.status, headers: out });
|
|
245
|
+
}
|
|
246
|
+
let bytes = 0;
|
|
247
|
+
const max = props.maxBytes;
|
|
248
|
+
const capped = upstream.body.pipeThrough(new TransformStream({
|
|
249
|
+
transform(chunk, controller) {
|
|
250
|
+
bytes += chunk.byteLength;
|
|
251
|
+
if (bytes > max) {
|
|
252
|
+
line('capped', { status: upstream.status, bytes });
|
|
253
|
+
controller.error(new Error(`the public read passed its ${max}-byte maximum`));
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
controller.enqueue(chunk);
|
|
257
|
+
},
|
|
258
|
+
flush() {
|
|
259
|
+
line('public-read', { status: upstream.status, bytes });
|
|
260
|
+
},
|
|
261
|
+
}));
|
|
262
|
+
return new Response(capped, { status: upstream.status, headers: out });
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
//# sourceMappingURL=supplied-pass-box.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"supplied-pass-box.js","sourceRoot":"","sources":["../src/supplied-pass-box.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,GAAG,IAAI,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACvF,OAAO,EACL,mCAAmC,E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69 (no scheduled walk over tenants), the safety net's first half (#3540): which tenants' due-work
|
|
4
|
+
* objects hold work that should have run long ago, read without visiting every tenant.
|
|
5
|
+
*
|
|
6
|
+
* Each tenant's due-work object (`tenant-due-work-do.ts`) keeps its rows and one alarm. When its
|
|
7
|
+
* deployment gives it an index, it notes here, after every arm and every clear, the earliest time any
|
|
8
|
+
* of its rows is due (`null` once it holds none). This object keeps ONE row per tenant, ordered by
|
|
9
|
+
* that time, so the tenants whose earliest row is more than an hour gone (K-69's overdue bound) are
|
|
10
|
+
* the head of one index range and are read a bounded page at a time: `overdue`. `lag` says how far
|
|
11
|
+
* behind the head is. Both are reads: nothing here wakes, arms or runs anything. The nudger that acts
|
|
12
|
+
* on the selection is a later step, and until it exists the `*\/15` pass's handoff stays the check.
|
|
13
|
+
*
|
|
14
|
+
* A note is a hint, as a due row is: an object's alarm still decides when its rows run. So:
|
|
15
|
+
* - a note carries a stamp, and one older than the row's is dropped (`stale`), so two notes that
|
|
16
|
+
* arrive out of order leave the later one standing. A clear keeps its stamp on the row, so a late
|
|
17
|
+
* arm noted before it cannot bring the tenant back;
|
|
18
|
+
* - an object reset loses its rows, not the clock its stamps are read from, so its next note is
|
|
19
|
+
* newer than any it sent before and replaces what the index held;
|
|
20
|
+
* - a lost note leaves the index behind its object. The selection may then name a tenant with nothing
|
|
21
|
+
* overdue (its object answers that when asked), or miss one, which the pass's handoff still covers.
|
|
22
|
+
*
|
|
23
|
+
* One object per environment: `ns.get(ns.idFromName(TENANT_DUE_INDEX_NAME))`.
|
|
24
|
+
*/
|
|
25
|
+
/** The one index object's name. */
|
|
26
|
+
export declare const TENANT_DUE_INDEX_NAME = "tenant-due-index";
|
|
27
|
+
/** How far past its time a tenant's earliest row may be before the selection calls it overdue: K-69's hour. */
|
|
28
|
+
export declare const TENANT_DUE_INDEX_OVERDUE_MS: number;
|
|
29
|
+
/** The most tenants one `overdue` page returns, and the most one `lag` counts. */
|
|
30
|
+
export declare const TENANT_DUE_INDEX_PAGE = 100;
|
|
31
|
+
export declare const TENANT_DUE_INDEX_LAG_CAP = 1000;
|
|
32
|
+
/** What a note did: `kept` replaced the row (or wrote the first), `stale` was older than it and was dropped. */
|
|
33
|
+
export type TenantDueNote = 'kept' | 'stale';
|
|
34
|
+
export interface TenantDueOverdueQuery {
|
|
35
|
+
/** Epoch ms the selection is judged at. */
|
|
36
|
+
now: number;
|
|
37
|
+
/** Default {@link TENANT_DUE_INDEX_OVERDUE_MS}. */
|
|
38
|
+
overdueAfterMs?: number;
|
|
39
|
+
/** The previous page's `next`. */
|
|
40
|
+
after?: string;
|
|
41
|
+
/** Default and ceiling {@link TENANT_DUE_INDEX_PAGE}. */
|
|
42
|
+
limit?: number;
|
|
43
|
+
}
|
|
44
|
+
export interface TenantDueOverdue {
|
|
45
|
+
tenantId: string;
|
|
46
|
+
/** Epoch ms of the tenant's earliest due row, as last noted. */
|
|
47
|
+
earliestAt: number;
|
|
48
|
+
/** `now - earliestAt`. */
|
|
49
|
+
lagMs: number;
|
|
50
|
+
}
|
|
51
|
+
export interface TenantDueOverduePage {
|
|
52
|
+
tenants: TenantDueOverdue[];
|
|
53
|
+
/** Hand back as `after` for the next page; null when this page was the last. */
|
|
54
|
+
next: string | null;
|
|
55
|
+
}
|
|
56
|
+
export interface TenantDueLag {
|
|
57
|
+
/** Tenants overdue, counted up to `cap`. */
|
|
58
|
+
overdue: number;
|
|
59
|
+
/** True when there were more than `cap`. */
|
|
60
|
+
capped: boolean;
|
|
61
|
+
/** How long the oldest overdue tenant's earliest row has waited; 0 when none is overdue. */
|
|
62
|
+
maxLagMs: number;
|
|
63
|
+
oldestTenantId: string | null;
|
|
64
|
+
}
|
|
65
|
+
/** The RPC surface a `defineTenantDueIndexDO` class exposes over its stub. */
|
|
66
|
+
export interface TenantDueIndexDo {
|
|
67
|
+
note(tenantId: string, stamp: number, earliestAt: number | null): Promise<TenantDueNote>;
|
|
68
|
+
overdue(query: TenantDueOverdueQuery): Promise<TenantDueOverduePage>;
|
|
69
|
+
lag(query: {
|
|
70
|
+
now: number;
|
|
71
|
+
overdueAfterMs?: number;
|
|
72
|
+
cap?: number;
|
|
73
|
+
}): Promise<TenantDueLag>;
|
|
74
|
+
}
|
|
75
|
+
export declare function defineTenantDueIndexDO<Env>(): new (ctx: DurableObjectState, env: Env) => DurableObject<Env> & TenantDueIndexDo;
|
|
76
|
+
//# sourceMappingURL=tenant-due-index-do.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenant-due-index-do.d.ts","sourceRoot":"","sources":["../src/tenant-due-index-do.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,mCAAmC;AACnC,eAAO,MAAM,qBAAqB,qBAAqB,CAAC;AAExD,+GAA+G;AAC/G,eAAO,MAAM,2BAA2B,QAAc,CAAC;AAEvD,kFAAkF;AAClF,eAAO,MAAM,qBAAqB,MAAM,CAAC;AACzC,eAAO,MAAM,wBAAwB,OAAQ,CAAC;AAE9C,gHAAgH;AAChH,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,OAAO,CAAC;AAE7C,MAAM,WAAW,qBAAqB;IACpC,2CAA2C;IAC3C,GAAG,EAAE,MAAM,CAAC;IACZ,mDAAmD;IACnD,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,kCAAkC;IAClC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,yDAAyD;IACzD,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB,gEAAgE;IAChE,UAAU,EAAE,MAAM,CAAC;IACnB,0BAA0B;IAC1B,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,oBAAoB;IACnC,OAAO,EAAE,gBAAgB,EAAE,CAAC;IAC5B,gFAAgF;IAChF,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CACrB;AAED,MAAM,WAAW,YAAY;IAC3B,4CAA4C;IAC5C,OAAO,EAAE,MAAM,CAAC;IAChB,4CAA4C;IAC5C,MAAM,EAAE,OAAO,CAAC;IAChB,4FAA4F;IAC5F,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;CAC/B;AAED,8EAA8E;AAC9E,MAAM,WAAW,gBAAgB;IAC/B,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IACzF,OAAO,CAAC,KAAK,EAAE,qBAAqB,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC;IACrE,GAAG,CAAC,KAAK,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,cAAc,CAAC,EAAE,MAAM,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;CAC3F;AAUD,wBAAgB,sBAAsB,CAAC,GAAG,KAAK,KAAK,GAAG,EAAE,kBAAkB,EAAE,GAAG,EAAE,GAAG,KAAK,aAAa,CAAC,GAAG,CAAC,GAAG,gBAAgB,CAiE9H"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { DurableObject } from 'cloudflare:workers';
|
|
2
|
+
/**
|
|
3
|
+
* K-69 (no scheduled walk over tenants), the safety net's first half (#3540): which tenants' due-work
|
|
4
|
+
* objects hold work that should have run long ago, read without visiting every tenant.
|
|
5
|
+
*
|
|
6
|
+
* Each tenant's due-work object (`tenant-due-work-do.ts`) keeps its rows and one alarm. When its
|
|
7
|
+
* deployment gives it an index, it notes here, after every arm and every clear, the earliest time any
|
|
8
|
+
* of its rows is due (`null` once it holds none). This object keeps ONE row per tenant, ordered by
|
|
9
|
+
* that time, so the tenants whose earliest row is more than an hour gone (K-69's overdue bound) are
|
|
10
|
+
* the head of one index range and are read a bounded page at a time: `overdue`. `lag` says how far
|
|
11
|
+
* behind the head is. Both are reads: nothing here wakes, arms or runs anything. The nudger that acts
|
|
12
|
+
* on the selection is a later step, and until it exists the `*\/15` pass's handoff stays the check.
|
|
13
|
+
*
|
|
14
|
+
* A note is a hint, as a due row is: an object's alarm still decides when its rows run. So:
|
|
15
|
+
* - a note carries a stamp, and one older than the row's is dropped (`stale`), so two notes that
|
|
16
|
+
* arrive out of order leave the later one standing. A clear keeps its stamp on the row, so a late
|
|
17
|
+
* arm noted before it cannot bring the tenant back;
|
|
18
|
+
* - an object reset loses its rows, not the clock its stamps are read from, so its next note is
|
|
19
|
+
* newer than any it sent before and replaces what the index held;
|
|
20
|
+
* - a lost note leaves the index behind its object. The selection may then name a tenant with nothing
|
|
21
|
+
* overdue (its object answers that when asked), or miss one, which the pass's handoff still covers.
|
|
22
|
+
*
|
|
23
|
+
* One object per environment: `ns.get(ns.idFromName(TENANT_DUE_INDEX_NAME))`.
|
|
24
|
+
*/
|
|
25
|
+
/** The one index object's name. */
|
|
26
|
+
export const TENANT_DUE_INDEX_NAME = 'tenant-due-index';
|
|
27
|
+
/** How far past its time a tenant's earliest row may be before the selection calls it overdue: K-69's hour. */
|
|
28
|
+
export const TENANT_DUE_INDEX_OVERDUE_MS = 60 * 60_000;
|
|
29
|
+
/** The most tenants one `overdue` page returns, and the most one `lag` counts. */
|
|
30
|
+
export const TENANT_DUE_INDEX_PAGE = 100;
|
|
31
|
+
export const TENANT_DUE_INDEX_LAG_CAP = 1_000;
|
|
32
|
+
const cursorOf = (earliestAt, tenantId) => `${earliestAt}:${tenantId}`;
|
|
33
|
+
function parseCursor(after) {
|
|
34
|
+
const at = after.indexOf(':');
|
|
35
|
+
const earliestAt = Number(after.slice(0, at));
|
|
36
|
+
if (at <= 0 || !Number.isSafeInteger(earliestAt))
|
|
37
|
+
throw new Error(`not a tenant due index cursor: ${after}`);
|
|
38
|
+
return [earliestAt, after.slice(at + 1)];
|
|
39
|
+
}
|
|
40
|
+
export function defineTenantDueIndexDO() {
|
|
41
|
+
return class TenantDueIndexDO extends DurableObject {
|
|
42
|
+
constructor(ctx, env) {
|
|
43
|
+
super(ctx, env);
|
|
44
|
+
// `earliest_at` null is a cleared tenant: its row stays only to hold the stamp a late note is judged by.
|
|
45
|
+
ctx.storage.sql.exec(`CREATE TABLE IF NOT EXISTS tenant_due (tenant_id TEXT PRIMARY KEY, earliest_at INTEGER, stamp INTEGER NOT NULL)`);
|
|
46
|
+
ctx.storage.sql.exec(`CREATE INDEX IF NOT EXISTS tenant_due_earliest ON tenant_due (earliest_at, tenant_id) WHERE earliest_at IS NOT NULL`);
|
|
47
|
+
}
|
|
48
|
+
async note(tenantId, stamp, earliestAt) {
|
|
49
|
+
if (!tenantId || !Number.isSafeInteger(stamp) || (earliestAt !== null && !Number.isSafeInteger(earliestAt))) {
|
|
50
|
+
throw new Error(`not a tenant due note: ${JSON.stringify({ tenantId, stamp, earliestAt })}`);
|
|
51
|
+
}
|
|
52
|
+
// One statement, so a note is judged and written at once: the newer stamp wins, ties to the later arrival.
|
|
53
|
+
const changed = this.ctx.storage.sql.exec(`INSERT INTO tenant_due (tenant_id, earliest_at, stamp) VALUES (?, ?, ?)
|
|
54
|
+
ON CONFLICT (tenant_id) DO UPDATE SET earliest_at = excluded.earliest_at, stamp = excluded.stamp
|
|
55
|
+
WHERE excluded.stamp >= tenant_due.stamp`, tenantId, earliestAt, stamp).rowsWritten;
|
|
56
|
+
return changed > 0 ? 'kept' : 'stale';
|
|
57
|
+
}
|
|
58
|
+
async overdue(query) {
|
|
59
|
+
const cutoff = query.now - (query.overdueAfterMs ?? TENANT_DUE_INDEX_OVERDUE_MS);
|
|
60
|
+
const limit = Math.max(1, Math.min(query.limit ?? TENANT_DUE_INDEX_PAGE, TENANT_DUE_INDEX_PAGE));
|
|
61
|
+
const [afterAt, afterTenant] = query.after === undefined ? [Number.MIN_SAFE_INTEGER, ''] : parseCursor(query.after);
|
|
62
|
+
// The index range, in (earliest_at, tenant_id) order, after the cursor: one row per tenant, so a
|
|
63
|
+
// page never names a tenant twice. One more than the page says whether there is a next.
|
|
64
|
+
const rows = this.ctx.storage.sql.exec(`SELECT tenant_id, earliest_at FROM tenant_due
|
|
65
|
+
WHERE earliest_at IS NOT NULL AND earliest_at <= ? AND (earliest_at, tenant_id) > (?, ?)
|
|
66
|
+
ORDER BY earliest_at, tenant_id LIMIT ?`, cutoff, afterAt, afterTenant, limit + 1).toArray();
|
|
67
|
+
const page = rows.slice(0, limit);
|
|
68
|
+
const last = page.at(-1);
|
|
69
|
+
return {
|
|
70
|
+
tenants: page.map((r) => ({ tenantId: r.tenant_id, earliestAt: r.earliest_at, lagMs: query.now - r.earliest_at })),
|
|
71
|
+
next: rows.length > limit && last ? cursorOf(last.earliest_at, last.tenant_id) : null,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
async lag(query) {
|
|
75
|
+
const cutoff = query.now - (query.overdueAfterMs ?? TENANT_DUE_INDEX_OVERDUE_MS);
|
|
76
|
+
const cap = Math.max(1, Math.min(query.cap ?? TENANT_DUE_INDEX_LAG_CAP, TENANT_DUE_INDEX_LAG_CAP));
|
|
77
|
+
const rows = this.ctx.storage.sql.exec(`SELECT tenant_id, earliest_at FROM tenant_due
|
|
78
|
+
WHERE earliest_at IS NOT NULL AND earliest_at <= ?
|
|
79
|
+
ORDER BY earliest_at, tenant_id LIMIT ?`, cutoff, cap + 1).toArray();
|
|
80
|
+
const oldest = rows[0];
|
|
81
|
+
return {
|
|
82
|
+
overdue: Math.min(rows.length, cap),
|
|
83
|
+
capped: rows.length > cap,
|
|
84
|
+
maxLagMs: oldest ? query.now - oldest.earliest_at : 0,
|
|
85
|
+
oldestTenantId: oldest?.tenant_id ?? null,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=tenant-due-index-do.js.map
|
|
@@ -0,0 +1 @@
|
|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GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;YACvB,OAAO;gBACL,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC;gBACnC,MAAM,EAAE,IAAI,CAAC,MAAM,GAAG,GAAG;gBACzB,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;gBACrD,cAAc,EAAE,MAAM,EAAE,SAAS,IAAI,IAAI;aAC1C,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { type TenantDueIndexDo } from './tenant-due-index-do.js';
|
|
2
|
+
import type { TenantDueWorkDo } from './tenant-due-work-do.js';
|
|
3
|
+
/**
|
|
4
|
+
* K-69's safety net, second half (#3540): act on the tenant due index's selection
|
|
5
|
+
* (`tenant-due-index-do.ts`, `overdue`) by asking each selected tenant's due-work object to re-check
|
|
6
|
+
* itself (`tenant-due-work-do.ts`, `recheck`). Its work is the number of tenants whose alarm failed,
|
|
7
|
+
* never the number of tenants.
|
|
8
|
+
*
|
|
9
|
+
* Bound nowhere: no deployment calls it, and no cron runs it. When it runs and how overdue a row must
|
|
10
|
+
* be before it is kicked are the owner's choices K-69 leaves open, so both are the caller's.
|
|
11
|
+
*
|
|
12
|
+
* - **Bounded.** One run reads the selection a page at a time, oldest first, and stops at `maxTenants`
|
|
13
|
+
* (`truncated`); the next run starts from the oldest again.
|
|
14
|
+
* - **Each tenant once per run.** A re-check re-notes the tenant, and a note that moves its row later
|
|
15
|
+
* while it stays overdue would put it ahead of the cursor; the run counts and skips it (`duplicates`).
|
|
16
|
+
* - **Fresh, never trusted.** The selection is a hint. The object decides from its own rows and alarm
|
|
17
|
+
* whether anything needs arming, refuses a tenant it does not hold, drops a kind its deployment no
|
|
18
|
+
* longer owns, and notes what it found, which removes a stale index row.
|
|
19
|
+
* - **One tenant's failure is its own.** A re-check that throws is counted and named; the run goes on.
|
|
20
|
+
*
|
|
21
|
+
* What it cannot see: a tenant whose note never reached the index. The object notes AFTER its alarm is
|
|
22
|
+
* armed and logs a note that fails, so such a tenant still runs from its alarm, and is invisible only if
|
|
23
|
+
* that alarm is later lost too. The `*\/15` pass's handoff check is what covers that today: each
|
|
24
|
+
* `ensure` it makes notes the tenant again. K-69 orders the other way round (the index row first, and
|
|
25
|
+
* no alarm when it fails); until one of the two holds, the handoff check is the missed-note fallback
|
|
26
|
+
* and is not retired by this.
|
|
27
|
+
*/
|
|
28
|
+
export interface TenantDueNudgeOptions {
|
|
29
|
+
/** The index whose selection is read. */
|
|
30
|
+
index: Pick<TenantDueIndexDo, 'overdue'>;
|
|
31
|
+
/** The tenant's due-work object: `ns.get(ns.idFromName(tenantDueWorkName(tenantId)))`. */
|
|
32
|
+
objectOf(tenantId: string): Pick<TenantDueWorkDo, 'recheck'>;
|
|
33
|
+
/** Epoch ms the selection is judged at. */
|
|
34
|
+
now: number;
|
|
35
|
+
/** How overdue a tenant's earliest row must be to be selected; the index's hour by default. An owner choice. */
|
|
36
|
+
overdueAfterMs?: number;
|
|
37
|
+
/** Tenants per index page; capped by the index at {@link TENANT_DUE_INDEX_PAGE}. */
|
|
38
|
+
pageLimit?: number;
|
|
39
|
+
/** The most tenants one run re-checks. Default {@link TENANT_DUE_NUDGE_MAX_TENANTS}. */
|
|
40
|
+
maxTenants?: number;
|
|
41
|
+
/** Re-checks in flight at once. Default 8. */
|
|
42
|
+
concurrency?: number;
|
|
43
|
+
onError?(err: unknown, tenantId: string): void;
|
|
44
|
+
}
|
|
45
|
+
export interface TenantDueNudgeReport {
|
|
46
|
+
/** Index pages read. */
|
|
47
|
+
pages: number;
|
|
48
|
+
/** Distinct tenants re-checked. */
|
|
49
|
+
selected: number;
|
|
50
|
+
/** Selected again later in the same run, after their own re-check moved their row: skipped. */
|
|
51
|
+
duplicates: number;
|
|
52
|
+
/** True when the run stopped at `maxTenants` with more selected. */
|
|
53
|
+
truncated: boolean;
|
|
54
|
+
armed: number;
|
|
55
|
+
onTime: number;
|
|
56
|
+
cleared: number;
|
|
57
|
+
/** Rows of an unowned kind the re-checks dropped. */
|
|
58
|
+
dropped: number;
|
|
59
|
+
failed: number;
|
|
60
|
+
/** The first few tenants whose re-check failed. */
|
|
61
|
+
failedTenantIds: string[];
|
|
62
|
+
}
|
|
63
|
+
/** The most tenants one nudge run re-checks by default. */
|
|
64
|
+
export declare const TENANT_DUE_NUDGE_MAX_TENANTS = 500;
|
|
65
|
+
export declare function runTenantDueNudge(options: TenantDueNudgeOptions): Promise<TenantDueNudgeReport>;
|
|
66
|
+
//# sourceMappingURL=tenant-due-nudge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenant-due-nudge.d.ts","sourceRoot":"","sources":["../src/tenant-due-nudge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,KAAK,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AACxF,OAAO,KAAK,EAAoB,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAEjF;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,MAAM,WAAW,qBAAqB;IACpC,yCAAyC;IACzC,KAAK,EAAE,IAAI,CAAC,gBAAgB,EAAE,SAAS,CAAC,CAAC;IACzC,0FAA0F;IAC1F,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC,eAAe,EAAE,SAAS,CAAC,CAAC;IAC7D,2CAA2C;IAC3C,GAAG,EAAE,MAAM,CAAC;IACZ,gHAAgH;IAChH,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,oFAAoF;IACpF,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,wFAAwF;IACxF,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,8CAA8C;IAC9C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,CAAC,GAAG,EAAE,OAAO,EAAE,Q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