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 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
@@ -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 ?? 30000;
16
+ this.lockTimeout = options.lockTimeoutMs ?? 10 * 60 * 1000; // default 10 minutes
17
17
  this.defaultTimezone = options.defaultTimezone;
18
18
  }
19
19
  on(event, listener) {
@@ -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;
@@ -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
  */
@@ -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 ?? 30000;
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
- const retry = job.retry;
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongo-job-scheduler",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
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",