mongo-job-scheduler 0.1.0 → 0.1.1
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 +10 -7
- package/dist/core/scheduler.d.ts +2 -0
- package/dist/core/scheduler.js +32 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +1 -0
- package/dist/types/schedule.d.ts +19 -0
- package/dist/types/schedule.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -56,13 +56,16 @@ await scheduler.start();
|
|
|
56
56
|
## Cron with Timezone
|
|
57
57
|
|
|
58
58
|
```typescript
|
|
59
|
-
await scheduler.schedule(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
59
|
+
await scheduler.schedule(
|
|
60
|
+
"daily-report",
|
|
61
|
+
{ type: "report" }, // payload
|
|
62
|
+
{
|
|
63
|
+
repeat: {
|
|
64
|
+
cron: "0 9 * * *",
|
|
65
|
+
timezone: "Asia/Kolkata", // default is UTC
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
);
|
|
66
69
|
```
|
|
67
70
|
|
|
68
71
|
## Documentation
|
package/dist/core/scheduler.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { SchedulerEventMap } from "../types/events";
|
|
2
2
|
import { JobStore } from "../store";
|
|
3
3
|
import { Job } from "../types/job";
|
|
4
|
+
import { ScheduleOptions } from "../types/schedule";
|
|
4
5
|
export interface SchedulerOptions {
|
|
5
6
|
id?: string;
|
|
6
7
|
store?: JobStore;
|
|
@@ -23,6 +24,7 @@ export declare class Scheduler {
|
|
|
23
24
|
private readonly defaultTimezone?;
|
|
24
25
|
constructor(options?: SchedulerOptions);
|
|
25
26
|
on<K extends keyof SchedulerEventMap>(event: K, listener: (payload: SchedulerEventMap[K]) => void): this;
|
|
27
|
+
schedule<T = unknown>(options: ScheduleOptions<T>): Promise<Job<T>>;
|
|
26
28
|
start(): Promise<void>;
|
|
27
29
|
stop(): Promise<void>;
|
|
28
30
|
isRunning(): boolean;
|
package/dist/core/scheduler.js
CHANGED
|
@@ -17,6 +17,38 @@ export class Scheduler {
|
|
|
17
17
|
this.emitter.on(event, listener);
|
|
18
18
|
return this;
|
|
19
19
|
}
|
|
20
|
+
async schedule(options) {
|
|
21
|
+
if (!this.store) {
|
|
22
|
+
throw new Error("Scheduler has no JobStore configured");
|
|
23
|
+
}
|
|
24
|
+
const now = new Date();
|
|
25
|
+
// ------------------------
|
|
26
|
+
// Validation
|
|
27
|
+
// ------------------------
|
|
28
|
+
if (!options.name) {
|
|
29
|
+
throw new Error("Job name is required");
|
|
30
|
+
}
|
|
31
|
+
if (options.repeat?.cron && options.repeat?.every != null) {
|
|
32
|
+
throw new Error("Use either cron or every, not both");
|
|
33
|
+
}
|
|
34
|
+
// ------------------------
|
|
35
|
+
// Normalize run time
|
|
36
|
+
// ------------------------
|
|
37
|
+
const nextRunAt = options.runAt ?? now;
|
|
38
|
+
const job = {
|
|
39
|
+
name: options.name,
|
|
40
|
+
data: options.data,
|
|
41
|
+
status: "pending",
|
|
42
|
+
attempts: 0,
|
|
43
|
+
nextRunAt,
|
|
44
|
+
retry: options.retry,
|
|
45
|
+
repeat: options.repeat,
|
|
46
|
+
createdAt: now,
|
|
47
|
+
updatedAt: now,
|
|
48
|
+
};
|
|
49
|
+
const created = await this.store.create(job);
|
|
50
|
+
return created;
|
|
51
|
+
}
|
|
20
52
|
async start() {
|
|
21
53
|
if (this.started)
|
|
22
54
|
return;
|
package/dist/types/index.d.ts
CHANGED
package/dist/types/index.js
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { RetryOptions } from "./retry";
|
|
2
|
+
import { RepeatOptions } from "./repeat";
|
|
3
|
+
export interface ScheduleOptions<T = unknown> {
|
|
4
|
+
name: string;
|
|
5
|
+
data: T;
|
|
6
|
+
/**
|
|
7
|
+
* When the job should first run.
|
|
8
|
+
* Defaults to now.
|
|
9
|
+
*/
|
|
10
|
+
runAt?: Date;
|
|
11
|
+
/**
|
|
12
|
+
* Retry configuration
|
|
13
|
+
*/
|
|
14
|
+
retry?: RetryOptions;
|
|
15
|
+
/**
|
|
16
|
+
* Repeat configuration (cron or every)
|
|
17
|
+
*/
|
|
18
|
+
repeat?: RepeatOptions;
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED