@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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Orionjs Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs CHANGED
@@ -524,6 +524,17 @@ var JobsRepo = class {
524
524
  );
525
525
  return result.matchedCount > 0;
526
526
  }
527
+ async rescheduleAfterExecutionGuardAcquisitionFailure(jobId, lockId, nextRunAt) {
528
+ const result = await this.jobs.updateOne(
529
+ { _id: jobId, lockId, tries: { $gte: 1 } },
530
+ {
531
+ $set: { nextRunAt },
532
+ $inc: { tries: -1 },
533
+ $unset: { lockId: "", lockedUntil: "", claimWasStale: "" }
534
+ }
535
+ );
536
+ return result.matchedCount > 0;
537
+ }
527
538
  async deleteEventJob(jobId, lockId) {
528
539
  const result = await this.jobs.deleteOne({
529
540
  _id: jobId,
@@ -1124,6 +1135,42 @@ var Executor = class {
1124
1135
  }
1125
1136
  return false;
1126
1137
  }
1138
+ async handleExecutionGuardAcquisitionFailure(error, config, jobToRun, context) {
1139
+ context.logger.error(`Error acquiring execution guard for job "${jobToRun.name}"`, { error });
1140
+ const nextRunAt = new Date(Date.now() + (config.executionGuardRetryDelayMs ?? 1e3));
1141
+ try {
1142
+ const didReschedule = await this.jobsRepo.rescheduleAfterExecutionGuardAcquisitionFailure(
1143
+ jobToRun.jobId,
1144
+ jobToRun.lockId,
1145
+ nextRunAt
1146
+ );
1147
+ if (!didReschedule) {
1148
+ context.logger.warn(
1149
+ `Could not requeue job "${jobToRun.name}" after execution guard acquisition failed because its lock is no longer owned`
1150
+ );
1151
+ }
1152
+ } catch (rescheduleError) {
1153
+ context.logger.error(
1154
+ `Error requeueing job "${jobToRun.name}" after execution guard acquisition failed`,
1155
+ { error: rescheduleError }
1156
+ );
1157
+ }
1158
+ }
1159
+ async releaseExecutionGuardLease(lease, jobToRun, context) {
1160
+ try {
1161
+ await lease.release();
1162
+ } catch (error) {
1163
+ context.logger.error(`Error releasing execution guard for job "${jobToRun.name}"`, { error });
1164
+ }
1165
+ }
1166
+ async runGuardedResolver(job, jobToRun, context, lease) {
1167
+ try {
1168
+ return await job.resolve(jobToRun.params, context);
1169
+ } finally {
1170
+ context.clearStaleTimeout();
1171
+ await this.releaseExecutionGuardLease(lease, jobToRun, context);
1172
+ }
1173
+ }
1127
1174
  async executeJob(config, jobToRun, onExecutionStale) {
1128
1175
  const job = this.getJobDefinition(jobToRun, config.jobs);
1129
1176
  if (!job) return;
@@ -1205,13 +1252,36 @@ var Executor = class {
1205
1252
  params: jobToRun.params
1206
1253
  };
1207
1254
  await (0, import_logger3.runWithOrionAsyncContext)(extraContext, async () => {
1208
- try {
1255
+ let executionGuardLease;
1256
+ if (config.executionGuard) {
1209
1257
  (0, import_logger3.updateOrionAsyncContext)({
1210
1258
  jobName: jobToRun.name,
1211
1259
  jobId: jobToRun.jobId,
1212
1260
  params: jobToRun.params
1213
1261
  });
1214
- const result = await job.resolve(jobToRun.params, context);
1262
+ try {
1263
+ executionGuardLease = await config.executionGuard.acquire(jobToRun);
1264
+ } catch (error) {
1265
+ context.clearStaleTimeout();
1266
+ await this.handleExecutionGuardAcquisitionFailure(error, config, jobToRun, context);
1267
+ if (executionStatus === "stale") await staleHandlingPromise;
1268
+ return;
1269
+ }
1270
+ if (executionStatus === "stale") {
1271
+ await staleHandlingPromise;
1272
+ await this.releaseExecutionGuardLease(executionGuardLease, jobToRun, context);
1273
+ return;
1274
+ }
1275
+ }
1276
+ try {
1277
+ if (!config.executionGuard) {
1278
+ (0, import_logger3.updateOrionAsyncContext)({
1279
+ jobName: jobToRun.name,
1280
+ jobId: jobToRun.jobId,
1281
+ params: jobToRun.params
1282
+ });
1283
+ }
1284
+ const result = executionGuardLease ? await this.runGuardedResolver(job, jobToRun, context, executionGuardLease) : await job.resolve(jobToRun.params, context);
1215
1285
  context.clearStaleTimeout();
1216
1286
  if (executionStatus === "stale") {
1217
1287
  await staleHandlingPromise;
@@ -1390,7 +1460,11 @@ var WorkerService = class {
1390
1460
  jobs: config.jobs,
1391
1461
  maxTries: config.maxTries,
1392
1462
  maxTriesReachedRetentionMs: config.maxTriesReachedRetentionMs,
1393
- onMaxTriesReached: config.onMaxTriesReached
1463
+ onMaxTriesReached: config.onMaxTriesReached,
1464
+ ...config.executionGuard ? {
1465
+ executionGuard: config.executionGuard,
1466
+ executionGuardRetryDelayMs: Math.max(config.pollInterval, 1e3)
1467
+ } : {}
1394
1468
  };
1395
1469
  import_logger4.logger.debug(`Running scheduler loop for jobs "${jobNames.join(", ")}"...`);
1396
1470
  let consecutivePartitionMisses = 0;