mongo-job-scheduler 0.1.4 → 0.1.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/README.md +20 -10
- package/dist/core/scheduler.d.ts +5 -1
- package/dist/core/scheduler.js +9 -0
- package/dist/store/in-memory-job-store.d.ts +2 -1
- package/dist/store/in-memory-job-store.js +18 -0
- package/dist/store/job-store.d.ts +12 -0
- package/dist/store/mongo/mongo-job-store.d.ts +2 -1
- package/dist/store/mongo/mongo-job-store.js +14 -0
- package/dist/types/job.d.ts +1 -1
- package/dist/types/schedule.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -65,16 +65,14 @@ await scheduler.start();
|
|
|
65
65
|
## Cron with Timezone
|
|
66
66
|
|
|
67
67
|
```typescript
|
|
68
|
-
await scheduler.schedule(
|
|
69
|
-
"daily-report",
|
|
70
|
-
{ type: "report" }, // payload
|
|
71
|
-
{
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
);
|
|
68
|
+
await scheduler.schedule({
|
|
69
|
+
name: "daily-report",
|
|
70
|
+
// data: { type: "report" }, // optional payload
|
|
71
|
+
repeat: {
|
|
72
|
+
cron: "0 9 * * *",
|
|
73
|
+
timezone: "Asia/Kolkata", // default is UTC
|
|
74
|
+
},
|
|
75
|
+
});
|
|
78
76
|
```
|
|
79
77
|
|
|
80
78
|
## Interval Scheduling
|
|
@@ -116,6 +114,18 @@ await scheduler.cancel(jobId);
|
|
|
116
114
|
const job = await scheduler.getJob(jobId);
|
|
117
115
|
```
|
|
118
116
|
|
|
117
|
+
## Job Persistence & Updates
|
|
118
|
+
|
|
119
|
+
Update job `data`, reschedule, or modify configuration safely.
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
await scheduler.updateJob(jobId, {
|
|
123
|
+
data: { page: 2 },
|
|
124
|
+
nextRunAt: new Date(Date.now() + 60000), // delay by 1 min
|
|
125
|
+
repeat: { every: 60000 }, // Change to run every minute
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
119
129
|
## Bulk Scheduling
|
|
120
130
|
|
|
121
131
|
For high-performance ingestion, use `scheduleBulk` to insert multiple jobs in a single database operation:
|
package/dist/core/scheduler.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SchedulerEventMap } from "../types/events";
|
|
2
|
-
import { JobStore } from "../store";
|
|
2
|
+
import { JobStore, JobUpdates } from "../store";
|
|
3
3
|
import { Job } from "../types/job";
|
|
4
4
|
import { ScheduleOptions } from "../types/schedule";
|
|
5
5
|
export interface SchedulerOptions {
|
|
@@ -33,6 +33,10 @@ export declare class Scheduler {
|
|
|
33
33
|
* Get a job by ID
|
|
34
34
|
*/
|
|
35
35
|
getJob(jobId: unknown): Promise<Job | null>;
|
|
36
|
+
/**
|
|
37
|
+
* Update job data or schedule
|
|
38
|
+
*/
|
|
39
|
+
updateJob(jobId: unknown, updates: JobUpdates): Promise<void>;
|
|
36
40
|
/**
|
|
37
41
|
* Cancel a job
|
|
38
42
|
*/
|
package/dist/core/scheduler.js
CHANGED
|
@@ -94,6 +94,15 @@ class Scheduler {
|
|
|
94
94
|
}
|
|
95
95
|
return this.store.findById(jobId);
|
|
96
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Update job data or schedule
|
|
99
|
+
*/
|
|
100
|
+
async updateJob(jobId, updates) {
|
|
101
|
+
if (!this.store) {
|
|
102
|
+
throw new Error("Scheduler has no JobStore configured");
|
|
103
|
+
}
|
|
104
|
+
await this.store.update(jobId, updates);
|
|
105
|
+
}
|
|
97
106
|
/**
|
|
98
107
|
* Cancel a job
|
|
99
108
|
*/
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Job } from "../types/job";
|
|
2
|
-
import { JobStore } from "./job-store";
|
|
2
|
+
import { JobStore, JobUpdates } from "./job-store";
|
|
3
3
|
export declare class InMemoryJobStore implements JobStore {
|
|
4
4
|
private jobs;
|
|
5
5
|
private mutex;
|
|
@@ -23,4 +23,5 @@ export declare class InMemoryJobStore implements JobStore {
|
|
|
23
23
|
cancel(jobId: unknown): Promise<void>;
|
|
24
24
|
findById(jobId: unknown): Promise<Job | null>;
|
|
25
25
|
renewLock(jobId: unknown, workerId: string): Promise<void>;
|
|
26
|
+
update(jobId: unknown, updates: JobUpdates): Promise<void>;
|
|
26
27
|
}
|
|
@@ -131,5 +131,23 @@ class InMemoryJobStore {
|
|
|
131
131
|
throw new Error("Job lock lost or owner changed");
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
|
+
async update(jobId, updates) {
|
|
135
|
+
const job = this.jobs.get(String(jobId));
|
|
136
|
+
if (!job)
|
|
137
|
+
throw new store_errors_1.JobNotFoundError();
|
|
138
|
+
if (updates.data !== undefined) {
|
|
139
|
+
job.data = updates.data;
|
|
140
|
+
}
|
|
141
|
+
if (updates.nextRunAt !== undefined) {
|
|
142
|
+
job.nextRunAt = updates.nextRunAt;
|
|
143
|
+
}
|
|
144
|
+
if (updates.retry !== undefined) {
|
|
145
|
+
job.retry = updates.retry;
|
|
146
|
+
}
|
|
147
|
+
if (updates.repeat !== undefined) {
|
|
148
|
+
job.repeat = updates.repeat;
|
|
149
|
+
}
|
|
150
|
+
job.updatedAt = new Date();
|
|
151
|
+
}
|
|
134
152
|
}
|
|
135
153
|
exports.InMemoryJobStore = InMemoryJobStore;
|
|
@@ -51,4 +51,16 @@ export interface JobStore {
|
|
|
51
51
|
* Renew the lock for a running job (heartbeat)
|
|
52
52
|
*/
|
|
53
53
|
renewLock(jobId: unknown, workerId: string): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Update job properties (data persistence)
|
|
56
|
+
*/
|
|
57
|
+
update(jobId: unknown, updates: JobUpdates): Promise<void>;
|
|
58
|
+
}
|
|
59
|
+
import { RetryOptions } from "../types/retry";
|
|
60
|
+
import { RepeatOptions } from "../types/repeat";
|
|
61
|
+
export interface JobUpdates {
|
|
62
|
+
data?: unknown;
|
|
63
|
+
nextRunAt?: Date;
|
|
64
|
+
retry?: RetryOptions;
|
|
65
|
+
repeat?: RepeatOptions;
|
|
54
66
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Db, ObjectId } from "mongodb";
|
|
2
|
-
import { JobStore } from "../job-store";
|
|
2
|
+
import { JobStore, JobUpdates } from "../job-store";
|
|
3
3
|
import { Job } from "../../types/job";
|
|
4
4
|
export interface MongoJobStoreOptions {
|
|
5
5
|
collectionName?: string;
|
|
@@ -29,4 +29,5 @@ export declare class MongoJobStore implements JobStore {
|
|
|
29
29
|
lockTimeoutMs: number;
|
|
30
30
|
}): Promise<number>;
|
|
31
31
|
renewLock(id: ObjectId, workerId: string): Promise<void>;
|
|
32
|
+
update(id: ObjectId, updates: JobUpdates): Promise<void>;
|
|
32
33
|
}
|
|
@@ -181,5 +181,19 @@ class MongoJobStore {
|
|
|
181
181
|
throw new Error("Job lock lost or owner changed");
|
|
182
182
|
}
|
|
183
183
|
}
|
|
184
|
+
async update(id, updates) {
|
|
185
|
+
if (Object.keys(updates).length === 0)
|
|
186
|
+
return;
|
|
187
|
+
const $set = { updatedAt: new Date() };
|
|
188
|
+
if (updates.data !== undefined)
|
|
189
|
+
$set.data = updates.data;
|
|
190
|
+
if (updates.nextRunAt !== undefined)
|
|
191
|
+
$set.nextRunAt = updates.nextRunAt;
|
|
192
|
+
if (updates.retry !== undefined)
|
|
193
|
+
$set.retry = updates.retry;
|
|
194
|
+
if (updates.repeat !== undefined)
|
|
195
|
+
$set.repeat = updates.repeat;
|
|
196
|
+
await this.collection.updateOne({ _id: id }, { $set });
|
|
197
|
+
}
|
|
184
198
|
}
|
|
185
199
|
exports.MongoJobStore = MongoJobStore;
|
package/dist/types/job.d.ts
CHANGED
package/dist/types/schedule.d.ts
CHANGED
package/package.json
CHANGED