@dudousxd/nestjs-durable-core 0.100.0 → 0.101.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +337 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +152 -6
- package/dist/index.d.ts +152 -6
- package/dist/index.js +334 -11
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2312,6 +2312,9 @@ function normalizeDescriptor(raw) {
|
|
|
2312
2312
|
...raw.registrations !== void 0 ? {
|
|
2313
2313
|
registrations: raw.registrations
|
|
2314
2314
|
} : {},
|
|
2315
|
+
...raw.schedules !== void 0 ? {
|
|
2316
|
+
schedules: raw.schedules
|
|
2317
|
+
} : {},
|
|
2315
2318
|
...raw.partition !== void 0 ? {
|
|
2316
2319
|
partition: raw.partition
|
|
2317
2320
|
} : {},
|
|
@@ -2386,12 +2389,30 @@ function canonicalizeForHash(d) {
|
|
|
2386
2389
|
...d.registrations !== void 0 ? {
|
|
2387
2390
|
registrations: canonicalizeRegistrations(d.registrations)
|
|
2388
2391
|
} : {},
|
|
2392
|
+
// Conditional like `registrations`: a descriptor without schedules keeps its published hash.
|
|
2393
|
+
...d.schedules !== void 0 ? {
|
|
2394
|
+
schedules: canonicalSchedules(d.schedules)
|
|
2395
|
+
} : {},
|
|
2389
2396
|
partition: d.partition ?? null,
|
|
2390
2397
|
namespace: d.namespace ?? null,
|
|
2391
2398
|
startedAt: d.startedAt
|
|
2392
2399
|
};
|
|
2393
2400
|
}
|
|
2394
2401
|
__name(canonicalizeForHash, "canonicalizeForHash");
|
|
2402
|
+
function canonicalSchedules(a) {
|
|
2403
|
+
const items = [
|
|
2404
|
+
...a.items ?? []
|
|
2405
|
+
].sort((x, y) => x.key.localeCompare(y.key));
|
|
2406
|
+
return {
|
|
2407
|
+
tenant: a.tenant,
|
|
2408
|
+
items: items.map((i) => stableJson(i))
|
|
2409
|
+
};
|
|
2410
|
+
}
|
|
2411
|
+
__name(canonicalSchedules, "canonicalSchedules");
|
|
2412
|
+
function stableJson(value) {
|
|
2413
|
+
return JSON.stringify(value, (_key, v) => v !== null && typeof v === "object" && !Array.isArray(v) ? Object.fromEntries(Object.entries(v).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) : v);
|
|
2414
|
+
}
|
|
2415
|
+
__name(stableJson, "stableJson");
|
|
2395
2416
|
function fnv1a64Hex(input) {
|
|
2396
2417
|
const OFFSET = 0xcbf29ce484222325n;
|
|
2397
2418
|
const PRIME = 0x100000001b3n;
|
|
@@ -2449,6 +2470,14 @@ function describeWorker(opts) {
|
|
|
2449
2470
|
...registrations.length > 0 ? {
|
|
2450
2471
|
registrations
|
|
2451
2472
|
} : {},
|
|
2473
|
+
...opts.schedules !== void 0 ? {
|
|
2474
|
+
schedules: {
|
|
2475
|
+
tenant: opts.schedules.tenant,
|
|
2476
|
+
items: [
|
|
2477
|
+
...opts.schedules.items
|
|
2478
|
+
].sort((a, b) => a.key.localeCompare(b.key))
|
|
2479
|
+
}
|
|
2480
|
+
} : {},
|
|
2452
2481
|
...opts.partition ? {
|
|
2453
2482
|
partition: opts.partition
|
|
2454
2483
|
} : {},
|
|
@@ -3867,6 +3896,12 @@ function matchesTenantPredicates(row, query) {
|
|
|
3867
3896
|
__name(matchesTenantPredicates, "matchesTenantPredicates");
|
|
3868
3897
|
|
|
3869
3898
|
// src/schedules.ts
|
|
3899
|
+
var ANNOUNCED_SCHEDULE_TAG = "schedule:announced";
|
|
3900
|
+
function announcedScheduleId(namespace, key) {
|
|
3901
|
+
return `app:${namespace}:${key}`;
|
|
3902
|
+
}
|
|
3903
|
+
__name(announcedScheduleId, "announcedScheduleId");
|
|
3904
|
+
var PARKED_FOR_WORKER = "no_live_worker";
|
|
3870
3905
|
var ScheduleAlreadyExistsError = class extends Error {
|
|
3871
3906
|
static {
|
|
3872
3907
|
__name(this, "ScheduleAlreadyExistsError");
|
|
@@ -4007,15 +4042,28 @@ var ScheduleClient = class {
|
|
|
4007
4042
|
async trigger(id) {
|
|
4008
4043
|
const record = await this.mustGet(id);
|
|
4009
4044
|
const runId = `sched:${id}:manual:${this.now()}`;
|
|
4010
|
-
return this.
|
|
4045
|
+
return this.startFor(record, runId);
|
|
4011
4046
|
}
|
|
4012
4047
|
/**
|
|
4013
|
-
*
|
|
4048
|
+
* Start one run of `record`: through the tenant start path for an announced schedule (its run waits
|
|
4049
|
+
* `blocked` for a live worker of its app instead of failing), else a plain `start`.
|
|
4050
|
+
*/
|
|
4051
|
+
startFor(record, runId) {
|
|
4052
|
+
const input = specOf(record).input;
|
|
4053
|
+
const opts = startOptions(record);
|
|
4054
|
+
if (isAnnounced(record) && this.host.startParked) {
|
|
4055
|
+
return this.host.startParked(record.workflow, input, runId, opts);
|
|
4056
|
+
}
|
|
4057
|
+
return this.host.start(record.workflow, input, runId, opts);
|
|
4058
|
+
}
|
|
4059
|
+
/**
|
|
4060
|
+
* Fire every due schedule (in this engine's namespace; all namespaces for an operator engine; only
|
|
4061
|
+
* those carrying `opts.tag` when given):
|
|
4014
4062
|
* start its window's run and advance it to the next window. Called by the NestJS timer poller on
|
|
4015
4063
|
* each tick when `persistedSchedules` is on; call it yourself from any other driver. Returns the
|
|
4016
4064
|
* run ids started (or re-found, for an idempotent re-fire).
|
|
4017
4065
|
*/
|
|
4018
|
-
async tick(nowMs = this.now()) {
|
|
4066
|
+
async tick(nowMs = this.now(), opts = {}) {
|
|
4019
4067
|
const store = this.requireStore();
|
|
4020
4068
|
const started = [];
|
|
4021
4069
|
for (let round = 0; round < 50; round++) {
|
|
@@ -4023,6 +4071,9 @@ var ScheduleClient = class {
|
|
|
4023
4071
|
const due = await store.listSchedules({
|
|
4024
4072
|
dueBy: nowMs,
|
|
4025
4073
|
namespace: this.options.namespace,
|
|
4074
|
+
...opts.tag !== void 0 ? {
|
|
4075
|
+
tag: opts.tag
|
|
4076
|
+
} : {},
|
|
4026
4077
|
limit: TICK_BATCH
|
|
4027
4078
|
});
|
|
4028
4079
|
for (const record of due) {
|
|
@@ -4036,15 +4087,19 @@ var ScheduleClient = class {
|
|
|
4036
4087
|
}
|
|
4037
4088
|
/**
|
|
4038
4089
|
* When {@link tick} next has a schedule to fire, if one is due by `byMs`: the earliest `nextFireAt`
|
|
4039
|
-
* of the unpaused schedules (of this engine's namespace
|
|
4090
|
+
* of the unpaused schedules (of this engine's namespace, carrying `opts.tag` when given) due by
|
|
4091
|
+
* then, else `undefined`. One indexed
|
|
4040
4092
|
* read (the due-schedule scan `tick` makes, with a later bound and a limit of one). A store that
|
|
4041
4093
|
* does not persist schedules has none.
|
|
4042
4094
|
*/
|
|
4043
|
-
async nextFireAt(byMs) {
|
|
4095
|
+
async nextFireAt(byMs, opts = {}) {
|
|
4044
4096
|
if (!this.supported) return void 0;
|
|
4045
4097
|
const [next] = await this.requireStore().listSchedules({
|
|
4046
4098
|
dueBy: byMs,
|
|
4047
4099
|
namespace: this.options.namespace,
|
|
4100
|
+
...opts.tag !== void 0 ? {
|
|
4101
|
+
tag: opts.tag
|
|
4102
|
+
} : {},
|
|
4048
4103
|
limit: 1
|
|
4049
4104
|
});
|
|
4050
4105
|
return next?.nextFireAt ?? void 0;
|
|
@@ -4079,10 +4134,10 @@ var ScheduleClient = class {
|
|
|
4079
4134
|
let fired = false;
|
|
4080
4135
|
const skipMissed = spec.catchup === "skip" && !onTime;
|
|
4081
4136
|
const beforeWindow = latest < window;
|
|
4082
|
-
if (!skipMissed && !beforeWindow && !await this.overlapping(spec, state)) {
|
|
4137
|
+
if (!skipMissed && !beforeWindow && !await this.overlapping(spec, state) && !await this.waitingForWorker(record, state)) {
|
|
4083
4138
|
runId = `sched:${record.id}:${latest}`;
|
|
4084
4139
|
try {
|
|
4085
|
-
await this.
|
|
4140
|
+
await this.startFor(record, runId);
|
|
4086
4141
|
fired = true;
|
|
4087
4142
|
} catch (error2) {
|
|
4088
4143
|
if (await this.host.getRun(runId)) fired = true;
|
|
@@ -4106,6 +4161,182 @@ var ScheduleClient = class {
|
|
|
4106
4161
|
}, record.nextFireAt);
|
|
4107
4162
|
return fired ? runId : void 0;
|
|
4108
4163
|
}
|
|
4164
|
+
/**
|
|
4165
|
+
* An announced schedule whose previous run is still parked waiting for a live worker of its app
|
|
4166
|
+
* (the app is down): this window is folded into that run instead of parking one more. However long
|
|
4167
|
+
* the app is down, it finds at most one run per schedule waiting when it comes back, the same
|
|
4168
|
+
* bound `catchup: 'latest'` puts on windows missed while no scheduler polled.
|
|
4169
|
+
*/
|
|
4170
|
+
async waitingForWorker(record, state) {
|
|
4171
|
+
if (!isAnnounced(record) || !state.lastRunId) return false;
|
|
4172
|
+
const prev = await this.host.getRun(state.lastRunId);
|
|
4173
|
+
return prev?.status === "blocked" && prev.error?.code === PARKED_FOR_WORKER;
|
|
4174
|
+
}
|
|
4175
|
+
/**
|
|
4176
|
+
* Reconcile the schedules adopted from one app (`namespace`) with what its LIVE workers announce
|
|
4177
|
+
* now (`items`, one per key; the caller resolved disagreeing workers). The rule:
|
|
4178
|
+
* - an announced key with no schedule yet is created (id {@link announcedScheduleId}, tagged
|
|
4179
|
+
* {@link ANNOUNCED_SCHEDULE_TAG}, `catchup: 'latest'`); one whose definition changed is
|
|
4180
|
+
* re-applied like `upsert` (its history kept, its next fire recomputed from now). An equal
|
|
4181
|
+
* definition writes nothing, so any number of replicas announcing the same set is a no-op;
|
|
4182
|
+
* - a pause or resume made from the console survives: a re-applied definition keeps the current
|
|
4183
|
+
* paused state, unless the app's own declared `paused` changed;
|
|
4184
|
+
* - an adopted schedule the app no longer announces is marked missing, and paused ("withdrawn")
|
|
4185
|
+
* once it has stayed missing for `graceMs`. It is never deleted: its history stays, and an app
|
|
4186
|
+
* that announces it again (a rollback) gets it back, resumed unless an operator had paused it.
|
|
4187
|
+
*
|
|
4188
|
+
* Call it only for an app that has at least one live worker stating its schedules: with none (the
|
|
4189
|
+
* app is down, or runs an SDK that does not announce schedules), nothing is withdrawn, and its
|
|
4190
|
+
* schedules keep firing into runs that wait for a worker.
|
|
4191
|
+
*/
|
|
4192
|
+
async syncAnnounced(namespace, items, opts) {
|
|
4193
|
+
const store = this.requireStore();
|
|
4194
|
+
const now = opts.nowMs ?? this.now();
|
|
4195
|
+
const out = {
|
|
4196
|
+
adopted: [],
|
|
4197
|
+
restored: [],
|
|
4198
|
+
missing: [],
|
|
4199
|
+
withdrawn: [],
|
|
4200
|
+
invalid: []
|
|
4201
|
+
};
|
|
4202
|
+
const adopted = /* @__PURE__ */ new Map();
|
|
4203
|
+
for (let offset = 0; ; offset += TICK_BATCH) {
|
|
4204
|
+
const page = await store.listSchedules({
|
|
4205
|
+
namespace,
|
|
4206
|
+
tag: ANNOUNCED_SCHEDULE_TAG,
|
|
4207
|
+
limit: TICK_BATCH,
|
|
4208
|
+
offset
|
|
4209
|
+
});
|
|
4210
|
+
for (const record of page) if (isAnnounced(record)) adopted.set(record.id, record);
|
|
4211
|
+
if (page.length < TICK_BATCH) break;
|
|
4212
|
+
}
|
|
4213
|
+
for (const item of items) {
|
|
4214
|
+
const id = announcedScheduleId(namespace, item.key);
|
|
4215
|
+
const existing = adopted.get(id) ?? await store.getSchedule(id);
|
|
4216
|
+
adopted.delete(id);
|
|
4217
|
+
const doc = existing ? existing.state.announce : void 0;
|
|
4218
|
+
if (existing && !doc) {
|
|
4219
|
+
out.invalid.push({
|
|
4220
|
+
key: item.key,
|
|
4221
|
+
error: `schedule id "${id}" is taken`
|
|
4222
|
+
});
|
|
4223
|
+
continue;
|
|
4224
|
+
}
|
|
4225
|
+
const fingerprint = stableJson(item);
|
|
4226
|
+
if (existing && doc && doc.fingerprint === fingerprint) {
|
|
4227
|
+
if (doc.missingSince === void 0 && doc.withdrawnAt === void 0) continue;
|
|
4228
|
+
const resume = doc.withdrawnAt !== void 0 && !doc.pausedBeforeWithdrawal && existing.paused;
|
|
4229
|
+
const spec = specOf(existing);
|
|
4230
|
+
const window = resume ? nextWindowAfter(spec, now) : void 0;
|
|
4231
|
+
await store.updateSchedule(id, {
|
|
4232
|
+
// The withdrawal note goes; the note it replaced comes back.
|
|
4233
|
+
...doc.withdrawnAt !== void 0 ? {
|
|
4234
|
+
spec: stripUndefined({
|
|
4235
|
+
...spec,
|
|
4236
|
+
note: doc.noteBeforeWithdrawal
|
|
4237
|
+
})
|
|
4238
|
+
} : {},
|
|
4239
|
+
...resume ? {
|
|
4240
|
+
paused: false,
|
|
4241
|
+
nextFireAt: window == null ? null : window + jitterFor(id, window, spec)
|
|
4242
|
+
} : {},
|
|
4243
|
+
state: stripUndefined({
|
|
4244
|
+
...existing.state,
|
|
4245
|
+
...resume ? {
|
|
4246
|
+
window
|
|
4247
|
+
} : {},
|
|
4248
|
+
announce: {
|
|
4249
|
+
key: doc.key,
|
|
4250
|
+
fingerprint,
|
|
4251
|
+
declaredPaused: doc.declaredPaused
|
|
4252
|
+
}
|
|
4253
|
+
}),
|
|
4254
|
+
updatedAt: new Date(now)
|
|
4255
|
+
});
|
|
4256
|
+
out.restored.push(id);
|
|
4257
|
+
continue;
|
|
4258
|
+
}
|
|
4259
|
+
const declaredPaused = item.paused === true;
|
|
4260
|
+
const paused = existing === null || existing === void 0 ? declaredPaused : declaredPaused !== (doc?.declaredPaused === true) ? declaredPaused : doc?.withdrawnAt !== void 0 && !doc.pausedBeforeWithdrawal ? declaredPaused : void 0;
|
|
4261
|
+
try {
|
|
4262
|
+
await this.write({
|
|
4263
|
+
id,
|
|
4264
|
+
workflow: item.workflow,
|
|
4265
|
+
input: item.input,
|
|
4266
|
+
cron: item.cron,
|
|
4267
|
+
every: item.everyMs,
|
|
4268
|
+
timezone: item.timezone,
|
|
4269
|
+
jitter: item.jitterMs,
|
|
4270
|
+
overlap: item.overlap,
|
|
4271
|
+
catchup: "latest",
|
|
4272
|
+
namespace,
|
|
4273
|
+
tags: [
|
|
4274
|
+
ANNOUNCED_SCHEDULE_TAG
|
|
4275
|
+
],
|
|
4276
|
+
paused,
|
|
4277
|
+
// A note (an operator's pause reason) is kept; a withdrawal note is replaced by the one
|
|
4278
|
+
// it had replaced.
|
|
4279
|
+
note: doc?.withdrawnAt !== void 0 ? doc.noteBeforeWithdrawal : existing ? specOf(existing).note : void 0,
|
|
4280
|
+
// Fires belong to the app, never to whichever request context reached the sync.
|
|
4281
|
+
...existing ? {} : {
|
|
4282
|
+
tenantId: null
|
|
4283
|
+
}
|
|
4284
|
+
}, existing ?? null, {
|
|
4285
|
+
announce: {
|
|
4286
|
+
key: item.key,
|
|
4287
|
+
fingerprint,
|
|
4288
|
+
declaredPaused
|
|
4289
|
+
}
|
|
4290
|
+
});
|
|
4291
|
+
out.adopted.push(id);
|
|
4292
|
+
} catch (error2) {
|
|
4293
|
+
out.invalid.push({
|
|
4294
|
+
key: item.key,
|
|
4295
|
+
error: errorMessage(error2)
|
|
4296
|
+
});
|
|
4297
|
+
}
|
|
4298
|
+
}
|
|
4299
|
+
for (const record of adopted.values()) {
|
|
4300
|
+
const state = record.state;
|
|
4301
|
+
const doc = state.announce;
|
|
4302
|
+
if (doc.withdrawnAt !== void 0) continue;
|
|
4303
|
+
if (doc.missingSince === void 0) {
|
|
4304
|
+
await store.updateSchedule(record.id, {
|
|
4305
|
+
state: {
|
|
4306
|
+
...state,
|
|
4307
|
+
announce: {
|
|
4308
|
+
...doc,
|
|
4309
|
+
missingSince: now
|
|
4310
|
+
}
|
|
4311
|
+
},
|
|
4312
|
+
updatedAt: new Date(now)
|
|
4313
|
+
});
|
|
4314
|
+
out.missing.push(record.id);
|
|
4315
|
+
continue;
|
|
4316
|
+
}
|
|
4317
|
+
if (now - doc.missingSince < opts.graceMs) continue;
|
|
4318
|
+
const spec = {
|
|
4319
|
+
...specOf(record)
|
|
4320
|
+
};
|
|
4321
|
+
spec.note = `withdrawn: app "${namespace}" no longer announces schedule "${doc.key}"`;
|
|
4322
|
+
await store.updateSchedule(record.id, {
|
|
4323
|
+
paused: true,
|
|
4324
|
+
spec,
|
|
4325
|
+
state: {
|
|
4326
|
+
...state,
|
|
4327
|
+
announce: stripUndefined({
|
|
4328
|
+
...doc,
|
|
4329
|
+
withdrawnAt: now,
|
|
4330
|
+
pausedBeforeWithdrawal: record.paused,
|
|
4331
|
+
noteBeforeWithdrawal: specOf(record).note
|
|
4332
|
+
})
|
|
4333
|
+
},
|
|
4334
|
+
updatedAt: new Date(now)
|
|
4335
|
+
});
|
|
4336
|
+
out.withdrawn.push(record.id);
|
|
4337
|
+
}
|
|
4338
|
+
return out;
|
|
4339
|
+
}
|
|
4109
4340
|
/** `overlap: 'skip'` and the previous run of this schedule is still in flight. */
|
|
4110
4341
|
async overlapping(spec, state) {
|
|
4111
4342
|
if (spec.overlap !== "skip" || !state.lastRunId) return false;
|
|
@@ -4141,7 +4372,7 @@ var ScheduleClient = class {
|
|
|
4141
4372
|
tenantId: tenant.id
|
|
4142
4373
|
};
|
|
4143
4374
|
}
|
|
4144
|
-
async write(opts, existing) {
|
|
4375
|
+
async write(opts, existing, extraState = {}) {
|
|
4145
4376
|
const store = this.requireStore();
|
|
4146
4377
|
const spec = toSpec(opts);
|
|
4147
4378
|
const now = this.now();
|
|
@@ -4157,7 +4388,8 @@ var ScheduleClient = class {
|
|
|
4157
4388
|
spec,
|
|
4158
4389
|
state: stripUndefined({
|
|
4159
4390
|
...existing?.state ?? {},
|
|
4160
|
-
window
|
|
4391
|
+
window,
|
|
4392
|
+
...extraState
|
|
4161
4393
|
}),
|
|
4162
4394
|
...this.scheduleTenant(opts, existing),
|
|
4163
4395
|
createdAt: existing?.createdAt ?? new Date(now),
|
|
@@ -4203,6 +4435,10 @@ function specOf(record) {
|
|
|
4203
4435
|
return record.spec;
|
|
4204
4436
|
}
|
|
4205
4437
|
__name(specOf, "specOf");
|
|
4438
|
+
function isAnnounced(record) {
|
|
4439
|
+
return record.state?.announce !== void 0;
|
|
4440
|
+
}
|
|
4441
|
+
__name(isAnnounced, "isAnnounced");
|
|
4206
4442
|
function latestWindowAtOrBefore(spec, atMs) {
|
|
4207
4443
|
if (spec.cron != null) return prevCronFireMs(spec.cron, atMs, spec.timezone);
|
|
4208
4444
|
if (spec.rrule != null) {
|
|
@@ -4277,6 +4513,9 @@ function describe(record) {
|
|
|
4277
4513
|
lastError: state.lastError,
|
|
4278
4514
|
tenantId: record.tenantId ?? void 0,
|
|
4279
4515
|
tenantKind: record.tenantKind ?? void 0,
|
|
4516
|
+
announcedKey: state.announce?.key,
|
|
4517
|
+
unannouncedSince: state.announce?.missingSince != null ? new Date(state.announce.missingSince) : void 0,
|
|
4518
|
+
withdrawnAt: state.announce?.withdrawnAt != null ? new Date(state.announce.withdrawnAt) : void 0,
|
|
4280
4519
|
createdAt: record.createdAt,
|
|
4281
4520
|
updatedAt: record.updatedAt
|
|
4282
4521
|
});
|
|
@@ -6663,6 +6902,7 @@ function runNamespace(namespace) {
|
|
|
6663
6902
|
__name(runNamespace, "runNamespace");
|
|
6664
6903
|
var VERSION_UNDECLARED_TAG = "version:undeclared";
|
|
6665
6904
|
var NO_LIVE_WORKER = "no_live_worker";
|
|
6905
|
+
var DEFAULT_ANNOUNCED_SCHEDULE_GRACE_MS = 10 * 6e4;
|
|
6666
6906
|
var TenantStartInvalid = class TenantStartInvalid2 extends Error {
|
|
6667
6907
|
static {
|
|
6668
6908
|
__name(this, "TenantStartInvalid");
|
|
@@ -6876,6 +7116,10 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
6876
7116
|
remoteRedispatchMax;
|
|
6877
7117
|
/** How long a `blocked` run waits before the recovery poll re-checks the live fleet (design §7.5). */
|
|
6878
7118
|
blockedPollMs;
|
|
7119
|
+
/** Verifies the wire tenant of a start-run, and of a worker's schedule announcement. */
|
|
7120
|
+
verifyTenant;
|
|
7121
|
+
/** Wire tenants whose schedule announcement failed verification, warned about once. */
|
|
7122
|
+
rejectedScheduleTenants = /* @__PURE__ */ new Set();
|
|
6879
7123
|
workerDescriptorCacheMs;
|
|
6880
7124
|
descriptorCache = /* @__PURE__ */ new Map();
|
|
6881
7125
|
/** This engine's control-plane handshake descriptor — negotiated against each worker (design §7.3). */
|
|
@@ -7086,6 +7330,7 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
7086
7330
|
this.remoteRedispatchMs = deps.remoteRedispatchMs || void 0;
|
|
7087
7331
|
this.remoteRedispatchMax = Math.max(1, deps.remoteRedispatchMax ?? 10);
|
|
7088
7332
|
this.blockedPollMs = deps.blockedPollMs ?? 5e3;
|
|
7333
|
+
this.verifyTenant = deps.verifyTenant;
|
|
7089
7334
|
this.workerDescriptorCacheMs = deps.workerDescriptorCacheMs ?? 2e3;
|
|
7090
7335
|
this.cpDescriptor = controlPlaneDescriptor({
|
|
7091
7336
|
instanceId: `cp-${this.instanceId}`,
|
|
@@ -7126,6 +7371,9 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
7126
7371
|
});
|
|
7127
7372
|
this.schedules = new ScheduleClient(this.store, {
|
|
7128
7373
|
start: /* @__PURE__ */ __name((workflow, input, runId, opts) => this.start(workflow, input, runId, opts), "start"),
|
|
7374
|
+
// An announced (tenant) schedule fires through the tenant start path: with no live worker
|
|
7375
|
+
// of its app, the run waits `blocked` instead of the window being lost.
|
|
7376
|
+
startParked: /* @__PURE__ */ __name((workflow, input, runId, opts) => this.startWith(workflow, input, runId, opts, true), "startParked"),
|
|
7129
7377
|
getRun: /* @__PURE__ */ __name((runId) => this.store.getRun(runId), "getRun"),
|
|
7130
7378
|
isDraining: /* @__PURE__ */ __name(() => this.draining, "isDraining"),
|
|
7131
7379
|
tenant: /* @__PURE__ */ __name(() => this.tenancy?.current(), "tenant"),
|
|
@@ -7299,14 +7547,17 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
7299
7547
|
* The earliest known time (epoch ms) the poll has work, if it is at or before `byMs`: this
|
|
7300
7548
|
* namespace's next due timer (`listDueTimers` up to `byMs`, limit one: it returns the most overdue
|
|
7301
7549
|
* first), the next `blocked` run's recovery poll, and with `persistedSchedules` the next persisted
|
|
7302
|
-
* schedule to fire (see {@link ScheduleClient.nextFireAt}
|
|
7550
|
+
* schedule to fire (see {@link ScheduleClient.nextFireAt}; with `announcedSchedules` alone, the next
|
|
7551
|
+
* schedule adopted from a tenant). `undefined` when nothing is due by
|
|
7303
7552
|
* then. A poller reads it before waiting longer than its tick, so the wait never outlasts a timer.
|
|
7304
7553
|
*/
|
|
7305
7554
|
async nextDueAt(byMs, opts = {}) {
|
|
7306
7555
|
const [timers, blocked, schedule] = await Promise.all([
|
|
7307
7556
|
this.store.listDueTimers(byMs, this.namespace, 1),
|
|
7308
7557
|
this.dueBlockedRuns(byMs),
|
|
7309
|
-
opts.persistedSchedules ? this.schedules.nextFireAt(byMs) :
|
|
7558
|
+
opts.persistedSchedules ? this.schedules.nextFireAt(byMs) : opts.announcedSchedules && this.schedules.supported ? this.schedules.nextFireAt(byMs, {
|
|
7559
|
+
tag: ANNOUNCED_SCHEDULE_TAG
|
|
7560
|
+
}) : void 0
|
|
7310
7561
|
]);
|
|
7311
7562
|
let next;
|
|
7312
7563
|
for (const at of [
|
|
@@ -11935,6 +12186,75 @@ var WorkflowEngine = class _WorkflowEngine {
|
|
|
11935
12186
|
*
|
|
11936
12187
|
* Workflows only, and deliberately: see the "steps are out of scope" note in `handshake/announced`.
|
|
11937
12188
|
*/
|
|
12189
|
+
/**
|
|
12190
|
+
* Adopt the schedules tenant apps announce (their `schedules` module option, carried on their live
|
|
12191
|
+
* workers' descriptors) as persisted schedules, and withdraw the ones an app stopped announcing.
|
|
12192
|
+
* The control plane's timer poll calls it; {@link ScheduleClient.tick} fires them.
|
|
12193
|
+
*
|
|
12194
|
+
* Per app (the namespace its announcement's verified `tenant` maps to, as for a start-run): the
|
|
12195
|
+
* schedules announced by its live workers, where two workers disagree on a key the one that
|
|
12196
|
+
* started last (its deploy is the newer one) wins. An app with no live worker that states its
|
|
12197
|
+
* schedules is left alone, so its schedules keep firing into runs that wait for a worker. See
|
|
12198
|
+
* {@link ScheduleClient.syncAnnounced} for the reconciliation rule and `graceMs` (default 10
|
|
12199
|
+
* minutes). A namespaced engine adopts only its own namespace's. Nothing without a store that
|
|
12200
|
+
* persists schedules or a transport that reads worker descriptors.
|
|
12201
|
+
*/
|
|
12202
|
+
async syncAnnouncedSchedules(opts = {}) {
|
|
12203
|
+
const out = /* @__PURE__ */ new Map();
|
|
12204
|
+
if (!this.schedules.supported) return out;
|
|
12205
|
+
const byNamespace = /* @__PURE__ */ new Map();
|
|
12206
|
+
for (const descriptor of await this.pool.readAllWorkerDescriptors()) {
|
|
12207
|
+
const announced = descriptor.schedules;
|
|
12208
|
+
if (!announced || typeof announced.tenant !== "string" || !Array.isArray(announced.items)) {
|
|
12209
|
+
continue;
|
|
12210
|
+
}
|
|
12211
|
+
let verified;
|
|
12212
|
+
try {
|
|
12213
|
+
verified = await verifyWireTenant(announced.tenant, this.verifyTenant);
|
|
12214
|
+
} catch {
|
|
12215
|
+
verified = null;
|
|
12216
|
+
}
|
|
12217
|
+
const tenant = verified?.tenant ?? null;
|
|
12218
|
+
if (tenant === null) {
|
|
12219
|
+
if (!this.rejectedScheduleTenants.has(descriptor.instanceId)) {
|
|
12220
|
+
this.rejectedScheduleTenants.add(descriptor.instanceId);
|
|
12221
|
+
console.warn(`[nestjs-durable] ignored the schedules worker ${descriptor.instanceId} announces: invalid or unauthorized tenant token`);
|
|
12222
|
+
}
|
|
12223
|
+
continue;
|
|
12224
|
+
}
|
|
12225
|
+
const namespace = poolNamespace(tenant) ?? "default";
|
|
12226
|
+
if (this.namespace !== void 0 && namespace !== this.namespace) continue;
|
|
12227
|
+
const list2 = byNamespace.get(namespace) ?? [];
|
|
12228
|
+
list2.push({
|
|
12229
|
+
startedAt: Number(descriptor.startedAt) || 0,
|
|
12230
|
+
items: announced.items
|
|
12231
|
+
});
|
|
12232
|
+
byNamespace.set(namespace, list2);
|
|
12233
|
+
}
|
|
12234
|
+
for (const [namespace, announcers] of byNamespace) {
|
|
12235
|
+
const items = /* @__PURE__ */ new Map();
|
|
12236
|
+
for (const { items: list2 } of announcers.sort((a, b) => b.startedAt - a.startedAt)) {
|
|
12237
|
+
for (const item of list2) {
|
|
12238
|
+
if (item && typeof item.key === "string" && !items.has(item.key)) {
|
|
12239
|
+
items.set(item.key, item);
|
|
12240
|
+
}
|
|
12241
|
+
}
|
|
12242
|
+
}
|
|
12243
|
+
const result = await this.schedules.syncAnnounced(namespace, [
|
|
12244
|
+
...items.values()
|
|
12245
|
+
], {
|
|
12246
|
+
graceMs: opts.graceMs ?? DEFAULT_ANNOUNCED_SCHEDULE_GRACE_MS,
|
|
12247
|
+
...opts.nowMs !== void 0 ? {
|
|
12248
|
+
nowMs: opts.nowMs
|
|
12249
|
+
} : {}
|
|
12250
|
+
});
|
|
12251
|
+
for (const { key, error: error2 } of result.invalid) {
|
|
12252
|
+
console.warn(`[nestjs-durable] app "${namespace}" announces schedule "${key}", which cannot be adopted: ${error2}`);
|
|
12253
|
+
}
|
|
12254
|
+
out.set(namespace, result);
|
|
12255
|
+
}
|
|
12256
|
+
return out;
|
|
12257
|
+
}
|
|
11938
12258
|
async announcedWorkflows() {
|
|
11939
12259
|
return (await this.workflowDirectory()).workflows;
|
|
11940
12260
|
}
|
|
@@ -17797,6 +18117,7 @@ async function settleResultsWithPlainCalls(store, results) {
|
|
|
17797
18117
|
__name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
|
|
17798
18118
|
export {
|
|
17799
18119
|
ACCUMULATOR_SEEN_MAX,
|
|
18120
|
+
ANNOUNCED_SCHEDULE_TAG,
|
|
17800
18121
|
ANY_WAIT_GROUP,
|
|
17801
18122
|
CHILD_EDGE_PREFIXES,
|
|
17802
18123
|
CONTROL_FLOW_SIGNAL,
|
|
@@ -17805,6 +18126,7 @@ export {
|
|
|
17805
18126
|
CodecStateStore,
|
|
17806
18127
|
ConcurrencyLimitError,
|
|
17807
18128
|
ContinueAsNew,
|
|
18129
|
+
DEFAULT_ANNOUNCED_SCHEDULE_GRACE_MS,
|
|
17808
18130
|
DEFAULT_CONCURRENCY_STATUSES,
|
|
17809
18131
|
DEFAULT_PARTITION,
|
|
17810
18132
|
DEFAULT_STALLED_AFTER_MS,
|
|
@@ -17886,6 +18208,7 @@ export {
|
|
|
17886
18208
|
accumulatorRunId,
|
|
17887
18209
|
aggregateAnnouncements,
|
|
17888
18210
|
aggregatesCheckpoints,
|
|
18211
|
+
announcedScheduleId,
|
|
17889
18212
|
announcementsOf,
|
|
17890
18213
|
anyCheckpointName,
|
|
17891
18214
|
applyAttributeQuery,
|