gencow 0.1.299 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/gencow.mjs +0 -6
- package/core/package.json +1 -1
- package/docs/packaging.md +14 -4
- package/lib/cli-artifact-guard.mjs +53 -3
- package/lib/cli-dev-runtime.mjs +1 -0
- package/lib/cli-project-runtime.mjs +26 -14
- package/lib/codegen-command.mjs +16 -0
- package/lib/coding-guardrails.mjs +2 -0
- package/lib/dashboard-artifact.mjs +65 -0
- package/lib/dashboard-assets.mjs +20 -0
- package/lib/db-command.mjs +121 -80
- package/lib/db-migration-command-steps.mjs +19 -16
- package/lib/db-push-command.mjs +1 -1
- package/lib/deploy-codegen-preflight.mjs +1 -0
- package/lib/dev-cloud-command.mjs +1 -1
- package/lib/dev-local-command.mjs +19 -56
- package/lib/document-local-converter.mjs +11 -0
- package/lib/local-dashboard-plan.mjs +60 -0
- package/lib/local-database-commands.mjs +127 -0
- package/lib/local-development-migrations.mjs +157 -0
- package/lib/local-drizzle-database.config.mjs +10 -0
- package/lib/local-runtime-features.mjs +12 -0
- package/lib/rag-selection.mjs +7 -0
- package/lib/runtime-local-worker.mjs +116 -0
- package/lib/runtime-mode.mjs +3 -0
- package/lib/standalone-codegen-adapter.mjs +91 -0
- package/lib/ts-codegen-import-extension.mjs +228 -19
- package/package.json +16 -9
- package/runtime/config.mjs +218 -2
- package/runtime/engine/chunk-2HA4CUMS.mjs +90 -0
- package/runtime/engine/chunk-2JLO3KMM.mjs +305 -0
- package/runtime/engine/chunk-2SR2M7SR.mjs +1045 -0
- package/runtime/engine/chunk-3D7MHEUC.mjs +253 -0
- package/runtime/engine/chunk-3R7COBWG.mjs +309 -0
- package/runtime/engine/chunk-3ZKA54UW.mjs +1560 -0
- package/runtime/engine/chunk-45I4YR2J.mjs +9 -0
- package/runtime/engine/chunk-4MLRXO7F.mjs +74 -0
- package/runtime/engine/chunk-5575APCJ.mjs +215 -0
- package/runtime/engine/chunk-5GUJZQF3.mjs +114 -0
- package/runtime/engine/chunk-5MXY3T75.mjs +428 -0
- package/runtime/engine/chunk-5TACQ2US.mjs +44 -0
- package/runtime/engine/chunk-66G4YEDT.mjs +3104 -0
- package/runtime/engine/chunk-67YO53WW.mjs +1426 -0
- package/runtime/engine/chunk-6GQRM7XY.mjs +87 -0
- package/runtime/engine/chunk-6JO72SPP.mjs +68 -0
- package/runtime/engine/chunk-6W653LXI.mjs +13 -0
- package/runtime/engine/chunk-7ITSMI3S.mjs +69 -0
- package/runtime/engine/chunk-7KUJ2H2S.mjs +128 -0
- package/runtime/engine/chunk-A5BMQ5IC.mjs +45 -0
- package/runtime/engine/chunk-ADF3UR6L.mjs +428 -0
- package/runtime/engine/chunk-AEBW7XZJ.mjs +4510 -0
- package/runtime/engine/chunk-AGPQ5XIV.mjs +156 -0
- package/runtime/engine/chunk-CPU4OZSX.mjs +753 -0
- package/runtime/engine/chunk-CSWPPWBL.mjs +50 -0
- package/runtime/engine/chunk-DPWXSMWO.mjs +53 -0
- package/runtime/engine/chunk-EAIELFKU.mjs +151 -0
- package/runtime/engine/chunk-EDU5CBKT.mjs +10 -0
- package/runtime/engine/chunk-EQCTAET7.mjs +173 -0
- package/runtime/engine/chunk-EQCXDF2Z.mjs +70 -0
- package/runtime/engine/chunk-F3G4UGV4.mjs +330 -0
- package/runtime/engine/chunk-F62NUNOO.mjs +26 -0
- package/runtime/engine/chunk-FKPNIFRP.mjs +2492 -0
- package/runtime/engine/chunk-FM23RGVI.mjs +81 -0
- package/runtime/engine/chunk-FW5AK4YP.mjs +1041 -0
- package/runtime/engine/chunk-GDWYAGWV.mjs +23 -0
- package/runtime/engine/chunk-GO5PZUYW.mjs +53 -0
- package/runtime/engine/chunk-GOLP7OSG.mjs +340 -0
- package/runtime/engine/chunk-GSLM44EF.mjs +242 -0
- package/runtime/engine/chunk-GYAJ7OX2.mjs +125 -0
- package/runtime/engine/chunk-I3GWR43K.mjs +14 -0
- package/runtime/engine/chunk-IFMOTJNU.mjs +106 -0
- package/runtime/engine/chunk-IKQKBE2G.mjs +128 -0
- package/runtime/engine/chunk-ITH2NEKM.mjs +282 -0
- package/runtime/engine/chunk-J6SDRCG4.mjs +96 -0
- package/runtime/engine/chunk-JODRNM3S.mjs +29596 -0
- package/runtime/engine/chunk-JUDKX53X.mjs +137 -0
- package/runtime/engine/chunk-K2YZVPJ4.mjs +827 -0
- package/runtime/engine/chunk-KEZJUXEE.mjs +151 -0
- package/runtime/engine/chunk-KVLTJFCH.mjs +234 -0
- package/runtime/engine/chunk-KX2PRQPS.mjs +126 -0
- package/runtime/engine/chunk-LEW5ZMLR.mjs +76 -0
- package/runtime/engine/chunk-LINVSJEM.mjs +175 -0
- package/runtime/engine/chunk-M7K2ICSM.mjs +11 -0
- package/runtime/engine/chunk-MZXDXSDC.mjs +292 -0
- package/runtime/engine/chunk-N2TA2IK2.mjs +678 -0
- package/runtime/engine/chunk-N4LTCKJP.mjs +695 -0
- package/runtime/engine/chunk-N6GCLOUX.mjs +13 -0
- package/runtime/engine/chunk-NAG2OXS5.mjs +37 -0
- package/runtime/engine/chunk-NNZ2QJUG.mjs +381 -0
- package/runtime/engine/chunk-NZPPLDNW.mjs +15 -0
- package/runtime/engine/chunk-ODWM57W6.mjs +138 -0
- package/runtime/engine/chunk-P5IIXAIE.mjs +23 -0
- package/runtime/engine/chunk-PBOXB2BV.mjs +6 -0
- package/runtime/engine/chunk-PMOJLN2P.mjs +8 -0
- package/runtime/engine/chunk-PZFMKFHO.mjs +180 -0
- package/runtime/engine/chunk-QL2TQX2N.mjs +114 -0
- package/runtime/engine/chunk-RITT2RSJ.mjs +258 -0
- package/runtime/engine/chunk-RODBVJ5M.mjs +135 -0
- package/runtime/engine/chunk-RW65MPVY.mjs +261 -0
- package/runtime/engine/chunk-S67UYPB6.mjs +25 -0
- package/runtime/engine/chunk-SVJCOUI3.mjs +47 -0
- package/runtime/engine/chunk-TCN3ZASV.mjs +957 -0
- package/runtime/engine/chunk-U2NRG6KC.mjs +32 -0
- package/runtime/engine/chunk-UM3F7HUV.mjs +432 -0
- package/runtime/engine/chunk-VWU7JJ3P.mjs +146 -0
- package/runtime/engine/chunk-WDTKPPMU.mjs +310 -0
- package/runtime/engine/chunk-WJOO4XTB.mjs +31 -0
- package/runtime/engine/chunk-XNFVL2OY.mjs +63 -0
- package/runtime/engine/chunk-XPWJXP2J.mjs +58 -0
- package/runtime/engine/chunk-Y3HSBJ3I.mjs +1132 -0
- package/runtime/engine/chunk-YMH4RYSX.mjs +317 -0
- package/runtime/engine/chunk-YNOMPSGS.mjs +8 -0
- package/runtime/engine/chunk-YRBAYTEZ.mjs +251 -0
- package/runtime/engine/chunk-YRHM272W.mjs +528 -0
- package/runtime/engine/chunk-ZQUZAGIQ.mjs +149 -0
- package/runtime/engine/chunk-ZXMM7QFP.mjs +176 -0
- package/runtime/engine/chunk-ZZJSIPV2.mjs +262 -0
- package/runtime/engine/composition.mjs +325 -0
- package/runtime/engine/document-local.mjs +504 -0
- package/runtime/engine/graph.json +4134 -0
- package/runtime/engine/index.mjs +265 -0
- package/runtime/engine/local-postgres-support.mjs +69 -0
- package/runtime/engine/local.mjs +3275 -0
- package/runtime/engine/pglite-directory-lock.mjs +8 -0
- package/runtime/engine/rag.mjs +11 -0
- package/runtime/engine/search.mjs +18 -0
- package/runtime/framework-migrations/foundation-bootstrap/rag.sql +115 -0
- package/runtime/framework-migrations/foundation-bootstrap/workflow.sql +194 -0
- package/runtime/framework-migrations/journal-bootstrap/migration.sql +26 -0
- package/runtime/framework-migrations/manifest.json +56 -0
- package/runtime/framework-migrations/rag/20261006111509_mushy_warhawk/migration.sql +121 -0
- package/runtime/framework-migrations/rag/20261013234621_rag_v2_lifecycle_markers/migration.sql +6 -0
- package/runtime/framework-migrations/workflow/20261006111508_perpetual_star_brand/migration.sql +31 -0
- package/runtime/runtime-metadata.json +27 -0
- package/runtime/tooling.mjs +10399 -8951
- package/scripts/bundle-runtime-config.mjs +23 -0
- package/scripts/bundle-runtime.mjs +100 -0
- package/scripts/bundle-runtime.test.mjs +33 -0
- package/scripts/bundle-shared.mjs +3 -0
- package/scripts/copy-dashboard.mjs +16 -0
- package/scripts/pre-publish-check.mjs +23 -11
- package/templateFeature/sso/sso-auth-callback.tsx +37 -5
- package/templateFeature/tools/manifest.json +2 -1
- package/templateFeature/tools/tools.ts +3 -3
- package/runtime/server.mjs +0 -14389
- package/scripts/bundle-server.mjs +0 -223
- package/server/config.js +0 -1
- package/server/index.js +0 -1
- package/server/package.json +0 -6
|
@@ -0,0 +1,1045 @@
|
|
|
1
|
+
import {
|
|
2
|
+
registerLocalJobSourceEvidence,
|
|
3
|
+
registerLocalSchedulerInvocationConsumer,
|
|
4
|
+
schedulerIdentifier,
|
|
5
|
+
schedulerInstant,
|
|
6
|
+
schedulerOperationId,
|
|
7
|
+
snapshotOnErrorArgs,
|
|
8
|
+
snapshotRuntimeBinding,
|
|
9
|
+
snapshotSchedulerAdmissionArgs,
|
|
10
|
+
snapshotSchedulerArgs,
|
|
11
|
+
timedOutSchedulerWorkFor
|
|
12
|
+
} from "./chunk-WDTKPPMU.mjs";
|
|
13
|
+
import {
|
|
14
|
+
invalidCronDeclaration,
|
|
15
|
+
schedulerFailure
|
|
16
|
+
} from "./chunk-NAG2OXS5.mjs";
|
|
17
|
+
import {
|
|
18
|
+
runtimeDiagnostic
|
|
19
|
+
} from "./chunk-AEBW7XZJ.mjs";
|
|
20
|
+
|
|
21
|
+
// ../runtime/dist/adapters/scheduler/local/policy.js
|
|
22
|
+
var defaultLocalSchedulerPolicy = Object.freeze({
|
|
23
|
+
maxActive: 32,
|
|
24
|
+
maxPending: 1e4,
|
|
25
|
+
maxCrons: 100,
|
|
26
|
+
maxArgsBytes: 65536
|
|
27
|
+
});
|
|
28
|
+
function snapshotLocalSchedulerPolicy(input = {}) {
|
|
29
|
+
const keys = Object.keys(defaultLocalSchedulerPolicy);
|
|
30
|
+
if (!input || typeof input !== "object" || Array.isArray(input) || Reflect.ownKeys(input).some((key) => typeof key !== "string" || !keys.includes(key)))
|
|
31
|
+
throw invalid();
|
|
32
|
+
const policy = { ...defaultLocalSchedulerPolicy };
|
|
33
|
+
const descriptors = Object.getOwnPropertyDescriptors(input);
|
|
34
|
+
for (const key of keys) {
|
|
35
|
+
const property = descriptors[key];
|
|
36
|
+
if (!property)
|
|
37
|
+
continue;
|
|
38
|
+
if (!("value" in property))
|
|
39
|
+
throw invalid();
|
|
40
|
+
const value = property.value;
|
|
41
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value <= 0 || value > defaultLocalSchedulerPolicy[key])
|
|
42
|
+
throw invalid();
|
|
43
|
+
policy[key] = value;
|
|
44
|
+
}
|
|
45
|
+
return Object.freeze(policy);
|
|
46
|
+
}
|
|
47
|
+
function invalid() {
|
|
48
|
+
return schedulerFailure("RUNTIME_SCHEDULER_INPUT_INVALID", "Use positive integer local policy limits no larger than the documented defaults.", "startup");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// ../runtime/dist/adapters/scheduler/local/resource.js
|
|
52
|
+
import { randomUUID as randomUUID2 } from "node:crypto";
|
|
53
|
+
|
|
54
|
+
// ../runtime/dist/scheduler/outcome-view.js
|
|
55
|
+
function localOutcomeView(outcome) {
|
|
56
|
+
if (outcome.operationIds.length > 64)
|
|
57
|
+
throw schedulerFailure("RUNTIME_SCHEDULER_EXECUTION_FAILED", "Inspect the safe job outcome and accepted child effects before deliberately scheduling new work.");
|
|
58
|
+
const childOperationIds = Object.freeze(outcome.operationIds.map((id) => {
|
|
59
|
+
schedulerOperationId(id);
|
|
60
|
+
return id;
|
|
61
|
+
}));
|
|
62
|
+
const diagnostic = outcome.diagnostic ?? outcome.settlementDiagnostic;
|
|
63
|
+
return Object.freeze({
|
|
64
|
+
delivery: Object.freeze({
|
|
65
|
+
deliveryId: outcome.deliveryId,
|
|
66
|
+
occurrenceId: outcome.occurrenceId,
|
|
67
|
+
attempt: outcome.attempt,
|
|
68
|
+
execution: outcome.execution,
|
|
69
|
+
effects: outcome.effects,
|
|
70
|
+
dataImpact: outcome.dataImpact
|
|
71
|
+
}),
|
|
72
|
+
childOperationIds,
|
|
73
|
+
...diagnostic ? { diagnostic: Object.freeze({ ...diagnostic }) } : {},
|
|
74
|
+
...outcome.diagnostic && outcome.settlementDiagnostic ? { settlementDiagnostic: Object.freeze({ ...outcome.settlementDiagnostic }) } : {}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// ../runtime/dist/scheduler/cron-pattern.js
|
|
79
|
+
function isSupportedCronPattern(pattern) {
|
|
80
|
+
if (typeof pattern !== "string" || pattern.length > 1024)
|
|
81
|
+
return false;
|
|
82
|
+
if (["@yearly", "@annually", "@monthly", "@weekly", "@daily", "@midnight", "@hourly"].includes(pattern.trim().toLowerCase()))
|
|
83
|
+
return true;
|
|
84
|
+
const fields = pattern.trim().split(/\s+/u);
|
|
85
|
+
if (fields.length !== 5 && fields.length !== 6)
|
|
86
|
+
return false;
|
|
87
|
+
const bounds = fields.length === 6 ? [
|
|
88
|
+
[0, 59],
|
|
89
|
+
[0, 59],
|
|
90
|
+
[0, 23],
|
|
91
|
+
[1, 31],
|
|
92
|
+
[1, 12],
|
|
93
|
+
[0, 7]
|
|
94
|
+
] : [
|
|
95
|
+
[0, 59],
|
|
96
|
+
[0, 23],
|
|
97
|
+
[1, 31],
|
|
98
|
+
[1, 12],
|
|
99
|
+
[0, 7]
|
|
100
|
+
];
|
|
101
|
+
const months = [
|
|
102
|
+
"january",
|
|
103
|
+
"february",
|
|
104
|
+
"march",
|
|
105
|
+
"april",
|
|
106
|
+
"may",
|
|
107
|
+
"june",
|
|
108
|
+
"july",
|
|
109
|
+
"august",
|
|
110
|
+
"september",
|
|
111
|
+
"october",
|
|
112
|
+
"november",
|
|
113
|
+
"december"
|
|
114
|
+
];
|
|
115
|
+
const days = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
|
|
116
|
+
return fields.every((field, index) => {
|
|
117
|
+
const [min, max] = bounds[index];
|
|
118
|
+
const names = index === fields.length - 2 ? months : index === fields.length - 1 ? days : [];
|
|
119
|
+
const number = (value) => {
|
|
120
|
+
if (/^\d{1,2}$/u.test(value))
|
|
121
|
+
return Number(value);
|
|
122
|
+
const name = names.findIndex((name2) => name2 === value.toLowerCase() || name2.slice(0, 3) === value.toLowerCase());
|
|
123
|
+
return name < 0 ? NaN : name + (names === months ? 1 : 0);
|
|
124
|
+
};
|
|
125
|
+
return field.split(",").every((part) => {
|
|
126
|
+
if (index === fields.length - 3 && part.toUpperCase() === "L")
|
|
127
|
+
return true;
|
|
128
|
+
if (index === fields.length - 1) {
|
|
129
|
+
const special = /^([a-z]+|[0-7])(?:#([1-5])|L)$/iu.exec(part);
|
|
130
|
+
if (special) {
|
|
131
|
+
const day = number(special[1]);
|
|
132
|
+
return day >= 0 && day <= 7;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const split = part.split("/");
|
|
136
|
+
if (split.length > 2 || split.length === 2 && (!/^\d+$/u.test(split[1]) || Number(split[1]) < 1 || Number(split[1]) > max))
|
|
137
|
+
return false;
|
|
138
|
+
if (split[0] === "*")
|
|
139
|
+
return true;
|
|
140
|
+
const range = split[0].split("-");
|
|
141
|
+
if (range.length > 2)
|
|
142
|
+
return false;
|
|
143
|
+
const start = number(range[0]);
|
|
144
|
+
const end = range.length === 2 ? number(range[1]) : start;
|
|
145
|
+
return start >= min && start <= max && end >= start && end <= max;
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ../runtime/dist/adapters/scheduler/local/job-store.js
|
|
151
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
152
|
+
|
|
153
|
+
// ../runtime/dist/adapters/scheduler/local/history.js
|
|
154
|
+
var LocalSchedulerHistory = class {
|
|
155
|
+
now;
|
|
156
|
+
entries = /* @__PURE__ */ new Map();
|
|
157
|
+
tombstones = /* @__PURE__ */ new Map();
|
|
158
|
+
constructor(now) {
|
|
159
|
+
this.now = now;
|
|
160
|
+
}
|
|
161
|
+
put(key, entry) {
|
|
162
|
+
this.entries.delete(key);
|
|
163
|
+
this.entries.set(key, { entry: Object.freeze(entry), at: this.now() });
|
|
164
|
+
this.prune();
|
|
165
|
+
}
|
|
166
|
+
get(key) {
|
|
167
|
+
this.prune();
|
|
168
|
+
return this.entries.get(key)?.entry;
|
|
169
|
+
}
|
|
170
|
+
known(key) {
|
|
171
|
+
this.prune();
|
|
172
|
+
return this.entries.has(key) || this.tombstones.has(key);
|
|
173
|
+
}
|
|
174
|
+
missing(key) {
|
|
175
|
+
this.prune();
|
|
176
|
+
const expired = this.tombstones.has(key);
|
|
177
|
+
return Object.freeze({
|
|
178
|
+
protocol: 2,
|
|
179
|
+
result: expired ? "expired" : "not_found",
|
|
180
|
+
diagnostic: schedulerFailure(expired ? "RUNTIME_SCHEDULER_OPERATION_EXPIRED" : "RUNTIME_SCHEDULER_STATUS_NOT_FOUND", "Local history is volatile. Inspect the correct selector and prior effects before deliberately scheduling new work.").diagnostic
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
recent() {
|
|
184
|
+
this.prune();
|
|
185
|
+
return Object.freeze([...this.entries.values()].map((value) => value.entry.view));
|
|
186
|
+
}
|
|
187
|
+
clear() {
|
|
188
|
+
this.entries.clear();
|
|
189
|
+
this.tombstones.clear();
|
|
190
|
+
}
|
|
191
|
+
prune() {
|
|
192
|
+
const now = this.now();
|
|
193
|
+
const cutoff = now - 864e5;
|
|
194
|
+
for (const [key, value] of this.entries) {
|
|
195
|
+
if (value.at > cutoff && this.entries.size <= 1e3)
|
|
196
|
+
break;
|
|
197
|
+
this.entries.delete(key);
|
|
198
|
+
this.tombstones.delete(key);
|
|
199
|
+
this.tombstones.set(key, now);
|
|
200
|
+
}
|
|
201
|
+
for (const [key, at] of this.tombstones) {
|
|
202
|
+
if (at > cutoff && this.tombstones.size <= 1e4)
|
|
203
|
+
break;
|
|
204
|
+
this.tombstones.delete(key);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
// ../runtime/dist/adapters/scheduler/local/job-store.js
|
|
210
|
+
var LocalJobStore = class {
|
|
211
|
+
binding;
|
|
212
|
+
policy;
|
|
213
|
+
now;
|
|
214
|
+
jobs = /* @__PURE__ */ new Map();
|
|
215
|
+
history;
|
|
216
|
+
aliases = /* @__PURE__ */ new Map();
|
|
217
|
+
dedupe = /* @__PURE__ */ new Map();
|
|
218
|
+
appLabel;
|
|
219
|
+
constructor(binding, policy, now) {
|
|
220
|
+
this.binding = binding;
|
|
221
|
+
this.policy = policy;
|
|
222
|
+
this.now = now;
|
|
223
|
+
this.history = new LocalSchedulerHistory(now);
|
|
224
|
+
this.appLabel = `local-${createHash("sha256").update(binding.appId).digest("hex").slice(0, 16)}`;
|
|
225
|
+
}
|
|
226
|
+
find(id) {
|
|
227
|
+
return this.jobs.get(this.aliases.get(id) ?? id);
|
|
228
|
+
}
|
|
229
|
+
pendingCount() {
|
|
230
|
+
return [...this.jobs.values()].filter((job) => job.state === "pending").length;
|
|
231
|
+
}
|
|
232
|
+
reserve(input) {
|
|
233
|
+
schedulerIdentifier(input.logicalJobId);
|
|
234
|
+
schedulerOperationId(input.operationId);
|
|
235
|
+
if (input.origin.appId !== this.binding.appId || input.origin.generationId !== this.binding.generationId)
|
|
236
|
+
throw this.conflict();
|
|
237
|
+
const args = snapshotSchedulerAdmissionArgs(input.args, this.policy.maxArgsBytes, input.onErrorTarget === void 0 ? void 0 : input.target.name, input.correlationId);
|
|
238
|
+
const definition = Object.freeze({
|
|
239
|
+
...input,
|
|
240
|
+
...args,
|
|
241
|
+
runAtMs: schedulerInstant(input.runAtMs),
|
|
242
|
+
target: Object.freeze({ ...input.target }),
|
|
243
|
+
origin: Object.freeze({ ...input.origin }),
|
|
244
|
+
...input.onErrorTarget ? { onErrorTarget: Object.freeze({ ...input.onErrorTarget }) } : {}
|
|
245
|
+
});
|
|
246
|
+
if (input.dedupeKey !== void 0)
|
|
247
|
+
schedulerIdentifier(input.dedupeKey);
|
|
248
|
+
const signature = snapshotSchedulerArgs([
|
|
249
|
+
definition.origin,
|
|
250
|
+
definition.target,
|
|
251
|
+
definition.argsDigest,
|
|
252
|
+
definition.runAtMs,
|
|
253
|
+
definition.onErrorTarget ?? null,
|
|
254
|
+
definition.dedupeKey ?? null
|
|
255
|
+
]).argsDigest;
|
|
256
|
+
const digest = snapshotSchedulerArgs([
|
|
257
|
+
signature,
|
|
258
|
+
input.logicalJobId,
|
|
259
|
+
input.expectedRevision ?? null,
|
|
260
|
+
input.parentDeliveryId ?? null
|
|
261
|
+
]).argsDigest;
|
|
262
|
+
const previous = this.history.get(`op:${input.operationId}`);
|
|
263
|
+
if (previous) {
|
|
264
|
+
if (previous.digest !== digest || previous.receipt?.outcome === "cancelled" || !previous.receipt || !("runAt" in previous.receipt))
|
|
265
|
+
throw this.conflict();
|
|
266
|
+
return { receipt: previous.receipt, changed: false };
|
|
267
|
+
}
|
|
268
|
+
if (this.history.known(`op:${input.operationId}`))
|
|
269
|
+
throw this.conflict();
|
|
270
|
+
const key = definition.dedupeKey && JSON.stringify([definition.target.kind, definition.target.name, definition.dedupeKey]);
|
|
271
|
+
const explicit = this.find(input.logicalJobId);
|
|
272
|
+
if (!explicit && this.history.known(`job:${input.logicalJobId}`))
|
|
273
|
+
throw this.conflict();
|
|
274
|
+
const matching = key ? this.jobs.get(this.dedupe.get(key) ?? "") : void 0;
|
|
275
|
+
if (explicit && matching && explicit !== matching)
|
|
276
|
+
throw this.conflict();
|
|
277
|
+
let job = explicit ?? matching;
|
|
278
|
+
let outcome = "accepted";
|
|
279
|
+
let changed = true;
|
|
280
|
+
if (job) {
|
|
281
|
+
if (job.state !== "pending")
|
|
282
|
+
throw this.conflict();
|
|
283
|
+
if (job.signature === signature && matching) {
|
|
284
|
+
outcome = "deduplicated";
|
|
285
|
+
changed = false;
|
|
286
|
+
} else {
|
|
287
|
+
if (input.expectedRevision !== job.revision)
|
|
288
|
+
throw this.conflict();
|
|
289
|
+
outcome = "replaced";
|
|
290
|
+
if (job.dedupe)
|
|
291
|
+
this.dedupe.delete(job.dedupe);
|
|
292
|
+
job.definition = definition;
|
|
293
|
+
job.signature = signature;
|
|
294
|
+
job.revision++;
|
|
295
|
+
job.dedupe = key || void 0;
|
|
296
|
+
}
|
|
297
|
+
} else {
|
|
298
|
+
if (this.pendingCount() >= this.policy.maxPending)
|
|
299
|
+
throw schedulerFailure("RUNTIME_SCHEDULER_CAPACITY_EXCEEDED", "Inspect pending local jobs and wait for capacity or cancel known pending work before submitting a new operation.");
|
|
300
|
+
job = {
|
|
301
|
+
id: randomUUID(),
|
|
302
|
+
definition,
|
|
303
|
+
aliases: /* @__PURE__ */ new Set(),
|
|
304
|
+
revision: 1,
|
|
305
|
+
state: "pending",
|
|
306
|
+
signature,
|
|
307
|
+
dedupe: key || void 0
|
|
308
|
+
};
|
|
309
|
+
this.jobs.set(job.id, job);
|
|
310
|
+
}
|
|
311
|
+
job.aliases.add(input.logicalJobId);
|
|
312
|
+
this.aliases.set(input.logicalJobId, job.id);
|
|
313
|
+
if (key)
|
|
314
|
+
this.dedupe.set(key, job.id);
|
|
315
|
+
const receipt = Object.freeze({
|
|
316
|
+
protocol: 2,
|
|
317
|
+
operationId: input.operationId,
|
|
318
|
+
logicalJobId: input.logicalJobId,
|
|
319
|
+
canonicalJobId: job.id,
|
|
320
|
+
revision: job.revision,
|
|
321
|
+
outcome,
|
|
322
|
+
runAt: new Date(definition.runAtMs).toISOString(),
|
|
323
|
+
durability: "memory",
|
|
324
|
+
generationId: this.binding.generationId,
|
|
325
|
+
correlationId: input.correlationId
|
|
326
|
+
});
|
|
327
|
+
this.history.put(`op:${input.operationId}`, {
|
|
328
|
+
view: Object.freeze({
|
|
329
|
+
...this.view(job, input.logicalJobId),
|
|
330
|
+
operationId: input.operationId,
|
|
331
|
+
operationKind: "admission",
|
|
332
|
+
operationOutcome: outcome
|
|
333
|
+
}),
|
|
334
|
+
receipt,
|
|
335
|
+
digest
|
|
336
|
+
});
|
|
337
|
+
return changed ? { receipt, job, changed: true } : { receipt, changed: false };
|
|
338
|
+
}
|
|
339
|
+
cancel(input) {
|
|
340
|
+
schedulerIdentifier(input.logicalJobId);
|
|
341
|
+
schedulerOperationId(input.operationId);
|
|
342
|
+
const digest = snapshotSchedulerArgs([
|
|
343
|
+
input.logicalJobId,
|
|
344
|
+
input.expectedRevision ?? null,
|
|
345
|
+
input.parentDeliveryId ?? null
|
|
346
|
+
]).argsDigest;
|
|
347
|
+
const previous = this.history.get(`op:${input.operationId}`);
|
|
348
|
+
if (previous) {
|
|
349
|
+
if (previous.digest === digest && previous.receipt && !("runAt" in previous.receipt) && previous.receipt.logicalJobId === input.logicalJobId)
|
|
350
|
+
return previous.receipt;
|
|
351
|
+
throw this.conflict();
|
|
352
|
+
}
|
|
353
|
+
if (this.history.known(`op:${input.operationId}`))
|
|
354
|
+
throw this.conflict();
|
|
355
|
+
const job = this.find(input.logicalJobId);
|
|
356
|
+
const terminal = this.history.get(`job:${input.logicalJobId}`)?.view;
|
|
357
|
+
const outcome = !job ? terminal ? "terminal" : "not_found" : job.state === "running" ? "already_running" : "cancelled";
|
|
358
|
+
if (job && input.expectedRevision !== void 0 && input.expectedRevision !== job.revision)
|
|
359
|
+
throw this.conflict();
|
|
360
|
+
const view = job ? this.view(job, input.logicalJobId) : terminal ?? this.absentView(input.logicalJobId, input.correlationId);
|
|
361
|
+
if (job && outcome === "cancelled")
|
|
362
|
+
this.finish(job, "cancelled");
|
|
363
|
+
const receipt = Object.freeze({
|
|
364
|
+
protocol: 2,
|
|
365
|
+
operationId: input.operationId,
|
|
366
|
+
logicalJobId: input.logicalJobId,
|
|
367
|
+
...view.canonicalJobId ? { canonicalJobId: view.canonicalJobId, revision: view.revision } : {},
|
|
368
|
+
outcome,
|
|
369
|
+
durability: "memory",
|
|
370
|
+
generationId: this.binding.generationId,
|
|
371
|
+
correlationId: input.correlationId
|
|
372
|
+
});
|
|
373
|
+
this.history.put(`op:${input.operationId}`, {
|
|
374
|
+
receipt,
|
|
375
|
+
digest,
|
|
376
|
+
view: Object.freeze({
|
|
377
|
+
...view,
|
|
378
|
+
...outcome === "cancelled" ? { state: "cancelled" } : {},
|
|
379
|
+
operationId: input.operationId,
|
|
380
|
+
operationKind: "cancellation",
|
|
381
|
+
operationOutcome: outcome
|
|
382
|
+
})
|
|
383
|
+
});
|
|
384
|
+
return receipt;
|
|
385
|
+
}
|
|
386
|
+
claim(job, revision) {
|
|
387
|
+
if (this.jobs.get(job.id) !== job || job.state !== "pending" || job.revision !== revision)
|
|
388
|
+
return false;
|
|
389
|
+
job.state = "running";
|
|
390
|
+
return true;
|
|
391
|
+
}
|
|
392
|
+
/** Exact never-entered delivery has settled; preserve its accepted identity and revision. */
|
|
393
|
+
defer(job, revision) {
|
|
394
|
+
if (this.jobs.get(job.id) !== job || job.state !== "running" || job.revision !== revision)
|
|
395
|
+
return false;
|
|
396
|
+
job.state = "pending";
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
finish(job, state, outcome) {
|
|
400
|
+
const terminalAt = new Date(this.now()).toISOString();
|
|
401
|
+
this.jobs.delete(job.id);
|
|
402
|
+
if (job.dedupe && this.dedupe.get(job.dedupe) === job.id)
|
|
403
|
+
this.dedupe.delete(job.dedupe);
|
|
404
|
+
for (const alias of /* @__PURE__ */ new Set([job.id, ...job.aliases])) {
|
|
405
|
+
this.aliases.delete(alias);
|
|
406
|
+
this.history.put(`job:${alias}`, {
|
|
407
|
+
view: Object.freeze({
|
|
408
|
+
...this.view(job, alias),
|
|
409
|
+
state,
|
|
410
|
+
terminalAt,
|
|
411
|
+
...outcome ? localOutcomeView(outcome) : {}
|
|
412
|
+
})
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
inspectJob(id) {
|
|
417
|
+
schedulerIdentifier(id);
|
|
418
|
+
const job = this.find(id);
|
|
419
|
+
const view = job ? this.view(job, id) : this.history.get(`job:${id}`)?.view;
|
|
420
|
+
return view ? Object.freeze({ protocol: 2, result: "found", view }) : this.history.missing(`job:${id}`);
|
|
421
|
+
}
|
|
422
|
+
inspectOperation(id) {
|
|
423
|
+
schedulerIdentifier(id);
|
|
424
|
+
const entry = this.history.get(`op:${id}`);
|
|
425
|
+
if (!entry)
|
|
426
|
+
return this.history.missing(`op:${id}`);
|
|
427
|
+
const job = entry.view.canonicalJobId && this.find(entry.view.canonicalJobId);
|
|
428
|
+
const terminal = entry.view.canonicalJobId && this.history.get(`job:${entry.view.canonicalJobId}`)?.view;
|
|
429
|
+
return Object.freeze({
|
|
430
|
+
protocol: 2,
|
|
431
|
+
result: "found",
|
|
432
|
+
view: Object.freeze({
|
|
433
|
+
...entry.view,
|
|
434
|
+
...job ? { state: job.state } : terminal ? {
|
|
435
|
+
state: terminal.state,
|
|
436
|
+
diagnostic: terminal.diagnostic,
|
|
437
|
+
settlementDiagnostic: terminal.settlementDiagnostic,
|
|
438
|
+
delivery: terminal.delivery,
|
|
439
|
+
childOperationIds: terminal.childOperationIds,
|
|
440
|
+
terminalAt: terminal.terminalAt
|
|
441
|
+
} : {}
|
|
442
|
+
})
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
view(job, logicalJobId = job.definition.logicalJobId) {
|
|
446
|
+
return Object.freeze({
|
|
447
|
+
appId: this.appLabel,
|
|
448
|
+
logicalJobId,
|
|
449
|
+
canonicalJobId: job.id,
|
|
450
|
+
revision: job.revision,
|
|
451
|
+
state: job.state,
|
|
452
|
+
durability: "memory",
|
|
453
|
+
generationId: this.binding.generationId,
|
|
454
|
+
runAt: new Date(job.definition.runAtMs).toISOString(),
|
|
455
|
+
action: job.definition.target.name,
|
|
456
|
+
correlationId: job.definition.correlationId
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
absentView(logicalJobId, correlationId) {
|
|
460
|
+
return Object.freeze({
|
|
461
|
+
appId: this.appLabel,
|
|
462
|
+
logicalJobId,
|
|
463
|
+
state: "not_found",
|
|
464
|
+
durability: "memory",
|
|
465
|
+
generationId: this.binding.generationId,
|
|
466
|
+
correlationId
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
clear() {
|
|
470
|
+
this.jobs.clear();
|
|
471
|
+
this.aliases.clear();
|
|
472
|
+
this.dedupe.clear();
|
|
473
|
+
this.history.clear();
|
|
474
|
+
}
|
|
475
|
+
conflict() {
|
|
476
|
+
return schedulerFailure("RUNTIME_SCHEDULER_IDEMPOTENCY_CONFLICT", "Inspect existing local work and prior effects; only deliberately replace a known pending job with its current revision.");
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
// ../runtime/dist/adapters/scheduler/local/dashboard-snapshot.js
|
|
481
|
+
function localSchedulerDashboardSnapshot(store, crons, timezone) {
|
|
482
|
+
const pending = [...store.jobs.values()].filter((job) => job.state === "pending");
|
|
483
|
+
const failed = store.history.recent().filter((view) => view.state === "failed" && !view.operationId && (!view.canonicalJobId || view.logicalJobId === view.canonicalJobId) && view.action !== void 0 && view.terminalAt !== void 0).reverse();
|
|
484
|
+
const cronList = [...crons];
|
|
485
|
+
return Object.freeze({
|
|
486
|
+
durability: "memory",
|
|
487
|
+
generationId: store.binding.generationId,
|
|
488
|
+
timezone,
|
|
489
|
+
crons: Object.freeze(cronList.slice(0, 100).map(({ name, pattern }) => Object.freeze({ name, pattern }))),
|
|
490
|
+
pendingJobs: Object.freeze(pending.slice(0, 100).map((job) => Object.freeze({
|
|
491
|
+
id: job.id,
|
|
492
|
+
action: job.definition.target.name,
|
|
493
|
+
scheduledAt: new Date(job.definition.runAtMs).toISOString()
|
|
494
|
+
}))),
|
|
495
|
+
failedJobs: Object.freeze(failed.slice(0, 100).map((view) => Object.freeze({
|
|
496
|
+
id: view.canonicalJobId ?? view.logicalJobId,
|
|
497
|
+
action: view.action,
|
|
498
|
+
failedAt: view.terminalAt
|
|
499
|
+
}))),
|
|
500
|
+
truncated: pending.length > 100 || failed.length > 100 || cronList.length > 100
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// ../runtime/dist/adapters/scheduler/local/resource.js
|
|
505
|
+
function createLocalSchedulerResource(input) {
|
|
506
|
+
const binding = snapshotRuntimeBinding(input.binding);
|
|
507
|
+
const policy = snapshotLocalSchedulerPolicy(input.policy);
|
|
508
|
+
const origin = Object.freeze({
|
|
509
|
+
...binding,
|
|
510
|
+
deploymentId: binding.generationId,
|
|
511
|
+
artifactId: binding.generationId
|
|
512
|
+
});
|
|
513
|
+
const execution = Object.freeze({ ...origin, assignmentId: binding.generationId });
|
|
514
|
+
const store = new LocalJobStore(binding, policy, input.clock.now);
|
|
515
|
+
const timers = /* @__PURE__ */ new Map();
|
|
516
|
+
const waiting = /* @__PURE__ */ new Set();
|
|
517
|
+
const waking = /* @__PURE__ */ new Set();
|
|
518
|
+
const crons = /* @__PURE__ */ new Map();
|
|
519
|
+
const invocations = /* @__PURE__ */ new Map();
|
|
520
|
+
const issuedInvocations = /* @__PURE__ */ new WeakMap();
|
|
521
|
+
const actualWork = /* @__PURE__ */ new Set();
|
|
522
|
+
let stopped = false;
|
|
523
|
+
let started = false;
|
|
524
|
+
let armed = false;
|
|
525
|
+
let dispatch;
|
|
526
|
+
let unsubscribe;
|
|
527
|
+
let unsubscribeTargets;
|
|
528
|
+
let targetWork;
|
|
529
|
+
let unregisterStop;
|
|
530
|
+
let disposed;
|
|
531
|
+
let stoppedWork;
|
|
532
|
+
let quiesceFailed = false;
|
|
533
|
+
function observe(phase, fields = {}) {
|
|
534
|
+
try {
|
|
535
|
+
input.logger.info(`runtime.scheduler.${phase}`, {
|
|
536
|
+
"runtime.capability": "scheduler",
|
|
537
|
+
"scheduler.app": store.appLabel,
|
|
538
|
+
"scheduler.durability": "memory",
|
|
539
|
+
"scheduler.timezone": input.timezone,
|
|
540
|
+
...fields
|
|
541
|
+
});
|
|
542
|
+
} catch {
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
function assertAdmission() {
|
|
546
|
+
if (stopped || !input.admission.isReady())
|
|
547
|
+
throw schedulerFailure("RUNTIME_GENERATION_CLOSING", "Use a ready local generation; closing cannot accept new work.");
|
|
548
|
+
}
|
|
549
|
+
function active() {
|
|
550
|
+
return !stopped && started && input.activation.isActive() && input.admission.isReady();
|
|
551
|
+
}
|
|
552
|
+
function capacityFailure() {
|
|
553
|
+
return schedulerFailure("RUNTIME_SCHEDULER_CAPACITY_EXCEEDED", "Inspect active local work; wait for known capacity before a deliberate invocation.");
|
|
554
|
+
}
|
|
555
|
+
function cancelTimer(id) {
|
|
556
|
+
timers.get(id)?.cancel();
|
|
557
|
+
timers.delete(id);
|
|
558
|
+
waiting.delete(id);
|
|
559
|
+
waking.delete(id);
|
|
560
|
+
}
|
|
561
|
+
function schedule(job) {
|
|
562
|
+
cancelTimer(job.id);
|
|
563
|
+
if (!active() || job.state !== "pending")
|
|
564
|
+
return;
|
|
565
|
+
const revision = job.revision;
|
|
566
|
+
const timer = input.clock.setTimer(() => {
|
|
567
|
+
if (timers.get(job.id) !== timer || !active() || job.revision !== revision || job.state !== "pending")
|
|
568
|
+
return;
|
|
569
|
+
timers.delete(job.id);
|
|
570
|
+
waking.delete(job.id);
|
|
571
|
+
if (job.definition.runAtMs > input.clock.now()) {
|
|
572
|
+
schedule(job);
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
if (targetWork?.blocked(job.definition.target) || invocations.size >= policy.maxActive) {
|
|
576
|
+
waiting.add(job.id);
|
|
577
|
+
observe("defer", {
|
|
578
|
+
"scheduler.job": job.id,
|
|
579
|
+
"scheduler.reason": targetWork?.blocked(job.definition.target) ? "timeout_work_still_live" : "capacity",
|
|
580
|
+
"scheduler.revision": revision
|
|
581
|
+
});
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
if (!store.claim(job, revision))
|
|
585
|
+
return;
|
|
586
|
+
const work = invoke("one-shot", job.id, revision, job.definition.target, job.definition.args, job.definition.correlationId, async (outcome) => {
|
|
587
|
+
store.finish(job, outcome.execution === "succeeded" && outcome.effects === "settled" ? "succeeded" : "failed", outcome);
|
|
588
|
+
if (outcome.execution === "failed" && job.definition.onErrorTarget && active()) {
|
|
589
|
+
const args = snapshotOnErrorArgs(job.definition.target.name, job.id, job.definition.args, policy.maxArgsBytes).args;
|
|
590
|
+
await invoke("on-error", randomUUID2(), 1, job.definition.onErrorTarget, args, job.definition.correlationId);
|
|
591
|
+
}
|
|
592
|
+
}, { job, revision });
|
|
593
|
+
void work.catch((error) => recordFailure(randomUUID2(), error, job.definition.target.name));
|
|
594
|
+
}, Math.max(0, Math.min(2147483647, job.definition.runAtMs - input.clock.now())));
|
|
595
|
+
timers.set(job.id, timer);
|
|
596
|
+
}
|
|
597
|
+
function resumePending() {
|
|
598
|
+
if (!active())
|
|
599
|
+
return;
|
|
600
|
+
let available = policy.maxActive - invocations.size - waking.size;
|
|
601
|
+
for (const id of waiting) {
|
|
602
|
+
if (available <= 0)
|
|
603
|
+
break;
|
|
604
|
+
const job = store.jobs.get(id);
|
|
605
|
+
if (job?.state === "pending" && targetWork?.blocked(job.definition.target))
|
|
606
|
+
continue;
|
|
607
|
+
waiting.delete(id);
|
|
608
|
+
if (job?.state !== "pending")
|
|
609
|
+
continue;
|
|
610
|
+
schedule(job);
|
|
611
|
+
if (timers.has(id) && job.definition.runAtMs <= input.clock.now()) {
|
|
612
|
+
waking.add(id);
|
|
613
|
+
available--;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
function outcomeFailure(invocation, error) {
|
|
618
|
+
const diagnosis = runtimeDiagnostic(error) ?? schedulerFailure("RUNTIME_SCHEDULER_EXECUTION_FAILED", "Inspect the safe local job outcome and prior effects before deliberately scheduling new work.", "request", invocation.correlationId).diagnostic;
|
|
619
|
+
return Object.freeze({
|
|
620
|
+
outcomeId: randomUUID2(),
|
|
621
|
+
deliveryId: invocation.deliveryId,
|
|
622
|
+
occurrenceId: invocation.occurrenceId,
|
|
623
|
+
attempt: 1,
|
|
624
|
+
execution: "failed",
|
|
625
|
+
effects: "settled",
|
|
626
|
+
dataImpact: "unknown",
|
|
627
|
+
operationIds: Object.freeze([]),
|
|
628
|
+
diagnostic: Object.freeze({
|
|
629
|
+
...diagnosis,
|
|
630
|
+
correlationId: invocation.correlationId,
|
|
631
|
+
dataImpact: "unknown"
|
|
632
|
+
})
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
function invoke(source, jobId, revision, target, args, correlationId, settled, claimed) {
|
|
636
|
+
assertAdmission();
|
|
637
|
+
if (!active() || !dispatch)
|
|
638
|
+
throw schedulerFailure("RUNTIME_SCHEDULER_EXECUTION_PENDING", "Activate the ready embedded generation, or wait for the local listener to bind, before direct execution.");
|
|
639
|
+
if (targetWork?.blocked(target))
|
|
640
|
+
throw schedulerFailure("RUNTIME_SCHEDULER_EXECUTION_PENDING", "Wait for the original timed-out target to settle; inspect its accepted job before deliberately executing direct or recurring work again.", "request", correlationId);
|
|
641
|
+
if (invocations.size >= policy.maxActive)
|
|
642
|
+
throw capacityFailure();
|
|
643
|
+
const lease = input.admission.acquireWork();
|
|
644
|
+
const invocation = Object.freeze({
|
|
645
|
+
source,
|
|
646
|
+
jobId,
|
|
647
|
+
revision,
|
|
648
|
+
occurrenceId: randomUUID2(),
|
|
649
|
+
deliveryId: randomUUID2(),
|
|
650
|
+
attempt: 1,
|
|
651
|
+
target,
|
|
652
|
+
args,
|
|
653
|
+
origin,
|
|
654
|
+
execution,
|
|
655
|
+
correlationId,
|
|
656
|
+
signal: lease.signal
|
|
657
|
+
});
|
|
658
|
+
const issued = {
|
|
659
|
+
lease,
|
|
660
|
+
source,
|
|
661
|
+
...claimed ? { job: claimed.job, revision: claimed.revision } : {},
|
|
662
|
+
consumed: false,
|
|
663
|
+
released: false
|
|
664
|
+
};
|
|
665
|
+
issuedInvocations.set(invocation, issued);
|
|
666
|
+
let begin;
|
|
667
|
+
let fail;
|
|
668
|
+
const work = new Promise((resolve, reject) => {
|
|
669
|
+
begin = resolve;
|
|
670
|
+
fail = reject;
|
|
671
|
+
}).catch((error) => outcomeFailure(invocation, error));
|
|
672
|
+
invocations.set(invocation.deliveryId, work);
|
|
673
|
+
observe("execution", {
|
|
674
|
+
"scheduler.job": jobId,
|
|
675
|
+
"scheduler.delivery": invocation.deliveryId,
|
|
676
|
+
"scheduler.source": source,
|
|
677
|
+
"scheduler.revision": revision,
|
|
678
|
+
"request.id": correlationId
|
|
679
|
+
});
|
|
680
|
+
const actual = work.then(async (outcome) => {
|
|
681
|
+
invocations.delete(invocation.deliveryId);
|
|
682
|
+
observe("settlement", {
|
|
683
|
+
"scheduler.job": jobId,
|
|
684
|
+
"scheduler.delivery": invocation.deliveryId,
|
|
685
|
+
"scheduler.execution": outcome.execution,
|
|
686
|
+
"scheduler.effects": outcome.effects,
|
|
687
|
+
"runtime.data_impact": outcome.dataImpact,
|
|
688
|
+
"request.id": correlationId
|
|
689
|
+
});
|
|
690
|
+
if (source === "on-error" || source === "direct") {
|
|
691
|
+
store.history.put(`job:${jobId}`, {
|
|
692
|
+
view: Object.freeze({
|
|
693
|
+
...store.absentView(jobId, correlationId),
|
|
694
|
+
state: outcome.execution === "succeeded" && outcome.effects === "settled" ? "succeeded" : "failed",
|
|
695
|
+
action: target.name,
|
|
696
|
+
terminalAt: new Date(input.clock.now()).toISOString(),
|
|
697
|
+
...localOutcomeView(outcome)
|
|
698
|
+
})
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
if (claimed && targetWork?.isDeferred(outcome, invocation) && store.defer(claimed.job, claimed.revision)) {
|
|
702
|
+
waiting.add(claimed.job.id);
|
|
703
|
+
} else
|
|
704
|
+
await settled?.(outcome);
|
|
705
|
+
return outcome;
|
|
706
|
+
}).finally(() => {
|
|
707
|
+
invocations.delete(invocation.deliveryId);
|
|
708
|
+
issued.released = true;
|
|
709
|
+
lease.release();
|
|
710
|
+
resumePending();
|
|
711
|
+
});
|
|
712
|
+
actualWork.add(actual);
|
|
713
|
+
void actual.then(() => actualWork.delete(actual), () => actualWork.delete(actual));
|
|
714
|
+
try {
|
|
715
|
+
begin(dispatch(invocation));
|
|
716
|
+
} catch (error) {
|
|
717
|
+
fail(error);
|
|
718
|
+
}
|
|
719
|
+
return actual;
|
|
720
|
+
}
|
|
721
|
+
function recordFailure(jobId, error, action) {
|
|
722
|
+
const diagnostic = runtimeDiagnostic(error) ?? schedulerFailure("RUNTIME_SCHEDULER_EXECUTION_FAILED", "Inspect prior local effects before deliberately scheduling replacement work.").diagnostic;
|
|
723
|
+
const view = Object.freeze({
|
|
724
|
+
...store.absentView(jobId, diagnostic.correlationId ?? randomUUID2()),
|
|
725
|
+
state: "failed",
|
|
726
|
+
action,
|
|
727
|
+
terminalAt: new Date(input.clock.now()).toISOString(),
|
|
728
|
+
diagnostic
|
|
729
|
+
});
|
|
730
|
+
store.history.put(`job:${jobId}`, { view });
|
|
731
|
+
try {
|
|
732
|
+
input.logger.error("runtime.scheduler.occurrence.failed", {
|
|
733
|
+
"runtime.public_code": diagnostic.code,
|
|
734
|
+
"runtime.capability": "scheduler",
|
|
735
|
+
"request.id": view.correlationId,
|
|
736
|
+
"scheduler.timezone": input.timezone
|
|
737
|
+
});
|
|
738
|
+
} catch {
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
function prepareCron(name, pattern, target, declarative) {
|
|
742
|
+
const previous = crons.get(name);
|
|
743
|
+
const record = {
|
|
744
|
+
name,
|
|
745
|
+
pattern,
|
|
746
|
+
target,
|
|
747
|
+
declarative,
|
|
748
|
+
revision: (previous?.revision ?? 0) + 1,
|
|
749
|
+
watermark: input.clock.now(),
|
|
750
|
+
handle: input.cron.create(pattern, async (due) => {
|
|
751
|
+
if (!active() || crons.get(name) !== record || due <= record.watermark)
|
|
752
|
+
return;
|
|
753
|
+
record.watermark = due;
|
|
754
|
+
const id = randomUUID2();
|
|
755
|
+
if (input.clock.now() - due >= 1e3) {
|
|
756
|
+
recordFailure(id, schedulerFailure("RUNTIME_SCHEDULER_EXECUTION_PENDING", "A local cron occurrence was missed; scheduling resumes at the next future calendar occurrence without catch-up."), name);
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
759
|
+
try {
|
|
760
|
+
const outcome = await invoke("cron", id, record.revision, target, snapshotSchedulerArgs({}).args, randomUUID2());
|
|
761
|
+
const state = outcome.execution === "succeeded" && outcome.effects === "settled" ? "succeeded" : "failed";
|
|
762
|
+
store.history.put(`job:${id}`, {
|
|
763
|
+
view: Object.freeze({
|
|
764
|
+
...store.absentView(id, outcome.diagnostic?.correlationId ?? id),
|
|
765
|
+
state,
|
|
766
|
+
action: name,
|
|
767
|
+
terminalAt: new Date(input.clock.now()).toISOString(),
|
|
768
|
+
...localOutcomeView(outcome)
|
|
769
|
+
})
|
|
770
|
+
});
|
|
771
|
+
} catch (error) {
|
|
772
|
+
recordFailure(id, error, name);
|
|
773
|
+
}
|
|
774
|
+
}, (due) => {
|
|
775
|
+
if (!active() || crons.get(name) !== record || due <= record.watermark)
|
|
776
|
+
return;
|
|
777
|
+
record.watermark = due;
|
|
778
|
+
recordFailure(randomUUID2(), schedulerFailure("RUNTIME_SCHEDULER_EXECUTION_PENDING", "A local cron occurrence was missed; no catch-up queue is created."), name);
|
|
779
|
+
})
|
|
780
|
+
};
|
|
781
|
+
return record;
|
|
782
|
+
}
|
|
783
|
+
function activate() {
|
|
784
|
+
if (!active() || armed)
|
|
785
|
+
return;
|
|
786
|
+
armed = true;
|
|
787
|
+
for (const record of crons.values()) {
|
|
788
|
+
record.watermark = input.clock.now();
|
|
789
|
+
record.handle.start();
|
|
790
|
+
}
|
|
791
|
+
for (const job of store.jobs.values())
|
|
792
|
+
schedule(job);
|
|
793
|
+
}
|
|
794
|
+
function quiesce() {
|
|
795
|
+
if (stopped) {
|
|
796
|
+
if (quiesceFailed)
|
|
797
|
+
throw schedulerFailure("RUNTIME_COMPONENT_DEPENDENCY_INVALID", "Keep Runtime-owned resources intact until local scheduler delivery fencing is confirmed.");
|
|
798
|
+
return;
|
|
799
|
+
}
|
|
800
|
+
stopped = true;
|
|
801
|
+
observe("stop", { "scheduler.pending": store.pendingCount(), "scheduler.running": invocations.size });
|
|
802
|
+
let failed = false;
|
|
803
|
+
try {
|
|
804
|
+
unsubscribeTargets?.();
|
|
805
|
+
unsubscribeTargets = void 0;
|
|
806
|
+
unsubscribe?.();
|
|
807
|
+
} catch {
|
|
808
|
+
failed = true;
|
|
809
|
+
}
|
|
810
|
+
unsubscribe = void 0;
|
|
811
|
+
for (const timer of timers.values()) {
|
|
812
|
+
try {
|
|
813
|
+
timer.cancel();
|
|
814
|
+
} catch {
|
|
815
|
+
failed = true;
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
timers.clear();
|
|
819
|
+
waiting.clear();
|
|
820
|
+
waking.clear();
|
|
821
|
+
for (const record of crons.values()) {
|
|
822
|
+
try {
|
|
823
|
+
record.handle.stop();
|
|
824
|
+
} catch {
|
|
825
|
+
failed = true;
|
|
826
|
+
}
|
|
827
|
+
try {
|
|
828
|
+
record.handle.destroy();
|
|
829
|
+
} catch {
|
|
830
|
+
failed = true;
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
crons.clear();
|
|
834
|
+
if (failed) {
|
|
835
|
+
quiesceFailed = true;
|
|
836
|
+
throw schedulerFailure("RUNTIME_COMPONENT_DEPENDENCY_INVALID", "Keep Runtime-owned resources intact until local scheduler delivery fencing is confirmed.");
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
function stop() {
|
|
840
|
+
try {
|
|
841
|
+
quiesce();
|
|
842
|
+
} catch {
|
|
843
|
+
}
|
|
844
|
+
stoppedWork ??= Promise.allSettled([...actualWork]).then(() => {
|
|
845
|
+
if (quiesceFailed)
|
|
846
|
+
throw schedulerFailure("RUNTIME_COMPONENT_DEPENDENCY_INVALID", "Keep Runtime-owned resources intact until local scheduler delivery fencing is confirmed.");
|
|
847
|
+
});
|
|
848
|
+
return stoppedWork;
|
|
849
|
+
}
|
|
850
|
+
try {
|
|
851
|
+
if (input.catalog.crons.length > policy.maxCrons || input.catalog.crons.some((cron) => !isSupportedCronPattern(cron.pattern) || !input.cron.validate(cron.pattern)))
|
|
852
|
+
throw invalidCronDeclaration("Correct cron patterns and the local recurring-definition limit before restarting; earlier import effects may remain.");
|
|
853
|
+
for (const cron of input.catalog.crons)
|
|
854
|
+
crons.set(cron.name, prepareCron(cron.name, cron.pattern, cron.target, true));
|
|
855
|
+
unregisterStop = input.admission.onStopAdmissions(quiesce);
|
|
856
|
+
} catch (error) {
|
|
857
|
+
void stop().catch(() => {
|
|
858
|
+
});
|
|
859
|
+
throw error;
|
|
860
|
+
}
|
|
861
|
+
function summary() {
|
|
862
|
+
return Object.freeze({
|
|
863
|
+
appId: store.appLabel,
|
|
864
|
+
generationId: binding.generationId,
|
|
865
|
+
originArtifact: binding.generationId,
|
|
866
|
+
currentAssignment: binding.generationId,
|
|
867
|
+
durability: "memory",
|
|
868
|
+
timezone: input.timezone,
|
|
869
|
+
pending: store.pendingCount(),
|
|
870
|
+
running: invocations.size,
|
|
871
|
+
crons: Object.freeze([...crons.values()].map((record) => {
|
|
872
|
+
const next = record.handle.nextRun();
|
|
873
|
+
return Object.freeze({
|
|
874
|
+
name: record.name,
|
|
875
|
+
nextRun: next === null ? null : new Date(next).toISOString()
|
|
876
|
+
});
|
|
877
|
+
})),
|
|
878
|
+
recent: Object.freeze(store.history.recent().filter((view) => !view.operationId && (!view.canonicalJobId || view.canonicalJobId === view.logicalJobId)).slice(-20).reverse())
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
const delivery = Object.freeze({
|
|
882
|
+
enabled: true,
|
|
883
|
+
policy,
|
|
884
|
+
dashboardSnapshot: () => localSchedulerDashboardSnapshot(store, crons.values(), input.timezone),
|
|
885
|
+
reserve(job) {
|
|
886
|
+
assertAdmission();
|
|
887
|
+
input.catalog.capture(job.target);
|
|
888
|
+
if (job.onErrorTarget)
|
|
889
|
+
input.catalog.capture(job.onErrorTarget);
|
|
890
|
+
const result = store.reserve(job);
|
|
891
|
+
observe("admission", {
|
|
892
|
+
"scheduler.job": result.receipt.canonicalJobId,
|
|
893
|
+
"scheduler.operation": result.receipt.operationId,
|
|
894
|
+
"scheduler.outcome": result.receipt.outcome,
|
|
895
|
+
"scheduler.revision": result.receipt.revision,
|
|
896
|
+
"request.id": result.receipt.correlationId
|
|
897
|
+
});
|
|
898
|
+
if (result.changed) {
|
|
899
|
+
schedule(result.job);
|
|
900
|
+
resumePending();
|
|
901
|
+
}
|
|
902
|
+
return result.receipt;
|
|
903
|
+
},
|
|
904
|
+
submit(job) {
|
|
905
|
+
return Promise.resolve(this.reserve(job));
|
|
906
|
+
},
|
|
907
|
+
cancelNow(job) {
|
|
908
|
+
assertAdmission();
|
|
909
|
+
const receipt = store.cancel(job);
|
|
910
|
+
observe("cancellation", {
|
|
911
|
+
"scheduler.operation": receipt.operationId,
|
|
912
|
+
"scheduler.outcome": receipt.outcome,
|
|
913
|
+
"request.id": receipt.correlationId
|
|
914
|
+
});
|
|
915
|
+
if (receipt.outcome === "cancelled" && receipt.canonicalJobId) {
|
|
916
|
+
cancelTimer(receipt.canonicalJobId);
|
|
917
|
+
resumePending();
|
|
918
|
+
}
|
|
919
|
+
return receipt;
|
|
920
|
+
},
|
|
921
|
+
cancel(job) {
|
|
922
|
+
return Promise.resolve(this.cancelNow(job));
|
|
923
|
+
},
|
|
924
|
+
revisionFor(id) {
|
|
925
|
+
return store.find(id)?.revision;
|
|
926
|
+
},
|
|
927
|
+
registerAction(name, callback) {
|
|
928
|
+
assertAdmission();
|
|
929
|
+
input.catalog.registerAction(name, callback);
|
|
930
|
+
},
|
|
931
|
+
cron(name, pattern, callback) {
|
|
932
|
+
assertAdmission();
|
|
933
|
+
schedulerIdentifier(name);
|
|
934
|
+
const previous = crons.get(name);
|
|
935
|
+
if (previous?.declarative || !isSupportedCronPattern(pattern) || !input.cron.validate(pattern))
|
|
936
|
+
throw schedulerFailure("RUNTIME_CRON_DECLARATION_INVALID", "Use a valid pattern and a distinct imperative name; declarative crons cannot be shadowed.");
|
|
937
|
+
if (!previous && crons.size >= policy.maxCrons)
|
|
938
|
+
throw capacityFailure();
|
|
939
|
+
const target = input.catalog.registerInline(callback);
|
|
940
|
+
let record;
|
|
941
|
+
try {
|
|
942
|
+
record = prepareCron(name, pattern, target, false);
|
|
943
|
+
} catch (error) {
|
|
944
|
+
input.catalog.releaseInline(target);
|
|
945
|
+
throw error;
|
|
946
|
+
}
|
|
947
|
+
if (previous) {
|
|
948
|
+
previous.handle.stop();
|
|
949
|
+
previous.handle.destroy();
|
|
950
|
+
input.catalog.releaseInline(previous.target);
|
|
951
|
+
}
|
|
952
|
+
crons.set(name, record);
|
|
953
|
+
if (active())
|
|
954
|
+
record.handle.start();
|
|
955
|
+
},
|
|
956
|
+
executeDirect(target, args, correlationId) {
|
|
957
|
+
input.catalog.capture(target);
|
|
958
|
+
return invoke("direct", randomUUID2(), 1, target, snapshotSchedulerArgs(args, policy.maxArgsBytes).args, correlationId);
|
|
959
|
+
},
|
|
960
|
+
async inspectJob(id, continuation) {
|
|
961
|
+
if (continuation !== void 0)
|
|
962
|
+
throw schedulerFailure("RUNTIME_SCHEDULER_INPUT_INVALID", "Omit continuation when inspecting local scheduler state.");
|
|
963
|
+
return Object.freeze({ ...store.inspectJob(id), local: summary() });
|
|
964
|
+
},
|
|
965
|
+
async inspect(id, continuation) {
|
|
966
|
+
if (continuation !== void 0)
|
|
967
|
+
throw schedulerFailure("RUNTIME_SCHEDULER_INPUT_INVALID", "Omit continuation when inspecting local scheduler state.");
|
|
968
|
+
return Object.freeze({ ...store.inspectOperation(id), local: summary() });
|
|
969
|
+
},
|
|
970
|
+
async start(nextDispatch) {
|
|
971
|
+
if (stopped || started)
|
|
972
|
+
throw schedulerFailure("RUNTIME_GENERATION_CLOSING", "Start a local scheduler once on a ready generation.");
|
|
973
|
+
dispatch = nextDispatch;
|
|
974
|
+
targetWork = timedOutSchedulerWorkFor(nextDispatch);
|
|
975
|
+
unsubscribeTargets = targetWork?.subscribe(resumePending);
|
|
976
|
+
started = true;
|
|
977
|
+
try {
|
|
978
|
+
unsubscribe = input.activation.subscribe(activate);
|
|
979
|
+
if (stopped) {
|
|
980
|
+
unsubscribe();
|
|
981
|
+
unsubscribe = void 0;
|
|
982
|
+
} else
|
|
983
|
+
activate();
|
|
984
|
+
} catch {
|
|
985
|
+
try {
|
|
986
|
+
quiesce();
|
|
987
|
+
} catch {
|
|
988
|
+
}
|
|
989
|
+
await stop().catch(() => {
|
|
990
|
+
});
|
|
991
|
+
throw schedulerFailure("RUNTIME_SCHEDULER_INTEGRATION_UNAVAILABLE", "Restore the selected local scheduler activation boundary before restarting.");
|
|
992
|
+
}
|
|
993
|
+
const handle = Object.freeze({ quiesce, stop });
|
|
994
|
+
return handle;
|
|
995
|
+
},
|
|
996
|
+
dispose() {
|
|
997
|
+
const stopping = stop();
|
|
998
|
+
unregisterStop?.();
|
|
999
|
+
unregisterStop = void 0;
|
|
1000
|
+
disposed ??= stopping.then(() => {
|
|
1001
|
+
store.clear();
|
|
1002
|
+
dispatch = void 0;
|
|
1003
|
+
});
|
|
1004
|
+
return disposed;
|
|
1005
|
+
}
|
|
1006
|
+
});
|
|
1007
|
+
registerLocalSchedulerInvocationConsumer(delivery, (candidate) => {
|
|
1008
|
+
if (!candidate || typeof candidate !== "object")
|
|
1009
|
+
return void 0;
|
|
1010
|
+
const invocation = candidate;
|
|
1011
|
+
const issued = issuedInvocations.get(invocation);
|
|
1012
|
+
if (!issued || issued.consumed || issued.released || issued.lease.signal !== invocation.signal || issued.lease.signal.aborted)
|
|
1013
|
+
return void 0;
|
|
1014
|
+
if (invocation.source !== issued.source || invocation.execution?.appId !== binding.appId || invocation.execution?.generationId !== binding.generationId || invocation.origin?.appId !== binding.appId || invocation.origin?.generationId !== binding.generationId || !invocations.has(invocation.deliveryId))
|
|
1015
|
+
return void 0;
|
|
1016
|
+
if (invocation.source === "one-shot" !== Boolean(issued.job))
|
|
1017
|
+
return void 0;
|
|
1018
|
+
if (issued.job && (store.jobs.get(issued.job.id) !== issued.job || issued.job.state !== "running" || issued.job.revision !== issued.revision || invocation.jobId !== issued.job.id || invocation.revision !== issued.revision))
|
|
1019
|
+
return void 0;
|
|
1020
|
+
issued.consumed = true;
|
|
1021
|
+
const evidence = Object.freeze({
|
|
1022
|
+
kind: "local",
|
|
1023
|
+
binding,
|
|
1024
|
+
invocation,
|
|
1025
|
+
source: issued.source,
|
|
1026
|
+
deliveryId: invocation.deliveryId,
|
|
1027
|
+
occurrenceId: invocation.occurrenceId,
|
|
1028
|
+
attempt: invocation.attempt,
|
|
1029
|
+
correlationId: invocation.correlationId,
|
|
1030
|
+
assertActive() {
|
|
1031
|
+
if (issued.released || !invocations.has(invocation.deliveryId) || issued.lease.signal !== invocation.signal || issued.job && (store.jobs.get(issued.job.id) !== issued.job || issued.job.state !== "running" || issued.job.revision !== issued.revision))
|
|
1032
|
+
throw schedulerFailure("RUNTIME_GENERATION_CLOSING", "Use the still-active original local scheduler invocation; an ended delivery cannot authorize new work.", "request", invocation.correlationId);
|
|
1033
|
+
}
|
|
1034
|
+
});
|
|
1035
|
+
registerLocalJobSourceEvidence(evidence);
|
|
1036
|
+
return evidence;
|
|
1037
|
+
});
|
|
1038
|
+
return delivery;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
export {
|
|
1042
|
+
isSupportedCronPattern,
|
|
1043
|
+
snapshotLocalSchedulerPolicy,
|
|
1044
|
+
createLocalSchedulerResource
|
|
1045
|
+
};
|