@orion-js/dogs 4.3.5 → 4.3.6

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.d.cts CHANGED
@@ -1,4 +1,3 @@
1
- import * as _orion_js_schema from '@orion-js/schema';
2
1
  import { SchemaInAnyOrionForm, Blackbox, InferSchemaType } from '@orion-js/schema';
3
2
  import { OrionLogger } from '@orion-js/logger';
4
3
  import { Collection, MongoDB } from '@orion-js/mongodb';
@@ -65,115 +64,16 @@ interface ScheduleJobsResult {
65
64
  }>;
66
65
  }
67
66
 
68
- declare const HistoryRecordSchema: {
69
- _id: {
70
- type: "string";
71
- };
72
- jobId: {
73
- type: "string";
74
- };
75
- executionId: {
76
- type: "string";
77
- };
78
- jobName: {
79
- type: "string";
80
- };
81
- type: {
82
- type: "string";
83
- };
84
- priority: {
85
- type: "number";
86
- };
87
- tries: {
88
- type: "number";
89
- };
90
- uniqueIdentifier: {
91
- type: "string";
92
- optional: true;
93
- };
94
- startedAt: {
95
- type: "date";
96
- };
97
- endedAt: {
98
- type: "date";
99
- };
100
- duration: {
101
- type: "number";
102
- };
103
- expiresAt: {
104
- type: "date";
105
- optional: true;
106
- };
107
- status: {
108
- type: "string";
109
- enum: string[];
110
- };
111
- errorMessage: {
112
- type: "string";
113
- optional: true;
114
- };
115
- params: {
116
- type: "blackbox";
117
- optional: true;
118
- };
119
- result: {
120
- type: "any";
121
- optional: true;
122
- };
123
- };
67
+ declare const HistoryRecordSchema: any;
124
68
  type HistoryRecord = InferSchemaType<typeof HistoryRecordSchema>;
125
69
 
126
70
  /**
127
71
  * Enum representing the status of a job record.
128
72
  * - 'pending': Job is active and can be executed (default for existing records)
129
- * - 'maxTriesReached': Job has exhausted all retry attempts and won't be executed
73
+ * - 'maxTriesReached': Event job has exhausted all retry attempts and won't be executed
130
74
  */
131
- declare const JobStatusEnum: _orion_js_schema.FieldType<"pending" | "maxTriesReached">;
132
- declare const JobRecordSchema: {
133
- _id: {
134
- type: "string";
135
- };
136
- jobName: {
137
- type: "string";
138
- };
139
- type: {
140
- type: _orion_js_schema.FieldType<"recurrent" | "event">;
141
- };
142
- priority: {
143
- type: "number";
144
- };
145
- uniqueIdentifier: {
146
- type: "string";
147
- optional: true;
148
- };
149
- nextRunAt: {
150
- type: "date";
151
- };
152
- lastRunAt: {
153
- type: "date";
154
- optional: true;
155
- };
156
- lockedUntil: {
157
- type: "date";
158
- optional: true;
159
- };
160
- tries: {
161
- type: "number";
162
- optional: true;
163
- };
164
- params: {
165
- type: "blackbox";
166
- optional: true;
167
- };
168
- /**
169
- * Status of the job. Optional for backwards compatibility with existing records.
170
- * Records without this field are treated as 'pending'.
171
- */
172
- status: {
173
- type: _orion_js_schema.FieldType<"pending" | "maxTriesReached">;
174
- optional: true;
175
- };
176
- };
75
+ declare const JobStatusEnum: any;
76
+ declare const JobRecordSchema: any;
177
77
  type JobRecord = InferSchemaType<typeof JobRecordSchema>;
178
78
 
179
79
  interface JobToRun {
@@ -249,12 +149,6 @@ interface BaseJobDefinition {
249
149
  * If not set, the defaultLockTime from config will be used.
250
150
  */
251
151
  lockTime?: number;
252
- /**
253
- * Maximum number of tries for this specific job before it is marked as 'maxTriesReached'.
254
- * Overrides the global maxTries set in startWorkers config.
255
- * If not set, the global maxTries from config will be used.
256
- */
257
- maxTries?: number;
258
152
  }
259
153
  interface RecurrentJobDefinition extends BaseJobDefinition {
260
154
  /**
@@ -291,6 +185,12 @@ interface EventJobDefinition<TParamsSchema extends SchemaInAnyOrionForm = any> e
291
185
  * Type of the job.
292
186
  */
293
187
  type: 'event';
188
+ /**
189
+ * Maximum number of tries for this specific event job before it is marked as 'maxTriesReached'.
190
+ * Overrides the global maxTries set in startWorkers config.
191
+ * If not set, the global maxTries from config will be used.
192
+ */
193
+ maxTries?: number;
294
194
  /**
295
195
  * Schedule of the job. Supports optional runIn (milliseconds) or runAt (Date) for delayed execution.
296
196
  */
@@ -352,16 +252,16 @@ interface StartWorkersConfig {
352
252
  */
353
253
  jobs: JobsDefinition;
354
254
  /**
355
- * Maximum number of tries for a job before it is marked as 'maxTriesReached'.
356
- * This is a required global default that can be overridden per job definition.
255
+ * Maximum number of tries for an event job before it is marked as 'maxTriesReached'.
256
+ * This is a global default that can be overridden per event job definition.
357
257
  */
358
- maxTries: number;
258
+ maxTries?: number;
359
259
  /**
360
- * Callback invoked when a job reaches its maximum tries limit.
260
+ * Callback invoked when an event job reaches its maximum tries limit.
361
261
  * Use this to notify administrators (e.g., send an email alert).
362
262
  * The job will remain in the database with status 'maxTriesReached'.
363
263
  */
364
- onMaxTriesReached: (job: JobToRun) => Promise<void>;
264
+ onMaxTriesReached?: (job: JobToRun) => Promise<void>;
365
265
  /**
366
266
  * Time in milliseconds to wait between each look without results for a job
367
267
  * to run at the database. Default is 3000.
@@ -403,7 +303,7 @@ declare class JobsRepo {
403
303
  }): Promise<void>;
404
304
  deleteEventJob(jobId: string): Promise<void>;
405
305
  /**
406
- * Marks a job as having reached its maximum tries limit.
306
+ * Marks an event job as having reached its maximum tries limit.
407
307
  * The job will remain in the database but won't be picked up for execution.
408
308
  */
409
309
  markJobAsMaxTriesReached(jobId: string): Promise<void>;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import * as _orion_js_schema from '@orion-js/schema';
2
1
  import { SchemaInAnyOrionForm, Blackbox, InferSchemaType } from '@orion-js/schema';
3
2
  import { OrionLogger } from '@orion-js/logger';
4
3
  import { Collection, MongoDB } from '@orion-js/mongodb';
@@ -65,115 +64,16 @@ interface ScheduleJobsResult {
65
64
  }>;
66
65
  }
67
66
 
68
- declare const HistoryRecordSchema: {
69
- _id: {
70
- type: "string";
71
- };
72
- jobId: {
73
- type: "string";
74
- };
75
- executionId: {
76
- type: "string";
77
- };
78
- jobName: {
79
- type: "string";
80
- };
81
- type: {
82
- type: "string";
83
- };
84
- priority: {
85
- type: "number";
86
- };
87
- tries: {
88
- type: "number";
89
- };
90
- uniqueIdentifier: {
91
- type: "string";
92
- optional: true;
93
- };
94
- startedAt: {
95
- type: "date";
96
- };
97
- endedAt: {
98
- type: "date";
99
- };
100
- duration: {
101
- type: "number";
102
- };
103
- expiresAt: {
104
- type: "date";
105
- optional: true;
106
- };
107
- status: {
108
- type: "string";
109
- enum: string[];
110
- };
111
- errorMessage: {
112
- type: "string";
113
- optional: true;
114
- };
115
- params: {
116
- type: "blackbox";
117
- optional: true;
118
- };
119
- result: {
120
- type: "any";
121
- optional: true;
122
- };
123
- };
67
+ declare const HistoryRecordSchema: any;
124
68
  type HistoryRecord = InferSchemaType<typeof HistoryRecordSchema>;
125
69
 
126
70
  /**
127
71
  * Enum representing the status of a job record.
128
72
  * - 'pending': Job is active and can be executed (default for existing records)
129
- * - 'maxTriesReached': Job has exhausted all retry attempts and won't be executed
73
+ * - 'maxTriesReached': Event job has exhausted all retry attempts and won't be executed
130
74
  */
131
- declare const JobStatusEnum: _orion_js_schema.FieldType<"pending" | "maxTriesReached">;
132
- declare const JobRecordSchema: {
133
- _id: {
134
- type: "string";
135
- };
136
- jobName: {
137
- type: "string";
138
- };
139
- type: {
140
- type: _orion_js_schema.FieldType<"recurrent" | "event">;
141
- };
142
- priority: {
143
- type: "number";
144
- };
145
- uniqueIdentifier: {
146
- type: "string";
147
- optional: true;
148
- };
149
- nextRunAt: {
150
- type: "date";
151
- };
152
- lastRunAt: {
153
- type: "date";
154
- optional: true;
155
- };
156
- lockedUntil: {
157
- type: "date";
158
- optional: true;
159
- };
160
- tries: {
161
- type: "number";
162
- optional: true;
163
- };
164
- params: {
165
- type: "blackbox";
166
- optional: true;
167
- };
168
- /**
169
- * Status of the job. Optional for backwards compatibility with existing records.
170
- * Records without this field are treated as 'pending'.
171
- */
172
- status: {
173
- type: _orion_js_schema.FieldType<"pending" | "maxTriesReached">;
174
- optional: true;
175
- };
176
- };
75
+ declare const JobStatusEnum: any;
76
+ declare const JobRecordSchema: any;
177
77
  type JobRecord = InferSchemaType<typeof JobRecordSchema>;
178
78
 
179
79
  interface JobToRun {
@@ -249,12 +149,6 @@ interface BaseJobDefinition {
249
149
  * If not set, the defaultLockTime from config will be used.
250
150
  */
251
151
  lockTime?: number;
252
- /**
253
- * Maximum number of tries for this specific job before it is marked as 'maxTriesReached'.
254
- * Overrides the global maxTries set in startWorkers config.
255
- * If not set, the global maxTries from config will be used.
256
- */
257
- maxTries?: number;
258
152
  }
259
153
  interface RecurrentJobDefinition extends BaseJobDefinition {
260
154
  /**
@@ -291,6 +185,12 @@ interface EventJobDefinition<TParamsSchema extends SchemaInAnyOrionForm = any> e
291
185
  * Type of the job.
292
186
  */
293
187
  type: 'event';
188
+ /**
189
+ * Maximum number of tries for this specific event job before it is marked as 'maxTriesReached'.
190
+ * Overrides the global maxTries set in startWorkers config.
191
+ * If not set, the global maxTries from config will be used.
192
+ */
193
+ maxTries?: number;
294
194
  /**
295
195
  * Schedule of the job. Supports optional runIn (milliseconds) or runAt (Date) for delayed execution.
296
196
  */
@@ -352,16 +252,16 @@ interface StartWorkersConfig {
352
252
  */
353
253
  jobs: JobsDefinition;
354
254
  /**
355
- * Maximum number of tries for a job before it is marked as 'maxTriesReached'.
356
- * This is a required global default that can be overridden per job definition.
255
+ * Maximum number of tries for an event job before it is marked as 'maxTriesReached'.
256
+ * This is a global default that can be overridden per event job definition.
357
257
  */
358
- maxTries: number;
258
+ maxTries?: number;
359
259
  /**
360
- * Callback invoked when a job reaches its maximum tries limit.
260
+ * Callback invoked when an event job reaches its maximum tries limit.
361
261
  * Use this to notify administrators (e.g., send an email alert).
362
262
  * The job will remain in the database with status 'maxTriesReached'.
363
263
  */
364
- onMaxTriesReached: (job: JobToRun) => Promise<void>;
264
+ onMaxTriesReached?: (job: JobToRun) => Promise<void>;
365
265
  /**
366
266
  * Time in milliseconds to wait between each look without results for a job
367
267
  * to run at the database. Default is 3000.
@@ -403,7 +303,7 @@ declare class JobsRepo {
403
303
  }): Promise<void>;
404
304
  deleteEventJob(jobId: string): Promise<void>;
405
305
  /**
406
- * Marks a job as having reached its maximum tries limit.
306
+ * Marks an event job as having reached its maximum tries limit.
407
307
  * The job will remain in the database but won't be picked up for execution.
408
308
  */
409
309
  markJobAsMaxTriesReached(jobId: string): Promise<void>;
package/dist/index.js CHANGED
@@ -124,10 +124,12 @@ var JobsRepo = class {
124
124
  {
125
125
  jobName: { $in: jobNames },
126
126
  nextRunAt: { $lte: /* @__PURE__ */ new Date() },
127
- $or: [{ lockedUntil: { $exists: false } }, { lockedUntil: { $lte: /* @__PURE__ */ new Date() } }],
128
- // Exclude jobs that have reached max tries. Using $ne handles backwards compatibility
129
- // since records without the status field will still match (undefined !== 'maxTriesReached')
130
- status: { $ne: "maxTriesReached" }
127
+ $and: [
128
+ { $or: [{ lockedUntil: { $exists: false } }, { lockedUntil: { $lte: /* @__PURE__ */ new Date() } }] },
129
+ // Exclude event jobs that have reached max tries. Recurrent jobs keep running even
130
+ // if old records still have this status from previous versions.
131
+ { $or: [{ type: { $ne: "event" } }, { status: { $ne: "maxTriesReached" } }] }
132
+ ]
131
133
  },
132
134
  {
133
135
  $set: { lockedUntil, lastRunAt: /* @__PURE__ */ new Date() },
@@ -180,12 +182,12 @@ var JobsRepo = class {
180
182
  await this.jobs.deleteOne({ _id: jobId, type: "event" });
181
183
  }
182
184
  /**
183
- * Marks a job as having reached its maximum tries limit.
185
+ * Marks an event job as having reached its maximum tries limit.
184
186
  * The job will remain in the database but won't be picked up for execution.
185
187
  */
186
188
  async markJobAsMaxTriesReached(jobId) {
187
189
  await this.jobs.updateOne(
188
- { _id: jobId },
190
+ { _id: jobId, type: "event" },
189
191
  {
190
192
  $set: { status: "maxTriesReached" },
191
193
  $unset: { lockedUntil: "" }
@@ -231,6 +233,9 @@ var JobsRepo = class {
231
233
  type: job.type,
232
234
  priority: job.priority
233
235
  },
236
+ $unset: {
237
+ status: ""
238
+ },
234
239
  $setOnInsert: {
235
240
  nextRunAt: /* @__PURE__ */ new Date()
236
241
  }
@@ -553,7 +558,7 @@ var Executor = class {
553
558
  return job;
554
559
  }
555
560
  /**
556
- * Determines the effective max tries for a job.
561
+ * Determines the effective max tries for an event job.
557
562
  * Job-specific maxTries takes precedence over the global maxTries from config.
558
563
  */
559
564
  getEffectiveMaxTries(job, globalMaxTries) {
@@ -561,7 +566,7 @@ var Executor = class {
561
566
  }
562
567
  /**
563
568
  * Handles when a job has reached its maximum retry attempts.
564
- * Marks the job in the database and invokes the onMaxTriesReached callback.
569
+ * Marks the job in the database and invokes the onMaxTriesReached callback when provided.
565
570
  */
566
571
  async handleMaxTriesReached(jobToRun, onMaxTriesReached) {
567
572
  const jobLogger = logger3.addMetadata({
@@ -572,6 +577,7 @@ var Executor = class {
572
577
  `Job "${jobToRun.name}" has exceeded max tries (${jobToRun.tries}). Marking as maxTriesReached.`
573
578
  );
574
579
  await this.jobsRepo.markJobAsMaxTriesReached(jobToRun.jobId);
580
+ if (!onMaxTriesReached) return;
575
581
  try {
576
582
  await onMaxTriesReached(jobToRun);
577
583
  } catch (callbackError) {
@@ -580,7 +586,7 @@ var Executor = class {
580
586
  });
581
587
  }
582
588
  }
583
- async onError(error, job, jobToRun, context, config) {
589
+ async onError(error, job, jobToRun, context) {
584
590
  const scheduleRecurrent = async () => {
585
591
  if (job.type === "recurrent") {
586
592
  await this.jobsRepo.scheduleNextRun({
@@ -661,10 +667,12 @@ var Executor = class {
661
667
  async executeJob(config, jobToRun, respawnWorker) {
662
668
  const job = this.getJobDefinition(jobToRun, config.jobs);
663
669
  if (!job) return;
664
- const effectiveMaxTries = this.getEffectiveMaxTries(job, config.maxTries);
665
- if (jobToRun.tries > effectiveMaxTries) {
666
- await this.handleMaxTriesReached(jobToRun, config.onMaxTriesReached);
667
- return;
670
+ if (job.type === "event") {
671
+ const effectiveMaxTries = this.getEffectiveMaxTries(job, config.maxTries);
672
+ if (typeof effectiveMaxTries === "number" && jobToRun.tries > effectiveMaxTries) {
673
+ await this.handleMaxTriesReached(jobToRun, config.onMaxTriesReached);
674
+ return;
675
+ }
668
676
  }
669
677
  const effectiveLockTime = this.getEffectiveLockTime(job, jobToRun);
670
678
  if (effectiveLockTime !== jobToRun.lockTime) {
@@ -733,7 +741,7 @@ var Executor = class {
733
741
  }).catch((saveError) => {
734
742
  context.logger.error("Error saving failed execution history", { error: saveError });
735
743
  });
736
- await this.onError(error, job, jobToRun, context, config);
744
+ await this.onError(error, job, jobToRun, context);
737
745
  }
738
746
  });
739
747
  } catch (error) {
@@ -920,7 +928,7 @@ var WorkerService = class {
920
928
  }
921
929
  /**
922
930
  * Starts the job workers with the provided configuration.
923
- * @param userConfig - Configuration for the workers. Required fields: jobs, maxTries, onMaxTriesReached
931
+ * @param userConfig - Configuration for the workers. Required field: jobs
924
932
  */
925
933
  startWorkers(userConfig) {
926
934
  const config = {
@@ -992,16 +1000,20 @@ function createEventJob(options) {
992
1000
  return jobDefinition;
993
1001
  }
994
1002
  function createRecurrentJob(options) {
995
- if ("cron" in options && options.cron && !options.timezone) {
1003
+ const recurrentOptions = {
1004
+ ...options
1005
+ };
1006
+ delete recurrentOptions.maxTries;
1007
+ if ("cron" in recurrentOptions && recurrentOptions.cron && !recurrentOptions.timezone) {
996
1008
  throw new Error("Cron recurrent jobs require a timezone");
997
1009
  }
998
- if ("cron" in options && options.cron) {
999
- CronExpressionParser2.parse(options.cron, { tz: options.timezone }).next();
1010
+ if ("cron" in recurrentOptions && recurrentOptions.cron) {
1011
+ CronExpressionParser2.parse(recurrentOptions.cron, { tz: recurrentOptions.timezone }).next();
1000
1012
  }
1001
- const runEvery = "runEvery" in options ? typeof options.runEvery === "string" ? parse(options.runEvery) : options.runEvery : void 0;
1013
+ const runEvery = "runEvery" in recurrentOptions ? typeof recurrentOptions.runEvery === "string" ? parse(recurrentOptions.runEvery) : recurrentOptions.runEvery : void 0;
1002
1014
  const jobDefinition = {
1003
- ...options,
1004
- priority: options.priority ?? 100,
1015
+ ...recurrentOptions,
1016
+ priority: recurrentOptions.priority ?? 100,
1005
1017
  type: "recurrent",
1006
1018
  runEvery
1007
1019
  };