@bernierllc/job-scheduler 1.5.1 → 1.5.4
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/README.md +1 -1
- package/dist/JobScheduler.js +35 -4
- package/dist/ScheduledJob.js +42 -14
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ npm install @bernierllc/job-scheduler
|
|
|
14
14
|
- **One-Time Scheduling** - Schedule jobs to run at specific dates/times
|
|
15
15
|
- **Job Management** - Pause, resume, and cancel jobs
|
|
16
16
|
- **Execution Tracking** - Complete history of job executions with success/failure status
|
|
17
|
-
- **Retry Logic** - Automatic retry with configurable delay for failed jobs
|
|
17
|
+
- **Retry Logic** - Automatic retry with configurable delay for failed jobs. For a recurring job a pending retry replaces the next cron run until the retries run out, and each cron run gets the full `maxRetries` budget. `cancel()`, `pause()`, `shutdown()` and a repeat `start()` clear a pending retry, and a job cancelled mid-run is not retried
|
|
18
18
|
- **Statistics** - Job execution statistics including duration, success rate, and next run time
|
|
19
19
|
- **TypeScript Support** - Full type definitions included
|
|
20
20
|
- **Task Queue Integration** - Optional integration with @bernierllc/task-queue
|
package/dist/JobScheduler.js
CHANGED
|
@@ -62,8 +62,16 @@ class JobScheduler {
|
|
|
62
62
|
retryDelay: options.retryDelay
|
|
63
63
|
};
|
|
64
64
|
const job = new ScheduledJob_1.ScheduledJob(jobConfig, this.logger);
|
|
65
|
+
// Stored before start(): a past-date job runs inside start() and its handler may look itself up.
|
|
65
66
|
this.jobs.set(jobId, job);
|
|
66
|
-
|
|
67
|
+
try {
|
|
68
|
+
job.start();
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
job.cancel(); // clears anything start() armed before it threw
|
|
72
|
+
this.jobs.delete(jobId);
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
67
75
|
this.logger.info('Recurring job scheduled', {
|
|
68
76
|
jobId,
|
|
69
77
|
name: options.name,
|
|
@@ -76,7 +84,11 @@ class JobScheduler {
|
|
|
76
84
|
}
|
|
77
85
|
catch (error) {
|
|
78
86
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
79
|
-
this.logger.error('Failed to schedule recurring job'
|
|
87
|
+
this.logger.error('Failed to schedule recurring job', error instanceof Error ? error : undefined, {
|
|
88
|
+
cause: errorMessage,
|
|
89
|
+
name: options.name,
|
|
90
|
+
cronExpression
|
|
91
|
+
});
|
|
80
92
|
return {
|
|
81
93
|
success: false,
|
|
82
94
|
error: `Failed to schedule job: ${errorMessage}`
|
|
@@ -92,6 +104,13 @@ class JobScheduler {
|
|
|
92
104
|
* @returns Scheduled job instance
|
|
93
105
|
*/
|
|
94
106
|
scheduleAt(date, handler, options = {}) {
|
|
107
|
+
if (!(date instanceof Date) || Number.isNaN(date.getTime())) {
|
|
108
|
+
this.logger.error('Failed to schedule one-time job: invalid date', undefined, { name: options.name });
|
|
109
|
+
return {
|
|
110
|
+
success: false,
|
|
111
|
+
error: 'Failed to schedule job: scheduled time must be a valid Date'
|
|
112
|
+
};
|
|
113
|
+
}
|
|
95
114
|
try {
|
|
96
115
|
const jobId = this.generateJobId();
|
|
97
116
|
const jobConfig = {
|
|
@@ -105,8 +124,16 @@ class JobScheduler {
|
|
|
105
124
|
retryDelay: options.retryDelay
|
|
106
125
|
};
|
|
107
126
|
const job = new ScheduledJob_1.ScheduledJob(jobConfig, this.logger);
|
|
127
|
+
// Stored before start(): a past-date job runs inside start() and its handler may look itself up.
|
|
108
128
|
this.jobs.set(jobId, job);
|
|
109
|
-
|
|
129
|
+
try {
|
|
130
|
+
job.start();
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
job.cancel(); // clears anything start() armed before it threw
|
|
134
|
+
this.jobs.delete(jobId);
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
110
137
|
this.logger.info('One-time job scheduled', {
|
|
111
138
|
jobId,
|
|
112
139
|
name: options.name,
|
|
@@ -119,7 +146,11 @@ class JobScheduler {
|
|
|
119
146
|
}
|
|
120
147
|
catch (error) {
|
|
121
148
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
122
|
-
this.logger.error('Failed to schedule one-time job'
|
|
149
|
+
this.logger.error('Failed to schedule one-time job', error instanceof Error ? error : undefined, {
|
|
150
|
+
cause: errorMessage,
|
|
151
|
+
name: options.name,
|
|
152
|
+
scheduledTime: date.toISOString()
|
|
153
|
+
});
|
|
123
154
|
return {
|
|
124
155
|
success: false,
|
|
125
156
|
error: `Failed to schedule job: ${errorMessage}`
|
package/dist/ScheduledJob.js
CHANGED
|
@@ -79,7 +79,11 @@ class ScheduledJob {
|
|
|
79
79
|
nextExecution = interval.next().toDate();
|
|
80
80
|
}
|
|
81
81
|
catch (error) {
|
|
82
|
-
this.logger.error('Failed to calculate next execution'
|
|
82
|
+
this.logger.error('Failed to calculate next execution', error instanceof Error ? error : undefined, {
|
|
83
|
+
cause: String(error),
|
|
84
|
+
jobId: this.config.id,
|
|
85
|
+
cronExpression: this.config.cronExpression
|
|
86
|
+
});
|
|
83
87
|
}
|
|
84
88
|
}
|
|
85
89
|
else if (this.config.type === types_1.JobType.ONE_TIME && this.config.scheduledTime) {
|
|
@@ -106,6 +110,11 @@ class ScheduledJob {
|
|
|
106
110
|
this.logger.warn('Cannot start cancelled job', { jobId: this.config.id });
|
|
107
111
|
return;
|
|
108
112
|
}
|
|
113
|
+
// A second start() on a scheduled job would otherwise orphan the armed timer.
|
|
114
|
+
if (this.timer) {
|
|
115
|
+
clearTimeout(this.timer);
|
|
116
|
+
this.timer = undefined;
|
|
117
|
+
}
|
|
109
118
|
this.status = types_1.JobStatus.SCHEDULED;
|
|
110
119
|
if (this.config.type === types_1.JobType.RECURRING) {
|
|
111
120
|
if (!this.config.cronExpression) {
|
|
@@ -176,6 +185,7 @@ class ScheduledJob {
|
|
|
176
185
|
};
|
|
177
186
|
this.executions.push(execution);
|
|
178
187
|
this.status = types_1.JobStatus.RUNNING;
|
|
188
|
+
let retryArmed = false;
|
|
179
189
|
this.logger.info('Job execution started', {
|
|
180
190
|
executionId,
|
|
181
191
|
jobId: this.config.id,
|
|
@@ -200,9 +210,15 @@ class ScheduledJob {
|
|
|
200
210
|
execution.status = types_1.JobStatus.FAILED;
|
|
201
211
|
execution.duration = endTime.getTime() - startTime.getTime();
|
|
202
212
|
execution.error = error instanceof Error ? error.message : String(error);
|
|
203
|
-
this.logger.error('Job execution failed'
|
|
213
|
+
this.logger.error('Job execution failed', error instanceof Error ? error : undefined, {
|
|
214
|
+
executionId,
|
|
215
|
+
jobId: this.config.id,
|
|
216
|
+
cause: execution.error
|
|
217
|
+
});
|
|
204
218
|
// Handle retries
|
|
205
|
-
|
|
219
|
+
// A job cancelled or shut down mid-run must not be revived by its retry.
|
|
220
|
+
if (this.status === types_1.JobStatus.RUNNING &&
|
|
221
|
+
this.config.maxRetries &&
|
|
206
222
|
this.currentRetries < this.config.maxRetries) {
|
|
207
223
|
this.currentRetries++;
|
|
208
224
|
const retryDelay = this.config.retryDelay || 5000;
|
|
@@ -212,21 +228,27 @@ class ScheduledJob {
|
|
|
212
228
|
maxRetries: this.config.maxRetries,
|
|
213
229
|
delay: retryDelay
|
|
214
230
|
});
|
|
215
|
-
|
|
231
|
+
// Held in this.timer so cancel(), pause() and shutdown() can clear it.
|
|
232
|
+
this.timer = setTimeout(() => {
|
|
216
233
|
void this.executeJob();
|
|
217
234
|
}, retryDelay);
|
|
235
|
+
retryArmed = true;
|
|
218
236
|
}
|
|
219
237
|
}
|
|
220
238
|
finally {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
239
|
+
// A cancel() during the handler run must stick, so only a still-running job re-arms.
|
|
240
|
+
if (this.status === types_1.JobStatus.RUNNING) {
|
|
241
|
+
if (this.config.type === types_1.JobType.RECURRING) {
|
|
242
|
+
this.status = types_1.JobStatus.SCHEDULED;
|
|
243
|
+
// A pending retry replaces the next cron run until the retries run out.
|
|
244
|
+
if (!retryArmed) {
|
|
245
|
+
this.scheduleRecurring();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
else if (this.config.type === types_1.JobType.ONE_TIME) {
|
|
249
|
+
this.status = types_1.JobStatus.COMPLETED;
|
|
250
|
+
this.logger.info('One-time job completed', { jobId: this.config.id });
|
|
251
|
+
}
|
|
230
252
|
}
|
|
231
253
|
}
|
|
232
254
|
}
|
|
@@ -242,6 +264,8 @@ class ScheduledJob {
|
|
|
242
264
|
const nextRun = interval.next().toDate();
|
|
243
265
|
const delay = nextRun.getTime() - Date.now();
|
|
244
266
|
this.timer = setTimeout(() => {
|
|
267
|
+
// Each cron run gets the full retry budget.
|
|
268
|
+
this.currentRetries = 0;
|
|
245
269
|
void this.executeJob();
|
|
246
270
|
}, delay);
|
|
247
271
|
this.logger.debug('Recurring job scheduled', {
|
|
@@ -251,7 +275,11 @@ class ScheduledJob {
|
|
|
251
275
|
});
|
|
252
276
|
}
|
|
253
277
|
catch (error) {
|
|
254
|
-
this.logger.error('Failed to schedule recurring job'
|
|
278
|
+
this.logger.error('Failed to schedule recurring job', error instanceof Error ? error : undefined, {
|
|
279
|
+
cause: String(error),
|
|
280
|
+
jobId: this.config.id,
|
|
281
|
+
cronExpression: this.config.cronExpression
|
|
282
|
+
});
|
|
255
283
|
throw error;
|
|
256
284
|
}
|
|
257
285
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bernierllc/job-scheduler",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.4",
|
|
4
4
|
"description": "Cron-based job scheduling for recurring tasks with execution tracking",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"license": "SEE LICENSE IN LICENSE",
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"cron-parser": "^4.9.0",
|
|
25
|
-
"@bernierllc/logger": "
|
|
25
|
+
"@bernierllc/logger": "2.0.1"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@types/jest": "^29.0.0",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"build": "tsc",
|
|
61
61
|
"prebuild": "npm run clean",
|
|
62
62
|
"clean": "rimraf dist",
|
|
63
|
-
"test": "jest
|
|
63
|
+
"test": "jest",
|
|
64
64
|
"test:watch": "jest --watch",
|
|
65
65
|
"test:coverage": "jest --coverage",
|
|
66
66
|
"test:run": "jest",
|