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 CHANGED
@@ -56,13 +56,16 @@ await scheduler.start();
56
56
  ## Cron with Timezone
57
57
 
58
58
  ```typescript
59
- await scheduler.schedule({
60
- name: "daily-report",
61
- repeat: {
62
- cron: "0 9 * * *",
63
- timezone: "Asia/Kolkata", // default is UTC
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
@@ -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;
@@ -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;
@@ -3,3 +3,4 @@ export * from "./retry";
3
3
  export * from "./repeat";
4
4
  export * from "./lifecycle";
5
5
  export * from "./events";
6
+ export * from "./schedule";
@@ -3,3 +3,4 @@ export * from "./retry";
3
3
  export * from "./repeat";
4
4
  export * from "./lifecycle";
5
5
  export * from "./events";
6
+ export * from "./schedule";
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongo-job-scheduler",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
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",