@dudousxd/nestjs-durable-core 0.85.0 → 0.85.2
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 +37 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +14 -4
- package/dist/index.d.ts +14 -4
- package/dist/index.js +37 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -2630,7 +2630,31 @@ __name(resolveCronParse, "resolveCronParse");
|
|
|
2630
2630
|
|
|
2631
2631
|
// src/scheduler.ts
|
|
2632
2632
|
var nodeRequire = (0, import_node_module.createRequire)(__importMetaUrl);
|
|
2633
|
-
var
|
|
2633
|
+
var jitterFireAt = /* @__PURE__ */ new WeakMap();
|
|
2634
|
+
function deferredWindows(engine, key) {
|
|
2635
|
+
return [
|
|
2636
|
+
...jitterFireAt.get(engine)?.get(key)?.keys() ?? []
|
|
2637
|
+
];
|
|
2638
|
+
}
|
|
2639
|
+
__name(deferredWindows, "deferredWindows");
|
|
2640
|
+
function jitterDue(engine, s, candidates, nowMs, random) {
|
|
2641
|
+
let byKey = jitterFireAt.get(engine);
|
|
2642
|
+
if (byKey === void 0) {
|
|
2643
|
+
byKey = /* @__PURE__ */ new Map();
|
|
2644
|
+
jitterFireAt.set(engine, byKey);
|
|
2645
|
+
}
|
|
2646
|
+
const previous = byKey.get(s.key);
|
|
2647
|
+
const current = /* @__PURE__ */ new Map();
|
|
2648
|
+
const due = [];
|
|
2649
|
+
for (const runId of candidates) {
|
|
2650
|
+
const at = previous?.get(runId) ?? nowMs + Math.floor(random() * s.jitter);
|
|
2651
|
+
if (nowMs >= at) due.push(runId);
|
|
2652
|
+
else current.set(runId, at);
|
|
2653
|
+
}
|
|
2654
|
+
byKey.set(s.key, current);
|
|
2655
|
+
return due;
|
|
2656
|
+
}
|
|
2657
|
+
__name(jitterDue, "jitterDue");
|
|
2634
2658
|
function scheduledRunId(key, everyMs, nowMs) {
|
|
2635
2659
|
return `sched:${key}:${Math.floor(nowMs / everyMs)}`;
|
|
2636
2660
|
}
|
|
@@ -2700,13 +2724,19 @@ function scheduleRunIdsAt(s, nowMs) {
|
|
|
2700
2724
|
__name(scheduleRunIdsAt, "scheduleRunIdsAt");
|
|
2701
2725
|
async function runSchedules(engine, schedules, nowMs, opts) {
|
|
2702
2726
|
const random = opts?.random ?? Math.random;
|
|
2703
|
-
const sleep = opts?.sleep ?? defaultSleep;
|
|
2704
2727
|
const settled = settledIdsFor(engine);
|
|
2705
2728
|
const ids = [];
|
|
2706
2729
|
for (const s of schedules) {
|
|
2707
2730
|
if (engine.isDraining?.()) break;
|
|
2708
2731
|
if (s.paused) continue;
|
|
2709
|
-
const
|
|
2732
|
+
const due = scheduleRunIdsAt(s, nowMs);
|
|
2733
|
+
const windows = s.jitter && s.jitter > 0 ? [
|
|
2734
|
+
.../* @__PURE__ */ new Set([
|
|
2735
|
+
...deferredWindows(engine, s.key),
|
|
2736
|
+
...due
|
|
2737
|
+
])
|
|
2738
|
+
] : due;
|
|
2739
|
+
const candidates = windows.filter((id) => !settled.get(s.key)?.has(id));
|
|
2710
2740
|
if (candidates.length === 0) continue;
|
|
2711
2741
|
if (s.overlap === "skip" && s.everyMs) {
|
|
2712
2742
|
const prevBucket = Math.floor(nowMs / s.everyMs) - 1;
|
|
@@ -2715,8 +2745,10 @@ async function runSchedules(engine, schedules, nowMs, opts) {
|
|
|
2715
2745
|
if (prev && (prev.status === "running" || prev.status === "suspended")) continue;
|
|
2716
2746
|
}
|
|
2717
2747
|
}
|
|
2718
|
-
if (s.jitter && s.jitter > 0)
|
|
2719
|
-
|
|
2748
|
+
if (!(s.jitter && s.jitter > 0)) jitterFireAt.get(engine)?.delete(s.key);
|
|
2749
|
+
const firing = s.jitter && s.jitter > 0 ? jitterDue(engine, s, candidates, nowMs, random) : candidates;
|
|
2750
|
+
if (firing.length === 0) continue;
|
|
2751
|
+
for (const runId of firing) {
|
|
2720
2752
|
if (engine.isDraining?.()) break;
|
|
2721
2753
|
const existing = await engine.getRun(runId);
|
|
2722
2754
|
remember(settled, s.key, runId);
|