@bernierllc/job-scheduler 1.5.2 → 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.d.ts +126 -0
- package/dist/JobScheduler.js +329 -0
- package/dist/ScheduledJob.d.ts +70 -0
- package/dist/ScheduledJob.js +312 -0
- package/dist/errors.d.ts +11 -0
- package/dist/errors.js +24 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +20 -0
- package/dist/types.d.ts +81 -0
- package/dist/types.js +30 -0
- 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
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { ScheduledJob } from './ScheduledJob';
|
|
2
|
+
import { JobHandler, JobSchedulerConfig, JobSchedulerResult, JobStatus, JobStatistics } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Job scheduler for cron-based recurring tasks and one-time scheduled jobs
|
|
5
|
+
*/
|
|
6
|
+
export declare class JobScheduler {
|
|
7
|
+
private readonly jobs;
|
|
8
|
+
private readonly logger;
|
|
9
|
+
private readonly config;
|
|
10
|
+
private nextJobId;
|
|
11
|
+
constructor(config?: JobSchedulerConfig);
|
|
12
|
+
/**
|
|
13
|
+
* Schedule a recurring job with cron expression
|
|
14
|
+
*
|
|
15
|
+
* @param cronExpression - Cron expression (e.g., '0 9 * * *' for daily at 9 AM)
|
|
16
|
+
* @param handler - Function to execute
|
|
17
|
+
* @param options - Additional job options
|
|
18
|
+
* @returns Scheduled job instance
|
|
19
|
+
*/
|
|
20
|
+
schedule(cronExpression: string, handler: JobHandler, options?: {
|
|
21
|
+
name?: string;
|
|
22
|
+
metadata?: Record<string, unknown>;
|
|
23
|
+
maxRetries?: number;
|
|
24
|
+
retryDelay?: number;
|
|
25
|
+
}): JobSchedulerResult<ScheduledJob>;
|
|
26
|
+
/**
|
|
27
|
+
* Schedule a one-time job at specific date/time
|
|
28
|
+
*
|
|
29
|
+
* @param date - Date/time to execute the job
|
|
30
|
+
* @param handler - Function to execute
|
|
31
|
+
* @param options - Additional job options
|
|
32
|
+
* @returns Scheduled job instance
|
|
33
|
+
*/
|
|
34
|
+
scheduleAt(date: Date, handler: JobHandler, options?: {
|
|
35
|
+
name?: string;
|
|
36
|
+
metadata?: Record<string, unknown>;
|
|
37
|
+
maxRetries?: number;
|
|
38
|
+
retryDelay?: number;
|
|
39
|
+
}): JobSchedulerResult<ScheduledJob>;
|
|
40
|
+
/**
|
|
41
|
+
* Cancel a scheduled job
|
|
42
|
+
*
|
|
43
|
+
* @param jobId - Job ID to cancel
|
|
44
|
+
* @returns Result indicating success or failure
|
|
45
|
+
*/
|
|
46
|
+
cancel(jobId: string): JobSchedulerResult<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Pause a scheduled job
|
|
49
|
+
*
|
|
50
|
+
* @param jobId - Job ID to pause
|
|
51
|
+
* @returns Result indicating success or failure
|
|
52
|
+
*/
|
|
53
|
+
pause(jobId: string): JobSchedulerResult<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Resume a paused job
|
|
56
|
+
*
|
|
57
|
+
* @param jobId - Job ID to resume
|
|
58
|
+
* @returns Result indicating success or failure
|
|
59
|
+
*/
|
|
60
|
+
resume(jobId: string): JobSchedulerResult<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Get a scheduled job by ID
|
|
63
|
+
*
|
|
64
|
+
* @param jobId - Job ID
|
|
65
|
+
* @returns Job instance or undefined
|
|
66
|
+
*/
|
|
67
|
+
getJob(jobId: string): ScheduledJob | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* Get all scheduled jobs
|
|
70
|
+
*
|
|
71
|
+
* @returns Array of all jobs
|
|
72
|
+
*/
|
|
73
|
+
getAllJobs(): ScheduledJob[];
|
|
74
|
+
/**
|
|
75
|
+
* Get jobs by status
|
|
76
|
+
*
|
|
77
|
+
* @param status - Job status to filter by
|
|
78
|
+
* @returns Array of jobs with the specified status
|
|
79
|
+
*/
|
|
80
|
+
getJobsByStatus(status: JobStatus): ScheduledJob[];
|
|
81
|
+
/**
|
|
82
|
+
* Get job statistics
|
|
83
|
+
*
|
|
84
|
+
* @param jobId - Job ID
|
|
85
|
+
* @returns Job statistics or undefined
|
|
86
|
+
*/
|
|
87
|
+
getJobStatistics(jobId: string): JobStatistics | undefined;
|
|
88
|
+
/**
|
|
89
|
+
* Cancel all jobs
|
|
90
|
+
*/
|
|
91
|
+
cancelAll(): void;
|
|
92
|
+
/**
|
|
93
|
+
* Get scheduler statistics
|
|
94
|
+
*
|
|
95
|
+
* @returns Overall scheduler statistics
|
|
96
|
+
*/
|
|
97
|
+
getSchedulerStatistics(): {
|
|
98
|
+
totalJobs: number;
|
|
99
|
+
activeJobs: number;
|
|
100
|
+
pausedJobs: number;
|
|
101
|
+
completedJobs: number;
|
|
102
|
+
failedJobs: number;
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* Validate cron expression
|
|
106
|
+
*
|
|
107
|
+
* @param cronExpression - Cron expression to validate
|
|
108
|
+
* @returns Result indicating if expression is valid
|
|
109
|
+
*/
|
|
110
|
+
validateCronExpression(cronExpression: string): JobSchedulerResult<void>;
|
|
111
|
+
/**
|
|
112
|
+
* Generate unique job ID
|
|
113
|
+
*/
|
|
114
|
+
private generateJobId;
|
|
115
|
+
/**
|
|
116
|
+
* Shutdown scheduler and cancel all jobs
|
|
117
|
+
*/
|
|
118
|
+
shutdown(): void;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Create a new job scheduler instance
|
|
122
|
+
*
|
|
123
|
+
* @param config - Scheduler configuration
|
|
124
|
+
* @returns JobScheduler instance
|
|
125
|
+
*/
|
|
126
|
+
export declare function createJobScheduler(config?: JobSchedulerConfig): JobScheduler;
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
Copyright (c) 2025 Bernier LLC
|
|
4
|
+
|
|
5
|
+
This file is licensed to the client under a limited-use license.
|
|
6
|
+
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
7
|
+
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.JobScheduler = void 0;
|
|
11
|
+
exports.createJobScheduler = createJobScheduler;
|
|
12
|
+
const cron_parser_1 = require("cron-parser");
|
|
13
|
+
const logger_1 = require("@bernierllc/logger");
|
|
14
|
+
const ScheduledJob_1 = require("./ScheduledJob");
|
|
15
|
+
const types_1 = require("./types");
|
|
16
|
+
/**
|
|
17
|
+
* Job scheduler for cron-based recurring tasks and one-time scheduled jobs
|
|
18
|
+
*/
|
|
19
|
+
class JobScheduler {
|
|
20
|
+
jobs;
|
|
21
|
+
logger;
|
|
22
|
+
config;
|
|
23
|
+
nextJobId = 1;
|
|
24
|
+
constructor(config = {}) {
|
|
25
|
+
this.jobs = new Map();
|
|
26
|
+
this.logger = (0, logger_1.createLogger)();
|
|
27
|
+
this.config = {
|
|
28
|
+
timezone: 'UTC',
|
|
29
|
+
maxConcurrentJobs: 10,
|
|
30
|
+
enableHistory: true,
|
|
31
|
+
historyRetention: 100,
|
|
32
|
+
enableTaskQueue: false,
|
|
33
|
+
...config
|
|
34
|
+
};
|
|
35
|
+
// Task queue integration is optional and not implemented in this version
|
|
36
|
+
if (this.config.enableTaskQueue) {
|
|
37
|
+
this.logger.info('Task queue integration requested but not implemented in this version');
|
|
38
|
+
}
|
|
39
|
+
this.logger.info('JobScheduler initialized');
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Schedule a recurring job with cron expression
|
|
43
|
+
*
|
|
44
|
+
* @param cronExpression - Cron expression (e.g., '0 9 * * *' for daily at 9 AM)
|
|
45
|
+
* @param handler - Function to execute
|
|
46
|
+
* @param options - Additional job options
|
|
47
|
+
* @returns Scheduled job instance
|
|
48
|
+
*/
|
|
49
|
+
schedule(cronExpression, handler, options = {}) {
|
|
50
|
+
try {
|
|
51
|
+
// Validate cron expression
|
|
52
|
+
(0, cron_parser_1.parseExpression)(cronExpression);
|
|
53
|
+
const jobId = this.generateJobId();
|
|
54
|
+
const jobConfig = {
|
|
55
|
+
id: jobId,
|
|
56
|
+
type: types_1.JobType.RECURRING,
|
|
57
|
+
handler,
|
|
58
|
+
cronExpression,
|
|
59
|
+
name: options.name,
|
|
60
|
+
metadata: options.metadata,
|
|
61
|
+
maxRetries: options.maxRetries,
|
|
62
|
+
retryDelay: options.retryDelay
|
|
63
|
+
};
|
|
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.
|
|
66
|
+
this.jobs.set(jobId, job);
|
|
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
|
+
}
|
|
75
|
+
this.logger.info('Recurring job scheduled', {
|
|
76
|
+
jobId,
|
|
77
|
+
name: options.name,
|
|
78
|
+
cronExpression
|
|
79
|
+
});
|
|
80
|
+
return {
|
|
81
|
+
success: true,
|
|
82
|
+
data: job
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
87
|
+
this.logger.error('Failed to schedule recurring job', error instanceof Error ? error : undefined, {
|
|
88
|
+
cause: errorMessage,
|
|
89
|
+
name: options.name,
|
|
90
|
+
cronExpression
|
|
91
|
+
});
|
|
92
|
+
return {
|
|
93
|
+
success: false,
|
|
94
|
+
error: `Failed to schedule job: ${errorMessage}`
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Schedule a one-time job at specific date/time
|
|
100
|
+
*
|
|
101
|
+
* @param date - Date/time to execute the job
|
|
102
|
+
* @param handler - Function to execute
|
|
103
|
+
* @param options - Additional job options
|
|
104
|
+
* @returns Scheduled job instance
|
|
105
|
+
*/
|
|
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
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
const jobId = this.generateJobId();
|
|
116
|
+
const jobConfig = {
|
|
117
|
+
id: jobId,
|
|
118
|
+
type: types_1.JobType.ONE_TIME,
|
|
119
|
+
handler,
|
|
120
|
+
scheduledTime: date,
|
|
121
|
+
name: options.name,
|
|
122
|
+
metadata: options.metadata,
|
|
123
|
+
maxRetries: options.maxRetries,
|
|
124
|
+
retryDelay: options.retryDelay
|
|
125
|
+
};
|
|
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.
|
|
128
|
+
this.jobs.set(jobId, job);
|
|
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
|
+
}
|
|
137
|
+
this.logger.info('One-time job scheduled', {
|
|
138
|
+
jobId,
|
|
139
|
+
name: options.name,
|
|
140
|
+
scheduledTime: date.toISOString()
|
|
141
|
+
});
|
|
142
|
+
return {
|
|
143
|
+
success: true,
|
|
144
|
+
data: job
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
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
|
+
});
|
|
154
|
+
return {
|
|
155
|
+
success: false,
|
|
156
|
+
error: `Failed to schedule job: ${errorMessage}`
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Cancel a scheduled job
|
|
162
|
+
*
|
|
163
|
+
* @param jobId - Job ID to cancel
|
|
164
|
+
* @returns Result indicating success or failure
|
|
165
|
+
*/
|
|
166
|
+
cancel(jobId) {
|
|
167
|
+
const job = this.jobs.get(jobId);
|
|
168
|
+
if (!job) {
|
|
169
|
+
return {
|
|
170
|
+
success: false,
|
|
171
|
+
error: `Job not found: ${jobId}`
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
job.cancel();
|
|
175
|
+
this.jobs.delete(jobId);
|
|
176
|
+
this.logger.info('Job cancelled and removed', { jobId });
|
|
177
|
+
return { success: true };
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Pause a scheduled job
|
|
181
|
+
*
|
|
182
|
+
* @param jobId - Job ID to pause
|
|
183
|
+
* @returns Result indicating success or failure
|
|
184
|
+
*/
|
|
185
|
+
pause(jobId) {
|
|
186
|
+
const job = this.jobs.get(jobId);
|
|
187
|
+
if (!job) {
|
|
188
|
+
return {
|
|
189
|
+
success: false,
|
|
190
|
+
error: `Job not found: ${jobId}`
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
job.pause();
|
|
194
|
+
this.logger.info('Job paused', { jobId });
|
|
195
|
+
return { success: true };
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Resume a paused job
|
|
199
|
+
*
|
|
200
|
+
* @param jobId - Job ID to resume
|
|
201
|
+
* @returns Result indicating success or failure
|
|
202
|
+
*/
|
|
203
|
+
resume(jobId) {
|
|
204
|
+
const job = this.jobs.get(jobId);
|
|
205
|
+
if (!job) {
|
|
206
|
+
return {
|
|
207
|
+
success: false,
|
|
208
|
+
error: `Job not found: ${jobId}`
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
if (job.getStatus() !== types_1.JobStatus.PAUSED) {
|
|
212
|
+
return {
|
|
213
|
+
success: false,
|
|
214
|
+
error: `Job is not paused: ${jobId}`
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
job.resume();
|
|
218
|
+
this.logger.info('Job resumed', { jobId });
|
|
219
|
+
return { success: true };
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Get a scheduled job by ID
|
|
223
|
+
*
|
|
224
|
+
* @param jobId - Job ID
|
|
225
|
+
* @returns Job instance or undefined
|
|
226
|
+
*/
|
|
227
|
+
getJob(jobId) {
|
|
228
|
+
return this.jobs.get(jobId);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Get all scheduled jobs
|
|
232
|
+
*
|
|
233
|
+
* @returns Array of all jobs
|
|
234
|
+
*/
|
|
235
|
+
getAllJobs() {
|
|
236
|
+
return Array.from(this.jobs.values());
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Get jobs by status
|
|
240
|
+
*
|
|
241
|
+
* @param status - Job status to filter by
|
|
242
|
+
* @returns Array of jobs with the specified status
|
|
243
|
+
*/
|
|
244
|
+
getJobsByStatus(status) {
|
|
245
|
+
return Array.from(this.jobs.values()).filter((job) => job.getStatus() === status);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Get job statistics
|
|
249
|
+
*
|
|
250
|
+
* @param jobId - Job ID
|
|
251
|
+
* @returns Job statistics or undefined
|
|
252
|
+
*/
|
|
253
|
+
getJobStatistics(jobId) {
|
|
254
|
+
const job = this.jobs.get(jobId);
|
|
255
|
+
return job?.getStatistics();
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Cancel all jobs
|
|
259
|
+
*/
|
|
260
|
+
cancelAll() {
|
|
261
|
+
const jobIds = Array.from(this.jobs.keys());
|
|
262
|
+
for (const jobId of jobIds) {
|
|
263
|
+
this.cancel(jobId);
|
|
264
|
+
}
|
|
265
|
+
this.logger.info('All jobs cancelled', { count: jobIds.length });
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Get scheduler statistics
|
|
269
|
+
*
|
|
270
|
+
* @returns Overall scheduler statistics
|
|
271
|
+
*/
|
|
272
|
+
getSchedulerStatistics() {
|
|
273
|
+
const jobs = Array.from(this.jobs.values());
|
|
274
|
+
return {
|
|
275
|
+
totalJobs: jobs.length,
|
|
276
|
+
activeJobs: jobs.filter((j) => j.getStatus() === types_1.JobStatus.SCHEDULED ||
|
|
277
|
+
j.getStatus() === types_1.JobStatus.RUNNING).length,
|
|
278
|
+
pausedJobs: jobs.filter((j) => j.getStatus() === types_1.JobStatus.PAUSED).length,
|
|
279
|
+
completedJobs: jobs.filter((j) => j.getStatus() === types_1.JobStatus.COMPLETED)
|
|
280
|
+
.length,
|
|
281
|
+
failedJobs: jobs.filter((j) => j.getStatus() === types_1.JobStatus.FAILED).length
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Validate cron expression
|
|
286
|
+
*
|
|
287
|
+
* @param cronExpression - Cron expression to validate
|
|
288
|
+
* @returns Result indicating if expression is valid
|
|
289
|
+
*/
|
|
290
|
+
validateCronExpression(cronExpression) {
|
|
291
|
+
try {
|
|
292
|
+
(0, cron_parser_1.parseExpression)(cronExpression);
|
|
293
|
+
return { success: true };
|
|
294
|
+
}
|
|
295
|
+
catch (error) {
|
|
296
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
297
|
+
return {
|
|
298
|
+
success: false,
|
|
299
|
+
error: `Invalid cron expression: ${errorMessage}`
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Generate unique job ID
|
|
305
|
+
*/
|
|
306
|
+
generateJobId() {
|
|
307
|
+
const id = `job-${this.nextJobId}`;
|
|
308
|
+
this.nextJobId++;
|
|
309
|
+
return id;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Shutdown scheduler and cancel all jobs
|
|
313
|
+
*/
|
|
314
|
+
shutdown() {
|
|
315
|
+
this.logger.info('Shutting down scheduler');
|
|
316
|
+
this.cancelAll();
|
|
317
|
+
this.logger.info('Scheduler shutdown complete');
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
exports.JobScheduler = JobScheduler;
|
|
321
|
+
/**
|
|
322
|
+
* Create a new job scheduler instance
|
|
323
|
+
*
|
|
324
|
+
* @param config - Scheduler configuration
|
|
325
|
+
* @returns JobScheduler instance
|
|
326
|
+
*/
|
|
327
|
+
function createJobScheduler(config) {
|
|
328
|
+
return new JobScheduler(config);
|
|
329
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Logger } from '@bernierllc/logger';
|
|
2
|
+
import { JobStatus, JobType, ScheduledJobConfig, JobExecution, JobStatistics } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Represents a scheduled job with execution tracking
|
|
5
|
+
*/
|
|
6
|
+
export declare class ScheduledJob {
|
|
7
|
+
private readonly config;
|
|
8
|
+
private status;
|
|
9
|
+
private readonly executions;
|
|
10
|
+
private timer?;
|
|
11
|
+
private readonly logger;
|
|
12
|
+
private currentRetries;
|
|
13
|
+
constructor(config: ScheduledJobConfig, logger: Logger);
|
|
14
|
+
/**
|
|
15
|
+
* Get job ID
|
|
16
|
+
*/
|
|
17
|
+
getId(): string;
|
|
18
|
+
/**
|
|
19
|
+
* Get job name
|
|
20
|
+
*/
|
|
21
|
+
getName(): string;
|
|
22
|
+
/**
|
|
23
|
+
* Get job type
|
|
24
|
+
*/
|
|
25
|
+
getType(): JobType;
|
|
26
|
+
/**
|
|
27
|
+
* Get job status
|
|
28
|
+
*/
|
|
29
|
+
getStatus(): JobStatus;
|
|
30
|
+
/**
|
|
31
|
+
* Get job configuration
|
|
32
|
+
*/
|
|
33
|
+
getConfig(): Readonly<ScheduledJobConfig>;
|
|
34
|
+
/**
|
|
35
|
+
* Get job execution history
|
|
36
|
+
*/
|
|
37
|
+
getExecutions(): readonly JobExecution[];
|
|
38
|
+
/**
|
|
39
|
+
* Get job statistics
|
|
40
|
+
*/
|
|
41
|
+
getStatistics(): JobStatistics;
|
|
42
|
+
/**
|
|
43
|
+
* Start job execution
|
|
44
|
+
*/
|
|
45
|
+
start(): void;
|
|
46
|
+
/**
|
|
47
|
+
* Pause job execution
|
|
48
|
+
*/
|
|
49
|
+
pause(): void;
|
|
50
|
+
/**
|
|
51
|
+
* Resume job execution
|
|
52
|
+
*/
|
|
53
|
+
resume(): void;
|
|
54
|
+
/**
|
|
55
|
+
* Cancel job execution
|
|
56
|
+
*/
|
|
57
|
+
cancel(): void;
|
|
58
|
+
/**
|
|
59
|
+
* Execute job handler
|
|
60
|
+
*/
|
|
61
|
+
private executeJob;
|
|
62
|
+
/**
|
|
63
|
+
* Schedule recurring job
|
|
64
|
+
*/
|
|
65
|
+
private scheduleRecurring;
|
|
66
|
+
/**
|
|
67
|
+
* Schedule one-time job
|
|
68
|
+
*/
|
|
69
|
+
private scheduleOneTime;
|
|
70
|
+
}
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
Copyright (c) 2025 Bernier LLC
|
|
4
|
+
|
|
5
|
+
This file is licensed to the client under a limited-use license.
|
|
6
|
+
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
7
|
+
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.ScheduledJob = void 0;
|
|
11
|
+
const cron_parser_1 = require("cron-parser");
|
|
12
|
+
const types_1 = require("./types");
|
|
13
|
+
/**
|
|
14
|
+
* Represents a scheduled job with execution tracking
|
|
15
|
+
*/
|
|
16
|
+
class ScheduledJob {
|
|
17
|
+
config;
|
|
18
|
+
status;
|
|
19
|
+
executions;
|
|
20
|
+
timer;
|
|
21
|
+
logger;
|
|
22
|
+
currentRetries = 0;
|
|
23
|
+
constructor(config, logger) {
|
|
24
|
+
this.config = config;
|
|
25
|
+
this.status = types_1.JobStatus.SCHEDULED;
|
|
26
|
+
this.executions = [];
|
|
27
|
+
this.logger = logger;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get job ID
|
|
31
|
+
*/
|
|
32
|
+
getId() {
|
|
33
|
+
return this.config.id;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Get job name
|
|
37
|
+
*/
|
|
38
|
+
getName() {
|
|
39
|
+
return this.config.name || this.config.id;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get job type
|
|
43
|
+
*/
|
|
44
|
+
getType() {
|
|
45
|
+
return this.config.type;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get job status
|
|
49
|
+
*/
|
|
50
|
+
getStatus() {
|
|
51
|
+
return this.status;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Get job configuration
|
|
55
|
+
*/
|
|
56
|
+
getConfig() {
|
|
57
|
+
return Object.freeze({ ...this.config });
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Get job execution history
|
|
61
|
+
*/
|
|
62
|
+
getExecutions() {
|
|
63
|
+
return Object.freeze([...this.executions]);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Get job statistics
|
|
67
|
+
*/
|
|
68
|
+
getStatistics() {
|
|
69
|
+
const successfulExecutions = this.executions.filter((e) => e.status === types_1.JobStatus.COMPLETED);
|
|
70
|
+
const failedExecutions = this.executions.filter((e) => e.status === types_1.JobStatus.FAILED);
|
|
71
|
+
const totalDuration = this.executions.reduce((sum, e) => sum + (e.duration || 0), 0);
|
|
72
|
+
const lastExecution = this.executions.length > 0
|
|
73
|
+
? this.executions[this.executions.length - 1].startTime
|
|
74
|
+
: undefined;
|
|
75
|
+
let nextExecution;
|
|
76
|
+
if (this.config.type === types_1.JobType.RECURRING && this.config.cronExpression) {
|
|
77
|
+
try {
|
|
78
|
+
const interval = (0, cron_parser_1.parseExpression)(this.config.cronExpression);
|
|
79
|
+
nextExecution = interval.next().toDate();
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
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
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else if (this.config.type === types_1.JobType.ONE_TIME && this.config.scheduledTime) {
|
|
90
|
+
nextExecution = this.config.scheduledTime;
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
totalExecutions: this.executions.length,
|
|
94
|
+
successfulExecutions: successfulExecutions.length,
|
|
95
|
+
failedExecutions: failedExecutions.length,
|
|
96
|
+
averageDuration: this.executions.length > 0 ? totalDuration / this.executions.length : 0,
|
|
97
|
+
lastExecution,
|
|
98
|
+
nextExecution
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Start job execution
|
|
103
|
+
*/
|
|
104
|
+
start() {
|
|
105
|
+
if (this.status === types_1.JobStatus.RUNNING) {
|
|
106
|
+
this.logger.warn('Job is already running', { jobId: this.config.id });
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (this.status === types_1.JobStatus.CANCELLED) {
|
|
110
|
+
this.logger.warn('Cannot start cancelled job', { jobId: this.config.id });
|
|
111
|
+
return;
|
|
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
|
+
}
|
|
118
|
+
this.status = types_1.JobStatus.SCHEDULED;
|
|
119
|
+
if (this.config.type === types_1.JobType.RECURRING) {
|
|
120
|
+
if (!this.config.cronExpression) {
|
|
121
|
+
throw new Error('Cron expression is required for recurring jobs');
|
|
122
|
+
}
|
|
123
|
+
this.scheduleRecurring();
|
|
124
|
+
}
|
|
125
|
+
else if (this.config.type === types_1.JobType.ONE_TIME) {
|
|
126
|
+
if (!this.config.scheduledTime) {
|
|
127
|
+
throw new Error('Scheduled time is required for one-time jobs');
|
|
128
|
+
}
|
|
129
|
+
this.scheduleOneTime();
|
|
130
|
+
}
|
|
131
|
+
this.logger.info('Job started', {
|
|
132
|
+
jobId: this.config.id,
|
|
133
|
+
type: this.config.type,
|
|
134
|
+
name: this.config.name
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Pause job execution
|
|
139
|
+
*/
|
|
140
|
+
pause() {
|
|
141
|
+
if (this.status === types_1.JobStatus.RUNNING) {
|
|
142
|
+
this.logger.warn('Cannot pause running job', { jobId: this.config.id });
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (this.timer) {
|
|
146
|
+
clearTimeout(this.timer);
|
|
147
|
+
this.timer = undefined;
|
|
148
|
+
}
|
|
149
|
+
this.status = types_1.JobStatus.PAUSED;
|
|
150
|
+
this.logger.info('Job paused', { jobId: this.config.id });
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Resume job execution
|
|
154
|
+
*/
|
|
155
|
+
resume() {
|
|
156
|
+
if (this.status !== types_1.JobStatus.PAUSED) {
|
|
157
|
+
this.logger.warn('Job is not paused', { jobId: this.config.id });
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
this.start();
|
|
161
|
+
this.logger.info('Job resumed', { jobId: this.config.id });
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Cancel job execution
|
|
165
|
+
*/
|
|
166
|
+
cancel() {
|
|
167
|
+
if (this.timer) {
|
|
168
|
+
clearTimeout(this.timer);
|
|
169
|
+
this.timer = undefined;
|
|
170
|
+
}
|
|
171
|
+
this.status = types_1.JobStatus.CANCELLED;
|
|
172
|
+
this.logger.info('Job cancelled', { jobId: this.config.id });
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Execute job handler
|
|
176
|
+
*/
|
|
177
|
+
async executeJob() {
|
|
178
|
+
const executionId = `${this.config.id}-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
179
|
+
const startTime = new Date();
|
|
180
|
+
const execution = {
|
|
181
|
+
executionId,
|
|
182
|
+
jobId: this.config.id,
|
|
183
|
+
startTime,
|
|
184
|
+
status: types_1.JobStatus.RUNNING
|
|
185
|
+
};
|
|
186
|
+
this.executions.push(execution);
|
|
187
|
+
this.status = types_1.JobStatus.RUNNING;
|
|
188
|
+
let retryArmed = false;
|
|
189
|
+
this.logger.info('Job execution started', {
|
|
190
|
+
executionId,
|
|
191
|
+
jobId: this.config.id,
|
|
192
|
+
name: this.config.name
|
|
193
|
+
});
|
|
194
|
+
try {
|
|
195
|
+
await this.config.handler();
|
|
196
|
+
const endTime = new Date();
|
|
197
|
+
execution.endTime = endTime;
|
|
198
|
+
execution.status = types_1.JobStatus.COMPLETED;
|
|
199
|
+
execution.duration = endTime.getTime() - startTime.getTime();
|
|
200
|
+
this.currentRetries = 0;
|
|
201
|
+
this.logger.info('Job execution completed', {
|
|
202
|
+
executionId,
|
|
203
|
+
jobId: this.config.id,
|
|
204
|
+
duration: execution.duration
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
const endTime = new Date();
|
|
209
|
+
execution.endTime = endTime;
|
|
210
|
+
execution.status = types_1.JobStatus.FAILED;
|
|
211
|
+
execution.duration = endTime.getTime() - startTime.getTime();
|
|
212
|
+
execution.error = error instanceof Error ? error.message : String(error);
|
|
213
|
+
this.logger.error('Job execution failed', error instanceof Error ? error : undefined, {
|
|
214
|
+
executionId,
|
|
215
|
+
jobId: this.config.id,
|
|
216
|
+
cause: execution.error
|
|
217
|
+
});
|
|
218
|
+
// Handle retries
|
|
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 &&
|
|
222
|
+
this.currentRetries < this.config.maxRetries) {
|
|
223
|
+
this.currentRetries++;
|
|
224
|
+
const retryDelay = this.config.retryDelay || 5000;
|
|
225
|
+
this.logger.info('Scheduling job retry', {
|
|
226
|
+
jobId: this.config.id,
|
|
227
|
+
retry: this.currentRetries,
|
|
228
|
+
maxRetries: this.config.maxRetries,
|
|
229
|
+
delay: retryDelay
|
|
230
|
+
});
|
|
231
|
+
// Held in this.timer so cancel(), pause() and shutdown() can clear it.
|
|
232
|
+
this.timer = setTimeout(() => {
|
|
233
|
+
void this.executeJob();
|
|
234
|
+
}, retryDelay);
|
|
235
|
+
retryArmed = true;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
finally {
|
|
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
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Schedule recurring job
|
|
257
|
+
*/
|
|
258
|
+
scheduleRecurring() {
|
|
259
|
+
if (!this.config.cronExpression) {
|
|
260
|
+
throw new Error('Cron expression is required for recurring jobs');
|
|
261
|
+
}
|
|
262
|
+
try {
|
|
263
|
+
const interval = (0, cron_parser_1.parseExpression)(this.config.cronExpression);
|
|
264
|
+
const nextRun = interval.next().toDate();
|
|
265
|
+
const delay = nextRun.getTime() - Date.now();
|
|
266
|
+
this.timer = setTimeout(() => {
|
|
267
|
+
// Each cron run gets the full retry budget.
|
|
268
|
+
this.currentRetries = 0;
|
|
269
|
+
void this.executeJob();
|
|
270
|
+
}, delay);
|
|
271
|
+
this.logger.debug('Recurring job scheduled', {
|
|
272
|
+
jobId: this.config.id,
|
|
273
|
+
nextRun: nextRun.toISOString(),
|
|
274
|
+
delay
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
catch (error) {
|
|
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
|
+
});
|
|
283
|
+
throw error;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Schedule one-time job
|
|
288
|
+
*/
|
|
289
|
+
scheduleOneTime() {
|
|
290
|
+
if (!this.config.scheduledTime) {
|
|
291
|
+
throw new Error('Scheduled time is required for one-time jobs');
|
|
292
|
+
}
|
|
293
|
+
const delay = this.config.scheduledTime.getTime() - Date.now();
|
|
294
|
+
if (delay < 0) {
|
|
295
|
+
this.logger.warn('Scheduled time is in the past, executing immediately', {
|
|
296
|
+
jobId: this.config.id,
|
|
297
|
+
scheduledTime: this.config.scheduledTime.toISOString()
|
|
298
|
+
});
|
|
299
|
+
void this.executeJob();
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
this.timer = setTimeout(() => {
|
|
303
|
+
void this.executeJob();
|
|
304
|
+
}, delay);
|
|
305
|
+
this.logger.debug('One-time job scheduled', {
|
|
306
|
+
jobId: this.config.id,
|
|
307
|
+
scheduledTime: this.config.scheduledTime.toISOString(),
|
|
308
|
+
delay
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
exports.ScheduledJob = ScheduledJob;
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type JobSchedulerErrorCode = 'JOB_SCHEDULER_ERROR' | 'INVALID_INPUT' | 'OPERATION_FAILED';
|
|
2
|
+
export interface JobSchedulerErrorOptions {
|
|
3
|
+
cause?: Error;
|
|
4
|
+
code?: JobSchedulerErrorCode;
|
|
5
|
+
context?: Record<string, unknown>;
|
|
6
|
+
}
|
|
7
|
+
export declare class JobSchedulerError extends Error {
|
|
8
|
+
readonly code: JobSchedulerErrorCode;
|
|
9
|
+
readonly context?: Record<string, unknown>;
|
|
10
|
+
constructor(message: string, options?: JobSchedulerErrorOptions);
|
|
11
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
Copyright (c) 2025 Bernier LLC
|
|
4
|
+
|
|
5
|
+
This file is licensed to the client under a limited-use license.
|
|
6
|
+
The client may use and modify this code only within the scope of the project it was delivered for.
|
|
7
|
+
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.JobSchedulerError = void 0;
|
|
11
|
+
class JobSchedulerError extends Error {
|
|
12
|
+
code;
|
|
13
|
+
context;
|
|
14
|
+
constructor(message, options) {
|
|
15
|
+
super(message, options?.cause ? { cause: options.cause } : undefined);
|
|
16
|
+
this.name = 'JobSchedulerError';
|
|
17
|
+
this.code = options?.code ?? 'JOB_SCHEDULER_ERROR';
|
|
18
|
+
if (options?.context !== undefined) {
|
|
19
|
+
this.context = options.context;
|
|
20
|
+
}
|
|
21
|
+
Error.captureStackTrace?.(this, this.constructor);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.JobSchedulerError = JobSchedulerError;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { JobSchedulerError, type JobSchedulerErrorCode, type JobSchedulerErrorOptions, } from './errors';
|
|
2
|
+
export { JobScheduler, createJobScheduler } from './JobScheduler';
|
|
3
|
+
export { ScheduledJob } from './ScheduledJob';
|
|
4
|
+
export { JobStatus, JobType, JobHandler, JobExecution, ScheduledJobConfig, JobSchedulerConfig, JobSchedulerResult, JobStatistics } from './types';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
Copyright (c) 2025 Bernier LLC
|
|
4
|
+
|
|
5
|
+
This file is licensed to the client under a limited-use license.
|
|
6
|
+
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
7
|
+
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.JobType = exports.JobStatus = exports.ScheduledJob = exports.createJobScheduler = exports.JobScheduler = exports.JobSchedulerError = void 0;
|
|
11
|
+
var errors_1 = require("./errors");
|
|
12
|
+
Object.defineProperty(exports, "JobSchedulerError", { enumerable: true, get: function () { return errors_1.JobSchedulerError; } });
|
|
13
|
+
var JobScheduler_1 = require("./JobScheduler");
|
|
14
|
+
Object.defineProperty(exports, "JobScheduler", { enumerable: true, get: function () { return JobScheduler_1.JobScheduler; } });
|
|
15
|
+
Object.defineProperty(exports, "createJobScheduler", { enumerable: true, get: function () { return JobScheduler_1.createJobScheduler; } });
|
|
16
|
+
var ScheduledJob_1 = require("./ScheduledJob");
|
|
17
|
+
Object.defineProperty(exports, "ScheduledJob", { enumerable: true, get: function () { return ScheduledJob_1.ScheduledJob; } });
|
|
18
|
+
var types_1 = require("./types");
|
|
19
|
+
Object.defineProperty(exports, "JobStatus", { enumerable: true, get: function () { return types_1.JobStatus; } });
|
|
20
|
+
Object.defineProperty(exports, "JobType", { enumerable: true, get: function () { return types_1.JobType; } });
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Job execution status
|
|
3
|
+
*/
|
|
4
|
+
export declare enum JobStatus {
|
|
5
|
+
SCHEDULED = "scheduled",
|
|
6
|
+
RUNNING = "running",
|
|
7
|
+
PAUSED = "paused",
|
|
8
|
+
COMPLETED = "completed",
|
|
9
|
+
FAILED = "failed",
|
|
10
|
+
CANCELLED = "cancelled"
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Job type
|
|
14
|
+
*/
|
|
15
|
+
export declare enum JobType {
|
|
16
|
+
RECURRING = "recurring",
|
|
17
|
+
ONE_TIME = "one-time"
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Job handler function
|
|
21
|
+
*/
|
|
22
|
+
export type JobHandler = () => void | Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Job execution record
|
|
25
|
+
*/
|
|
26
|
+
export interface JobExecution {
|
|
27
|
+
executionId: string;
|
|
28
|
+
jobId: string;
|
|
29
|
+
startTime: Date;
|
|
30
|
+
endTime?: Date;
|
|
31
|
+
status: JobStatus;
|
|
32
|
+
error?: string;
|
|
33
|
+
duration?: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Scheduled job configuration
|
|
37
|
+
*/
|
|
38
|
+
export interface ScheduledJobConfig {
|
|
39
|
+
id: string;
|
|
40
|
+
type: JobType;
|
|
41
|
+
handler: JobHandler;
|
|
42
|
+
cronExpression?: string;
|
|
43
|
+
scheduledTime?: Date;
|
|
44
|
+
name?: string;
|
|
45
|
+
metadata?: Record<string, unknown>;
|
|
46
|
+
maxRetries?: number;
|
|
47
|
+
retryDelay?: number;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Job scheduler configuration
|
|
51
|
+
*/
|
|
52
|
+
export interface JobSchedulerConfig {
|
|
53
|
+
timezone?: string;
|
|
54
|
+
maxConcurrentJobs?: number;
|
|
55
|
+
enableHistory?: boolean;
|
|
56
|
+
historyRetention?: number;
|
|
57
|
+
enableTaskQueue?: boolean;
|
|
58
|
+
taskQueueConfig?: {
|
|
59
|
+
maxSize?: number;
|
|
60
|
+
priority?: number;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Job scheduler result
|
|
65
|
+
*/
|
|
66
|
+
export interface JobSchedulerResult<T = unknown> {
|
|
67
|
+
success: boolean;
|
|
68
|
+
data?: T;
|
|
69
|
+
error?: string;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Job statistics
|
|
73
|
+
*/
|
|
74
|
+
export interface JobStatistics {
|
|
75
|
+
totalExecutions: number;
|
|
76
|
+
successfulExecutions: number;
|
|
77
|
+
failedExecutions: number;
|
|
78
|
+
averageDuration: number;
|
|
79
|
+
lastExecution?: Date;
|
|
80
|
+
nextExecution?: Date;
|
|
81
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
Copyright (c) 2025 Bernier LLC
|
|
4
|
+
|
|
5
|
+
This file is licensed to the client under a limited-use license.
|
|
6
|
+
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
7
|
+
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.JobType = exports.JobStatus = void 0;
|
|
11
|
+
/**
|
|
12
|
+
* Job execution status
|
|
13
|
+
*/
|
|
14
|
+
var JobStatus;
|
|
15
|
+
(function (JobStatus) {
|
|
16
|
+
JobStatus["SCHEDULED"] = "scheduled";
|
|
17
|
+
JobStatus["RUNNING"] = "running";
|
|
18
|
+
JobStatus["PAUSED"] = "paused";
|
|
19
|
+
JobStatus["COMPLETED"] = "completed";
|
|
20
|
+
JobStatus["FAILED"] = "failed";
|
|
21
|
+
JobStatus["CANCELLED"] = "cancelled";
|
|
22
|
+
})(JobStatus || (exports.JobStatus = JobStatus = {}));
|
|
23
|
+
/**
|
|
24
|
+
* Job type
|
|
25
|
+
*/
|
|
26
|
+
var JobType;
|
|
27
|
+
(function (JobType) {
|
|
28
|
+
JobType["RECURRING"] = "recurring";
|
|
29
|
+
JobType["ONE_TIME"] = "one-time";
|
|
30
|
+
})(JobType || (exports.JobType = JobType = {}));
|
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": "2.0.
|
|
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",
|