mongo-job-scheduler 0.1.6 → 0.1.7
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 +23 -1
- package/dist/core/scheduler.js +1 -1
- package/dist/types/job.d.ts +1 -1
- package/dist/types/schedule.d.ts +1 -1
- package/dist/worker/worker.js +5 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@ Designed for distributed systems that need:
|
|
|
21
21
|
- **Interval jobs**
|
|
22
22
|
- **Resume on restart**
|
|
23
23
|
- **Stale lock recovery**
|
|
24
|
-
- **Automatic Lock Renewal** (Heartbeats): Long-running jobs automatically extend their lock.
|
|
24
|
+
- **Automatic Lock Renewal** (Heartbeats): Long-running jobs automatically extend their lock. Default lock timeout is 10 minutes.
|
|
25
25
|
- **Sharding-safe design**
|
|
26
26
|
|
|
27
27
|
## Distributed Systems
|
|
@@ -62,6 +62,28 @@ const scheduler = new Scheduler({
|
|
|
62
62
|
await scheduler.start();
|
|
63
63
|
```
|
|
64
64
|
|
|
65
|
+
## Retries
|
|
66
|
+
|
|
67
|
+
You can configure retries using a simple number (for attempts with instant retry) or a detailed object:
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
// Shorthand: 3 max attempts, 0 delay
|
|
71
|
+
await scheduler.schedule({
|
|
72
|
+
name: "email",
|
|
73
|
+
retry: 3,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// Full configuration
|
|
77
|
+
await scheduler.schedule({
|
|
78
|
+
name: "webhook",
|
|
79
|
+
retry: {
|
|
80
|
+
maxAttempts: 5,
|
|
81
|
+
delay: 1000, // 1 second fixed delay
|
|
82
|
+
// delay: (attempt) => attempt * 1000 // or dynamic backoff
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
65
87
|
## Cron with Timezone
|
|
66
88
|
|
|
67
89
|
```typescript
|
package/dist/core/scheduler.js
CHANGED
|
@@ -13,7 +13,7 @@ class Scheduler {
|
|
|
13
13
|
this.handler = options.handler;
|
|
14
14
|
this.workerCount = options.workers ?? 1;
|
|
15
15
|
this.pollInterval = options.pollIntervalMs ?? 500;
|
|
16
|
-
this.lockTimeout = options.lockTimeoutMs ??
|
|
16
|
+
this.lockTimeout = options.lockTimeoutMs ?? 10 * 60 * 1000; // default 10 minutes
|
|
17
17
|
this.defaultTimezone = options.defaultTimezone;
|
|
18
18
|
}
|
|
19
19
|
on(event, listener) {
|
package/dist/types/job.d.ts
CHANGED
package/dist/types/schedule.d.ts
CHANGED
package/dist/worker/worker.js
CHANGED
|
@@ -10,7 +10,7 @@ class Worker {
|
|
|
10
10
|
this.handler = handler;
|
|
11
11
|
this.running = false;
|
|
12
12
|
this.pollInterval = options.pollIntervalMs ?? 500;
|
|
13
|
-
this.lockTimeout = options.lockTimeoutMs ??
|
|
13
|
+
this.lockTimeout = options.lockTimeoutMs ?? 10 * 60 * 1000; // default 10 minutes
|
|
14
14
|
this.workerId =
|
|
15
15
|
options.workerId ?? `worker-${Math.random().toString(36).slice(2)}`;
|
|
16
16
|
this.defaultTimezone = options.defaultTimezone;
|
|
@@ -128,7 +128,10 @@ class Worker {
|
|
|
128
128
|
catch (err) {
|
|
129
129
|
const error = err instanceof Error ? err : new Error(String(err));
|
|
130
130
|
const attempts = (job.attempts ?? 0) + 1;
|
|
131
|
-
|
|
131
|
+
let retry = job.retry;
|
|
132
|
+
if (typeof retry === "number") {
|
|
133
|
+
retry = { maxAttempts: retry, delay: 0 };
|
|
134
|
+
}
|
|
132
135
|
if (retry && attempts < retry.maxAttempts) {
|
|
133
136
|
const nextRunAt = new Date(Date.now() + (0, retry_1.getRetryDelay)(retry, attempts));
|
|
134
137
|
await this.store.reschedule(job._id, nextRunAt, { attempts });
|
package/package.json
CHANGED