@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.cjs
CHANGED
|
@@ -894,39 +894,102 @@ var Executor = class {
|
|
|
894
894
|
return job.lockTime ?? jobToRun.lockTime;
|
|
895
895
|
}
|
|
896
896
|
getContext(job, jobToRun, onStale, isExecutionActive) {
|
|
897
|
-
var _a;
|
|
898
897
|
const effectiveLockTime = this.getEffectiveLockTime(job, jobToRun);
|
|
899
|
-
let staleTimeout = setTimeout(() => onStale(), effectiveLockTime);
|
|
900
|
-
(_a = staleTimeout.unref) == null ? void 0 : _a.call(staleTimeout);
|
|
901
898
|
const contextLogger = import_logger3.logger.addMetadata({
|
|
902
899
|
jobName: jobToRun.name,
|
|
903
900
|
jobId: jobToRun.jobId
|
|
904
901
|
});
|
|
902
|
+
let staleTimeout;
|
|
903
|
+
let watchdogCleared = false;
|
|
904
|
+
let staleReported = false;
|
|
905
|
+
let pendingExtraTime;
|
|
906
|
+
let renewalRunning = false;
|
|
907
|
+
let renewalPromise;
|
|
908
|
+
const clearCurrentStaleTimeout = () => {
|
|
909
|
+
if (staleTimeout === void 0) return;
|
|
910
|
+
clearTimeout(staleTimeout);
|
|
911
|
+
staleTimeout = void 0;
|
|
912
|
+
};
|
|
913
|
+
const reportStale = () => {
|
|
914
|
+
if (staleReported) return;
|
|
915
|
+
staleReported = true;
|
|
916
|
+
watchdogCleared = true;
|
|
917
|
+
pendingExtraTime = void 0;
|
|
918
|
+
clearCurrentStaleTimeout();
|
|
919
|
+
onStale();
|
|
920
|
+
};
|
|
921
|
+
const armStaleTimeout = (lockedUntil) => {
|
|
922
|
+
var _a;
|
|
923
|
+
if (watchdogCleared || staleReported || !isExecutionActive()) return;
|
|
924
|
+
clearCurrentStaleTimeout();
|
|
925
|
+
staleTimeout = setTimeout(
|
|
926
|
+
() => {
|
|
927
|
+
staleTimeout = void 0;
|
|
928
|
+
reportStale();
|
|
929
|
+
},
|
|
930
|
+
Math.max(0, lockedUntil - Date.now())
|
|
931
|
+
);
|
|
932
|
+
(_a = staleTimeout.unref) == null ? void 0 : _a.call(staleTimeout);
|
|
933
|
+
};
|
|
934
|
+
const runRenewals = async () => {
|
|
935
|
+
try {
|
|
936
|
+
while (pendingExtraTime !== void 0) {
|
|
937
|
+
const extraTime = pendingExtraTime;
|
|
938
|
+
pendingExtraTime = void 0;
|
|
939
|
+
if (watchdogCleared || staleReported || !isExecutionActive()) {
|
|
940
|
+
clearCurrentStaleTimeout();
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
clearCurrentStaleTimeout();
|
|
944
|
+
const lockedUntil = Date.now() + extraTime;
|
|
945
|
+
let didExtend = false;
|
|
946
|
+
try {
|
|
947
|
+
didExtend = await this.jobsRepo.extendLockTime(
|
|
948
|
+
jobToRun.jobId,
|
|
949
|
+
extraTime,
|
|
950
|
+
jobToRun.lockId
|
|
951
|
+
);
|
|
952
|
+
} catch (error) {
|
|
953
|
+
reportStale();
|
|
954
|
+
throw error;
|
|
955
|
+
}
|
|
956
|
+
if (!didExtend) {
|
|
957
|
+
reportStale();
|
|
958
|
+
return;
|
|
959
|
+
}
|
|
960
|
+
if (watchdogCleared || staleReported || !isExecutionActive()) {
|
|
961
|
+
pendingExtraTime = void 0;
|
|
962
|
+
clearCurrentStaleTimeout();
|
|
963
|
+
return;
|
|
964
|
+
}
|
|
965
|
+
armStaleTimeout(lockedUntil);
|
|
966
|
+
}
|
|
967
|
+
} finally {
|
|
968
|
+
renewalRunning = false;
|
|
969
|
+
renewalPromise = void 0;
|
|
970
|
+
}
|
|
971
|
+
};
|
|
972
|
+
armStaleTimeout(Date.now() + effectiveLockTime);
|
|
905
973
|
return {
|
|
906
974
|
definition: job,
|
|
907
975
|
record: jobToRun,
|
|
908
976
|
tries: jobToRun.tries || 0,
|
|
909
|
-
clearStaleTimeout: () =>
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
977
|
+
clearStaleTimeout: () => {
|
|
978
|
+
watchdogCleared = true;
|
|
979
|
+
pendingExtraTime = void 0;
|
|
980
|
+
clearCurrentStaleTimeout();
|
|
981
|
+
},
|
|
982
|
+
extendLockTime: (extraTime) => {
|
|
983
|
+
if (watchdogCleared || staleReported || !isExecutionActive()) {
|
|
913
984
|
contextLogger.warn(`Will not extend lock for stale execution "${jobToRun.executionId}"`);
|
|
914
|
-
return;
|
|
915
|
-
}
|
|
916
|
-
clearTimeout(staleTimeout);
|
|
917
|
-
let didExtend = false;
|
|
918
|
-
try {
|
|
919
|
-
didExtend = await this.jobsRepo.extendLockTime(jobToRun.jobId, extraTime, jobToRun.lockId);
|
|
920
|
-
} catch (error) {
|
|
921
|
-
onStale();
|
|
922
|
-
throw error;
|
|
985
|
+
return Promise.resolve();
|
|
923
986
|
}
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
987
|
+
pendingExtraTime = Math.max(pendingExtraTime ?? extraTime, extraTime);
|
|
988
|
+
if (!renewalRunning) {
|
|
989
|
+
renewalRunning = true;
|
|
990
|
+
renewalPromise = runRenewals();
|
|
927
991
|
}
|
|
928
|
-
|
|
929
|
-
(_a2 = staleTimeout.unref) == null ? void 0 : _a2.call(staleTimeout);
|
|
992
|
+
return renewalPromise ?? Promise.resolve();
|
|
930
993
|
},
|
|
931
994
|
logger: contextLogger
|
|
932
995
|
};
|