mongo-job-scheduler 0.1.5 → 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 +31 -11
- package/dist/core/scheduler.js +1 -1
- package/dist/types/job.d.ts +2 -2
- package/dist/types/schedule.d.ts +2 -2
- 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,19 +62,39 @@ 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
|
|
68
|
-
await scheduler.schedule(
|
|
69
|
-
"daily-report",
|
|
70
|
-
{ type: "report" }, // payload
|
|
71
|
-
{
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
);
|
|
90
|
+
await scheduler.schedule({
|
|
91
|
+
name: "daily-report",
|
|
92
|
+
// data: { type: "report" }, // optional payload
|
|
93
|
+
repeat: {
|
|
94
|
+
cron: "0 9 * * *",
|
|
95
|
+
timezone: "Asia/Kolkata", // default is UTC
|
|
96
|
+
},
|
|
97
|
+
});
|
|
78
98
|
```
|
|
79
99
|
|
|
80
100
|
## Interval Scheduling
|
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
|
@@ -4,7 +4,7 @@ import { RepeatOptions } from "./repeat";
|
|
|
4
4
|
export interface Job<Data = unknown> {
|
|
5
5
|
_id?: unknown;
|
|
6
6
|
name: string;
|
|
7
|
-
data
|
|
7
|
+
data?: Data;
|
|
8
8
|
status: JobStatus;
|
|
9
9
|
nextRunAt: Date;
|
|
10
10
|
lastRunAt?: Date;
|
|
@@ -13,7 +13,7 @@ export interface Job<Data = unknown> {
|
|
|
13
13
|
lockedBy?: string;
|
|
14
14
|
attempts: number;
|
|
15
15
|
lastError?: string;
|
|
16
|
-
retry?: RetryOptions;
|
|
16
|
+
retry?: RetryOptions | number;
|
|
17
17
|
repeat?: RepeatOptions;
|
|
18
18
|
dedupeKey?: string;
|
|
19
19
|
createdAt: Date;
|
package/dist/types/schedule.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { RetryOptions } from "./retry";
|
|
|
2
2
|
import { RepeatOptions } from "./repeat";
|
|
3
3
|
export interface ScheduleOptions<T = unknown> {
|
|
4
4
|
name: string;
|
|
5
|
-
data
|
|
5
|
+
data?: T;
|
|
6
6
|
/**
|
|
7
7
|
* When the job should first run.
|
|
8
8
|
* Defaults to now.
|
|
@@ -11,7 +11,7 @@ export interface ScheduleOptions<T = unknown> {
|
|
|
11
11
|
/**
|
|
12
12
|
* Retry configuration
|
|
13
13
|
*/
|
|
14
|
-
retry?: RetryOptions;
|
|
14
|
+
retry?: RetryOptions | number;
|
|
15
15
|
/**
|
|
16
16
|
* Repeat configuration (cron or every)
|
|
17
17
|
*/
|
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