@orion-js/dogs 4.6.6 → 4.6.7
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 +84 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +84 -21
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/LICENSE +0 -21
package/dist/index.js
CHANGED
|
@@ -850,39 +850,102 @@ var Executor = class {
|
|
|
850
850
|
return job.lockTime ?? jobToRun.lockTime;
|
|
851
851
|
}
|
|
852
852
|
getContext(job, jobToRun, onStale, isExecutionActive) {
|
|
853
|
-
var _a;
|
|
854
853
|
const effectiveLockTime = this.getEffectiveLockTime(job, jobToRun);
|
|
855
|
-
let staleTimeout = setTimeout(() => onStale(), effectiveLockTime);
|
|
856
|
-
(_a = staleTimeout.unref) == null ? void 0 : _a.call(staleTimeout);
|
|
857
854
|
const contextLogger = logger3.addMetadata({
|
|
858
855
|
jobName: jobToRun.name,
|
|
859
856
|
jobId: jobToRun.jobId
|
|
860
857
|
});
|
|
858
|
+
let staleTimeout;
|
|
859
|
+
let watchdogCleared = false;
|
|
860
|
+
let staleReported = false;
|
|
861
|
+
let pendingExtraTime;
|
|
862
|
+
let renewalRunning = false;
|
|
863
|
+
let renewalPromise;
|
|
864
|
+
const clearCurrentStaleTimeout = () => {
|
|
865
|
+
if (staleTimeout === void 0) return;
|
|
866
|
+
clearTimeout(staleTimeout);
|
|
867
|
+
staleTimeout = void 0;
|
|
868
|
+
};
|
|
869
|
+
const reportStale = () => {
|
|
870
|
+
if (staleReported) return;
|
|
871
|
+
staleReported = true;
|
|
872
|
+
watchdogCleared = true;
|
|
873
|
+
pendingExtraTime = void 0;
|
|
874
|
+
clearCurrentStaleTimeout();
|
|
875
|
+
onStale();
|
|
876
|
+
};
|
|
877
|
+
const armStaleTimeout = (lockedUntil) => {
|
|
878
|
+
var _a;
|
|
879
|
+
if (watchdogCleared || staleReported || !isExecutionActive()) return;
|
|
880
|
+
clearCurrentStaleTimeout();
|
|
881
|
+
staleTimeout = setTimeout(
|
|
882
|
+
() => {
|
|
883
|
+
staleTimeout = void 0;
|
|
884
|
+
reportStale();
|
|
885
|
+
},
|
|
886
|
+
Math.max(0, lockedUntil - Date.now())
|
|
887
|
+
);
|
|
888
|
+
(_a = staleTimeout.unref) == null ? void 0 : _a.call(staleTimeout);
|
|
889
|
+
};
|
|
890
|
+
const runRenewals = async () => {
|
|
891
|
+
try {
|
|
892
|
+
while (pendingExtraTime !== void 0) {
|
|
893
|
+
const extraTime = pendingExtraTime;
|
|
894
|
+
pendingExtraTime = void 0;
|
|
895
|
+
if (watchdogCleared || staleReported || !isExecutionActive()) {
|
|
896
|
+
clearCurrentStaleTimeout();
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
clearCurrentStaleTimeout();
|
|
900
|
+
const lockedUntil = Date.now() + extraTime;
|
|
901
|
+
let didExtend = false;
|
|
902
|
+
try {
|
|
903
|
+
didExtend = await this.jobsRepo.extendLockTime(
|
|
904
|
+
jobToRun.jobId,
|
|
905
|
+
extraTime,
|
|
906
|
+
jobToRun.lockId
|
|
907
|
+
);
|
|
908
|
+
} catch (error) {
|
|
909
|
+
reportStale();
|
|
910
|
+
throw error;
|
|
911
|
+
}
|
|
912
|
+
if (!didExtend) {
|
|
913
|
+
reportStale();
|
|
914
|
+
return;
|
|
915
|
+
}
|
|
916
|
+
if (watchdogCleared || staleReported || !isExecutionActive()) {
|
|
917
|
+
pendingExtraTime = void 0;
|
|
918
|
+
clearCurrentStaleTimeout();
|
|
919
|
+
return;
|
|
920
|
+
}
|
|
921
|
+
armStaleTimeout(lockedUntil);
|
|
922
|
+
}
|
|
923
|
+
} finally {
|
|
924
|
+
renewalRunning = false;
|
|
925
|
+
renewalPromise = void 0;
|
|
926
|
+
}
|
|
927
|
+
};
|
|
928
|
+
armStaleTimeout(Date.now() + effectiveLockTime);
|
|
861
929
|
return {
|
|
862
930
|
definition: job,
|
|
863
931
|
record: jobToRun,
|
|
864
932
|
tries: jobToRun.tries || 0,
|
|
865
|
-
clearStaleTimeout: () =>
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
933
|
+
clearStaleTimeout: () => {
|
|
934
|
+
watchdogCleared = true;
|
|
935
|
+
pendingExtraTime = void 0;
|
|
936
|
+
clearCurrentStaleTimeout();
|
|
937
|
+
},
|
|
938
|
+
extendLockTime: (extraTime) => {
|
|
939
|
+
if (watchdogCleared || staleReported || !isExecutionActive()) {
|
|
869
940
|
contextLogger.warn(`Will not extend lock for stale execution "${jobToRun.executionId}"`);
|
|
870
|
-
return;
|
|
871
|
-
}
|
|
872
|
-
clearTimeout(staleTimeout);
|
|
873
|
-
let didExtend = false;
|
|
874
|
-
try {
|
|
875
|
-
didExtend = await this.jobsRepo.extendLockTime(jobToRun.jobId, extraTime, jobToRun.lockId);
|
|
876
|
-
} catch (error) {
|
|
877
|
-
onStale();
|
|
878
|
-
throw error;
|
|
941
|
+
return Promise.resolve();
|
|
879
942
|
}
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
943
|
+
pendingExtraTime = Math.max(pendingExtraTime ?? extraTime, extraTime);
|
|
944
|
+
if (!renewalRunning) {
|
|
945
|
+
renewalRunning = true;
|
|
946
|
+
renewalPromise = runRenewals();
|
|
883
947
|
}
|
|
884
|
-
|
|
885
|
-
(_a2 = staleTimeout.unref) == null ? void 0 : _a2.call(staleTimeout);
|
|
948
|
+
return renewalPromise ?? Promise.resolve();
|
|
886
949
|
},
|
|
887
950
|
logger: contextLogger
|
|
888
951
|
};
|