mongo-job-scheduler 0.1.4 → 0.1.5

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 CHANGED
@@ -116,6 +116,18 @@ await scheduler.cancel(jobId);
116
116
  const job = await scheduler.getJob(jobId);
117
117
  ```
118
118
 
119
+ ## Job Persistence & Updates
120
+
121
+ Update job `data`, reschedule, or modify configuration safely.
122
+
123
+ ```typescript
124
+ await scheduler.updateJob(jobId, {
125
+ data: { page: 2 },
126
+ nextRunAt: new Date(Date.now() + 60000), // delay by 1 min
127
+ repeat: { every: 60000 }, // Change to run every minute
128
+ });
129
+ ```
130
+
119
131
  ## Bulk Scheduling
120
132
 
121
133
  For high-performance ingestion, use `scheduleBulk` to insert multiple jobs in a single database operation:
@@ -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
  */
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongo-job-scheduler",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Production-grade MongoDB-backed job scheduler with retries, cron, timezone support, and crash recovery",
5
5
  "license": "MIT",
6
6
  "author": "Darshan Bhut",