@orion-js/dogs 4.6.7 → 4.6.8

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.js CHANGED
@@ -480,6 +480,17 @@ var JobsRepo = class {
480
480
  );
481
481
  return result.matchedCount > 0;
482
482
  }
483
+ async rescheduleAfterExecutionGuardAcquisitionFailure(jobId, lockId, nextRunAt) {
484
+ const result = await this.jobs.updateOne(
485
+ { _id: jobId, lockId, tries: { $gte: 1 } },
486
+ {
487
+ $set: { nextRunAt },
488
+ $inc: { tries: -1 },
489
+ $unset: { lockId: "", lockedUntil: "", claimWasStale: "" }
490
+ }
491
+ );
492
+ return result.matchedCount > 0;
493
+ }
483
494
  async deleteEventJob(jobId, lockId) {
484
495
  const result = await this.jobs.deleteOne({
485
496
  _id: jobId,
@@ -1080,6 +1091,42 @@ var Executor = class {
1080
1091
  }
1081
1092
  return false;
1082
1093
  }
1094
+ async handleExecutionGuardAcquisitionFailure(error, config, jobToRun, context) {
1095
+ context.logger.error(`Error acquiring execution guard for job "${jobToRun.name}"`, { error });
1096
+ const nextRunAt = new Date(Date.now() + (config.executionGuardRetryDelayMs ?? 1e3));
1097
+ try {
1098
+ const didReschedule = await this.jobsRepo.rescheduleAfterExecutionGuardAcquisitionFailure(
1099
+ jobToRun.jobId,
1100
+ jobToRun.lockId,
1101
+ nextRunAt
1102
+ );
1103
+ if (!didReschedule) {
1104
+ context.logger.warn(
1105
+ `Could not requeue job "${jobToRun.name}" after execution guard acquisition failed because its lock is no longer owned`
1106
+ );
1107
+ }
1108
+ } catch (rescheduleError) {
1109
+ context.logger.error(
1110
+ `Error requeueing job "${jobToRun.name}" after execution guard acquisition failed`,
1111
+ { error: rescheduleError }
1112
+ );
1113
+ }
1114
+ }
1115
+ async releaseExecutionGuardLease(lease, jobToRun, context) {
1116
+ try {
1117
+ await lease.release();
1118
+ } catch (error) {
1119
+ context.logger.error(`Error releasing execution guard for job "${jobToRun.name}"`, { error });
1120
+ }
1121
+ }
1122
+ async runGuardedResolver(job, jobToRun, context, lease) {
1123
+ try {
1124
+ return await job.resolve(jobToRun.params, context);
1125
+ } finally {
1126
+ context.clearStaleTimeout();
1127
+ await this.releaseExecutionGuardLease(lease, jobToRun, context);
1128
+ }
1129
+ }
1083
1130
  async executeJob(config, jobToRun, onExecutionStale) {
1084
1131
  const job = this.getJobDefinition(jobToRun, config.jobs);
1085
1132
  if (!job) return;
@@ -1161,13 +1208,36 @@ var Executor = class {
1161
1208
  params: jobToRun.params
1162
1209
  };
1163
1210
  await runWithOrionAsyncContext(extraContext, async () => {
1164
- try {
1211
+ let executionGuardLease;
1212
+ if (config.executionGuard) {
1165
1213
  updateOrionAsyncContext({
1166
1214
  jobName: jobToRun.name,
1167
1215
  jobId: jobToRun.jobId,
1168
1216
  params: jobToRun.params
1169
1217
  });
1170
- const result = await job.resolve(jobToRun.params, context);
1218
+ try {
1219
+ executionGuardLease = await config.executionGuard.acquire(jobToRun);
1220
+ } catch (error) {
1221
+ context.clearStaleTimeout();
1222
+ await this.handleExecutionGuardAcquisitionFailure(error, config, jobToRun, context);
1223
+ if (executionStatus === "stale") await staleHandlingPromise;
1224
+ return;
1225
+ }
1226
+ if (executionStatus === "stale") {
1227
+ await staleHandlingPromise;
1228
+ await this.releaseExecutionGuardLease(executionGuardLease, jobToRun, context);
1229
+ return;
1230
+ }
1231
+ }
1232
+ try {
1233
+ if (!config.executionGuard) {
1234
+ updateOrionAsyncContext({
1235
+ jobName: jobToRun.name,
1236
+ jobId: jobToRun.jobId,
1237
+ params: jobToRun.params
1238
+ });
1239
+ }
1240
+ const result = executionGuardLease ? await this.runGuardedResolver(job, jobToRun, context, executionGuardLease) : await job.resolve(jobToRun.params, context);
1171
1241
  context.clearStaleTimeout();
1172
1242
  if (executionStatus === "stale") {
1173
1243
  await staleHandlingPromise;
@@ -1346,7 +1416,11 @@ var WorkerService = class {
1346
1416
  jobs: config.jobs,
1347
1417
  maxTries: config.maxTries,
1348
1418
  maxTriesReachedRetentionMs: config.maxTriesReachedRetentionMs,
1349
- onMaxTriesReached: config.onMaxTriesReached
1419
+ onMaxTriesReached: config.onMaxTriesReached,
1420
+ ...config.executionGuard ? {
1421
+ executionGuard: config.executionGuard,
1422
+ executionGuardRetryDelayMs: Math.max(config.pollInterval, 1e3)
1423
+ } : {}
1350
1424
  };
1351
1425
  logger4.debug(`Running scheduler loop for jobs "${jobNames.join(", ")}"...`);
1352
1426
  let consecutivePartitionMisses = 0;