cron-guardian 1.0.0

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.
Files changed (37) hide show
  1. package/CONTRIBUTING.md +111 -0
  2. package/DOCS.md +744 -0
  3. package/LICENSE +21 -0
  4. package/README.md +141 -0
  5. package/dist/index.d.ts +2 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +6 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/src/jobRegistry/jobRegistry.d.ts +9 -0
  10. package/dist/src/jobRegistry/jobRegistry.d.ts.map +1 -0
  11. package/dist/src/jobRegistry/jobRegistry.js +25 -0
  12. package/dist/src/jobRegistry/jobRegistry.js.map +1 -0
  13. package/dist/src/jobRunner/jobRunner.d.ts +12 -0
  14. package/dist/src/jobRunner/jobRunner.d.ts.map +1 -0
  15. package/dist/src/jobRunner/jobRunner.js +46 -0
  16. package/dist/src/jobRunner/jobRunner.js.map +1 -0
  17. package/dist/src/logger/logger.d.ts +7 -0
  18. package/dist/src/logger/logger.d.ts.map +1 -0
  19. package/dist/src/logger/logger.js +16 -0
  20. package/dist/src/logger/logger.js.map +1 -0
  21. package/dist/src/notifier/notifier.d.ts +5 -0
  22. package/dist/src/notifier/notifier.d.ts.map +1 -0
  23. package/dist/src/notifier/notifier.js +12 -0
  24. package/dist/src/notifier/notifier.js.map +1 -0
  25. package/dist/src/retryManager/retryManager.d.ts +4 -0
  26. package/dist/src/retryManager/retryManager.d.ts.map +1 -0
  27. package/dist/src/retryManager/retryManager.js +23 -0
  28. package/dist/src/retryManager/retryManager.js.map +1 -0
  29. package/dist/src/scheduler/scheduler.d.ts +11 -0
  30. package/dist/src/scheduler/scheduler.d.ts.map +1 -0
  31. package/dist/src/scheduler/scheduler.js +65 -0
  32. package/dist/src/scheduler/scheduler.js.map +1 -0
  33. package/dist/src/smartCron.d.ts +40 -0
  34. package/dist/src/smartCron.d.ts.map +1 -0
  35. package/dist/src/smartCron.js +60 -0
  36. package/dist/src/smartCron.js.map +1 -0
  37. package/package.json +50 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nisarg Satasiya
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,141 @@
1
+ # Smart Cron Manager
2
+
3
+ [![npm version](https://badge.fury.io/js/smart-cron-manager.svg)](https://badge.fury.io/js/smart-cron-manager)
4
+ [![Node.js Version](https://img.shields.io/badge/node-%3E%3D18.0.0-brightgreen)](https://nodejs.org/)
5
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.0+-blue.svg)](https://www.typescriptlang.org/)
6
+
7
+ A reliable cron job manager for Node.js applications with retries, monitoring, and job control.
8
+
9
+ ## Features
10
+
11
+ - ✅ **Cron Scheduling**: Standard cron expressions with second precision
12
+ - ✅ **Named Jobs**: Unique identifiers with duplicate prevention
13
+ - ✅ **Automatic Retries**: Configurable retry logic with delays
14
+ - ✅ **Overlap Prevention**: Prevent concurrent job executions
15
+ - ✅ **Execution Logging**: Comprehensive logging with performance metrics
16
+ - ✅ **Failure Notifications**: Custom callbacks for error handling
17
+ - ✅ **Runtime Control**: Start, stop, and remove jobs dynamically
18
+ - ✅ **TypeScript Support**: Full type definitions included
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ npm install smart-cron-manager
24
+ ```
25
+
26
+ **Requirements:** Node.js ≥ 18.0.0
27
+
28
+ ## Quick Start
29
+
30
+ ```typescript
31
+ import { SmartCron } from 'smart-cron-manager';
32
+
33
+ const cronManager = new SmartCron();
34
+
35
+ // Schedule a simple job
36
+ cronManager.schedule('*/5 * * * *', async () => {
37
+ console.log('Runs every 5 minutes');
38
+ });
39
+
40
+ // Schedule with advanced options
41
+ cronManager.schedule('0 */1 * * *', async () => {
42
+ await processData();
43
+ }, {
44
+ name: 'hourly-processor',
45
+ retries: 3,
46
+ retryDelay: 5000,
47
+ preventOverlap: true,
48
+ onFailure: (error, job) => {
49
+ console.error(`Job ${job.name} failed:`, error.message);
50
+ }
51
+ });
52
+
53
+ // Control jobs
54
+ cronManager.start('hourly-processor');
55
+ cronManager.stop('hourly-processor');
56
+ cronManager.remove('hourly-processor');
57
+
58
+ // Monitor
59
+ const jobs = cronManager.listJobs();
60
+ const logs = cronManager.getLogs();
61
+ ```
62
+
63
+ ## API Overview
64
+
65
+ ### Core Methods
66
+ - `schedule(cronExpression, handler, options?)` - Schedule a new job
67
+ - `start(jobName)` - Start a stopped job
68
+ - `stop(jobName)` - Stop a running job
69
+ - `remove(jobName)` - Remove a job permanently
70
+ - `listJobs()` - Get all scheduled jobs
71
+ - `getLogs()` - Get execution logs
72
+
73
+ ### Job Options
74
+ - `name` - Unique job identifier
75
+ - `retries` - Number of retry attempts
76
+ - `retryDelay` - Delay between retries (ms)
77
+ - `preventOverlap` - Prevent concurrent executions
78
+ - `onFailure` - Error callback function
79
+
80
+ ## Examples
81
+
82
+ ### Database Backup
83
+ ```typescript
84
+ cronManager.schedule('0 2 * * *', async () => {
85
+ const backup = await createBackup();
86
+ await uploadToS3(backup);
87
+ }, {
88
+ name: 'daily-backup',
89
+ retries: 3,
90
+ retryDelay: 300000,
91
+ preventOverlap: true,
92
+ onFailure: (error) => sendAlert(`Backup failed: ${error.message}`)
93
+ });
94
+ ```
95
+
96
+ ### API Health Check
97
+ ```typescript
98
+ cronManager.schedule('*/5 * * * *', async () => {
99
+ const response = await fetch('https://api.example.com/health');
100
+ if (!response.ok) throw new Error('API unhealthy');
101
+ }, {
102
+ name: 'health-check',
103
+ retries: 2,
104
+ onFailure: (error) => console.error('Health check failed:', error.message)
105
+ });
106
+ ```
107
+
108
+ ## Documentation
109
+
110
+ 📖 **[Complete Documentation](DOCS.md)** - Includes:
111
+ - Detailed API reference
112
+ - Advanced usage examples
113
+ - Edge cases and error handling
114
+ - Best practices
115
+ - Troubleshooting guide
116
+ - Performance considerations
117
+
118
+ ## Testing
119
+
120
+ ```bash
121
+ # Run tests
122
+ npm test
123
+
124
+ # Build the project
125
+ npm run build
126
+
127
+ # Run example
128
+ node test-example.js
129
+ ```
130
+
131
+ ## License
132
+
133
+ MIT License - see LICENSE file for details.
134
+
135
+ ## Contributing
136
+
137
+ Contributions welcome! Please see the [contributing guidelines](CONTRIBUTING.md) and [complete documentation](DOCS.md) for details.
138
+
139
+ ---
140
+
141
+ **Version:** 1.0.0 | **Node.js:** ≥18.0.0 | **TypeScript:** ≥4.0.0
@@ -0,0 +1,2 @@
1
+ export { SmartCron } from './src/smartCron';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SmartCron = void 0;
4
+ var smartCron_1 = require("./src/smartCron");
5
+ Object.defineProperty(exports, "SmartCron", { enumerable: true, get: function () { return smartCron_1.SmartCron; } });
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AAAA,6CAA4C;AAAnC,sGAAA,SAAS,OAAA"}
@@ -0,0 +1,9 @@
1
+ import { Job } from '../smartCron';
2
+ export declare class JobRegistry {
3
+ private jobs;
4
+ add(job: Job): void;
5
+ get(name: string): Job | undefined;
6
+ remove(name: string): boolean;
7
+ list(): Job[];
8
+ }
9
+ //# sourceMappingURL=jobRegistry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jobRegistry.d.ts","sourceRoot":"","sources":["../../../src/jobRegistry/jobRegistry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAEnC,qBAAa,WAAW;IACtB,OAAO,CAAC,IAAI,CAA+B;IAE3C,GAAG,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI;IAOnB,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS;IAIlC,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAI7B,IAAI,IAAI,GAAG,EAAE;CAGd"}
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JobRegistry = void 0;
4
+ class JobRegistry {
5
+ constructor() {
6
+ this.jobs = new Map();
7
+ }
8
+ add(job) {
9
+ if (this.jobs.has(job.name)) {
10
+ throw new Error(`Job with name ${job.name} already exists`);
11
+ }
12
+ this.jobs.set(job.name, job);
13
+ }
14
+ get(name) {
15
+ return this.jobs.get(name);
16
+ }
17
+ remove(name) {
18
+ return this.jobs.delete(name);
19
+ }
20
+ list() {
21
+ return Array.from(this.jobs.values());
22
+ }
23
+ }
24
+ exports.JobRegistry = JobRegistry;
25
+ //# sourceMappingURL=jobRegistry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jobRegistry.js","sourceRoot":"","sources":["../../../src/jobRegistry/jobRegistry.ts"],"names":[],"mappings":";;;AAEA,MAAa,WAAW;IAAxB;QACU,SAAI,GAAqB,IAAI,GAAG,EAAE,CAAC;IAoB7C,CAAC;IAlBC,GAAG,CAAC,GAAQ;QACV,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,iBAAiB,GAAG,CAAC,IAAI,iBAAiB,CAAC,CAAC;QAC9D,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAC/B,CAAC;IAED,GAAG,CAAC,IAAY;QACd,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IAED,MAAM,CAAC,IAAY;QACjB,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAChC,CAAC;IAED,IAAI;QACF,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;IACxC,CAAC;CACF;AArBD,kCAqBC"}
@@ -0,0 +1,12 @@
1
+ import { Job } from '../smartCron';
2
+ import { Logger } from '../logger/logger';
3
+ import { Notifier } from '../notifier/notifier';
4
+ import { RetryManager } from '../retryManager/retryManager';
5
+ export declare class JobRunner {
6
+ private logger;
7
+ private notifier;
8
+ private retryManager;
9
+ constructor(logger: Logger, notifier: Notifier, retryManager: RetryManager);
10
+ run(job: Job): Promise<void>;
11
+ }
12
+ //# sourceMappingURL=jobRunner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jobRunner.d.ts","sourceRoot":"","sources":["../../../src/jobRunner/jobRunner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAY,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAE5D,qBAAa,SAAS;IAElB,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,QAAQ;IAChB,OAAO,CAAC,YAAY;gBAFZ,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,QAAQ,EAClB,YAAY,EAAE,YAAY;IAG9B,GAAG,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC;CAsCnC"}
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JobRunner = void 0;
4
+ class JobRunner {
5
+ constructor(logger, notifier, retryManager) {
6
+ this.logger = logger;
7
+ this.notifier = notifier;
8
+ this.retryManager = retryManager;
9
+ }
10
+ async run(job) {
11
+ if (job.options.preventOverlap && job.isRunning) {
12
+ return; // skip overlapping
13
+ }
14
+ job.isRunning = true;
15
+ const startTime = new Date();
16
+ try {
17
+ await this.retryManager.runWithRetries(job.handler, job.options.retries || 0, job.options.retryDelay || 0);
18
+ const endTime = new Date();
19
+ this.logger.log({
20
+ jobName: job.name,
21
+ startTime,
22
+ endTime,
23
+ duration: endTime.getTime() - startTime.getTime(),
24
+ status: 'success',
25
+ });
26
+ }
27
+ catch (error) {
28
+ const endTime = new Date();
29
+ this.logger.log({
30
+ jobName: job.name,
31
+ startTime,
32
+ endTime,
33
+ duration: endTime.getTime() - startTime.getTime(),
34
+ status: 'failure',
35
+ errorMessage: error.message,
36
+ });
37
+ this.notifier.notifyFailure(error, job);
38
+ }
39
+ finally {
40
+ job.isRunning = false;
41
+ job.lastRun = new Date();
42
+ }
43
+ }
44
+ }
45
+ exports.JobRunner = JobRunner;
46
+ //# sourceMappingURL=jobRunner.js.map
@@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AAE;gBACjD,MAAM,EAAE,SAAS;gBACjB,YAAY,EAAG,KAAe,CAAC,OAAO;aACvC,CAAC,CAAC;YACH,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,KAAc,EAAE,GAAG,CAAC,CAAC;QACnD,CAAC;gBAAS,CAAC;YACT,GAAG,CAAC,SAAS,GAAG,KAAK,CAAC;YACtB,GAAG,CAAC,OAAO,GAAG,IAAI,IAAI,EAAE,CAAC;QAC3B,CAAC;IACH,CAAC;CACF;AA7CD,8BA6CC"}
@@ -0,0 +1,7 @@
1
+ import { LogEntry } from '../smartCron';
2
+ export declare class Logger {
3
+ private logs;
4
+ log(entry: LogEntry): void;
5
+ getLogs(): LogEntry[];
6
+ }
7
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../src/logger/logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAExC,qBAAa,MAAM;IACjB,OAAO,CAAC,IAAI,CAAkB;IAE9B,GAAG,CAAC,KAAK,EAAE,QAAQ,GAAG,IAAI;IAI1B,OAAO,IAAI,QAAQ,EAAE;CAGtB"}
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Logger = void 0;
4
+ class Logger {
5
+ constructor() {
6
+ this.logs = [];
7
+ }
8
+ log(entry) {
9
+ this.logs.push(entry);
10
+ }
11
+ getLogs() {
12
+ return this.logs;
13
+ }
14
+ }
15
+ exports.Logger = Logger;
16
+ //# sourceMappingURL=logger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":["../../../src/logger/logger.ts"],"names":[],"mappings":";;;AAEA,MAAa,MAAM;IAAnB;QACU,SAAI,GAAe,EAAE,CAAC;IAShC,CAAC;IAPC,GAAG,CAAC,KAAe;QACjB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACxB,CAAC;IAED,OAAO;QACL,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;CACF;AAVD,wBAUC"}
@@ -0,0 +1,5 @@
1
+ import { Job } from '../smartCron';
2
+ export declare class Notifier {
3
+ notifyFailure(error: Error, job: Job): void;
4
+ }
5
+ //# sourceMappingURL=notifier.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notifier.d.ts","sourceRoot":"","sources":["../../../src/notifier/notifier.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAEnC,qBAAa,QAAQ;IACnB,aAAa,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,GAAG,IAAI;CAK5C"}
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Notifier = void 0;
4
+ class Notifier {
5
+ notifyFailure(error, job) {
6
+ if (job.options.onFailure) {
7
+ job.options.onFailure(error, job);
8
+ }
9
+ }
10
+ }
11
+ exports.Notifier = Notifier;
12
+ //# sourceMappingURL=notifier.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notifier.js","sourceRoot":"","sources":["../../../src/notifier/notifier.ts"],"names":[],"mappings":";;;AAEA,MAAa,QAAQ;IACnB,aAAa,CAAC,KAAY,EAAE,GAAQ;QAClC,IAAI,GAAG,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;YAC1B,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QACpC,CAAC;IACH,CAAC;CACF;AAND,4BAMC"}
@@ -0,0 +1,4 @@
1
+ export declare class RetryManager {
2
+ runWithRetries(handler: () => Promise<void>, retries: number, delay: number): Promise<void>;
3
+ }
4
+ //# sourceMappingURL=retryManager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retryManager.d.ts","sourceRoot":"","sources":["../../../src/retryManager/retryManager.ts"],"names":[],"mappings":"AAAA,qBAAa,YAAY;IACjB,cAAc,CAClB,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,EAC5B,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,IAAI,CAAC;CAejB"}
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RetryManager = void 0;
4
+ class RetryManager {
5
+ async runWithRetries(handler, retries, delay) {
6
+ let attempts = 0;
7
+ while (attempts <= retries) {
8
+ try {
9
+ await handler();
10
+ return;
11
+ }
12
+ catch (error) {
13
+ attempts++;
14
+ if (attempts > retries) {
15
+ throw error;
16
+ }
17
+ await new Promise((resolve) => setTimeout(resolve, delay));
18
+ }
19
+ }
20
+ }
21
+ }
22
+ exports.RetryManager = RetryManager;
23
+ //# sourceMappingURL=retryManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retryManager.js","sourceRoot":"","sources":["../../../src/retryManager/retryManager.ts"],"names":[],"mappings":";;;AAAA,MAAa,YAAY;IACvB,KAAK,CAAC,cAAc,CAClB,OAA4B,EAC5B,OAAe,EACf,KAAa;QAEb,IAAI,QAAQ,GAAG,CAAC,CAAC;QACjB,OAAO,QAAQ,IAAI,OAAO,EAAE,CAAC;YAC3B,IAAI,CAAC;gBACH,MAAM,OAAO,EAAE,CAAC;gBAChB,OAAO;YACT,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,QAAQ,EAAE,CAAC;gBACX,IAAI,QAAQ,GAAG,OAAO,EAAE,CAAC;oBACvB,MAAM,KAAK,CAAC;gBACd,CAAC;gBACD,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC;YAC7D,CAAC;QACH,CAAC;IACH,CAAC;CACF;AApBD,oCAoBC"}
@@ -0,0 +1,11 @@
1
+ import { Job } from '../smartCron';
2
+ import { JobRunner } from '../jobRunner/jobRunner';
3
+ export declare class Scheduler {
4
+ private jobRunner;
5
+ constructor(jobRunner: JobRunner);
6
+ scheduleJob(job: Job): void;
7
+ startJob(job: Job): void;
8
+ stopJob(job: Job): void;
9
+ destroyJob(job: Job): void;
10
+ }
11
+ //# sourceMappingURL=scheduler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler.d.ts","sourceRoot":"","sources":["../../../src/scheduler/scheduler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AACnC,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD,qBAAa,SAAS;IACR,OAAO,CAAC,SAAS;gBAAT,SAAS,EAAE,SAAS;IAExC,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI;IAO3B,QAAQ,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI;IAMxB,OAAO,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI;IAMvB,UAAU,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI;CAK3B"}
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.Scheduler = void 0;
37
+ const cron = __importStar(require("node-cron"));
38
+ class Scheduler {
39
+ constructor(jobRunner) {
40
+ this.jobRunner = jobRunner;
41
+ }
42
+ scheduleJob(job) {
43
+ const scheduledJob = cron.schedule(job.cronExpression, () => {
44
+ this.jobRunner.run(job);
45
+ });
46
+ job.scheduledJob = scheduledJob;
47
+ }
48
+ startJob(job) {
49
+ if (job.scheduledJob) {
50
+ job.scheduledJob.start();
51
+ }
52
+ }
53
+ stopJob(job) {
54
+ if (job.scheduledJob) {
55
+ job.scheduledJob.stop();
56
+ }
57
+ }
58
+ destroyJob(job) {
59
+ if (job.scheduledJob) {
60
+ job.scheduledJob.stop();
61
+ }
62
+ }
63
+ }
64
+ exports.Scheduler = Scheduler;
65
+ //# sourceMappingURL=scheduler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler.js","sourceRoot":"","sources":["../../../src/scheduler/scheduler.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gDAAkC;AAIlC,MAAa,SAAS;IACpB,YAAoB,SAAoB;QAApB,cAAS,GAAT,SAAS,CAAW;IAAG,CAAC;IAE5C,WAAW,CAAC,GAAQ;QAClB,MAAM,YAAY,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,cAAc,EAAE,GAAG,EAAE;YAC1D,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAC1B,CAAC,CAAC,CAAC;QACH,GAAG,CAAC,YAAY,GAAG,YAAY,CAAC;IAClC,CAAC;IAED,QAAQ,CAAC,GAAQ;QACf,IAAI,GAAG,CAAC,YAAY,EAAE,CAAC;YACrB,GAAG,CAAC,YAAY,CAAC,KAAK,EAAE,CAAC;QAC3B,CAAC;IACH,CAAC;IAED,OAAO,CAAC,GAAQ;QACd,IAAI,GAAG,CAAC,YAAY,EAAE,CAAC;YACrB,GAAG,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;QAC1B,CAAC;IACH,CAAC;IAED,UAAU,CAAC,GAAQ;QACjB,IAAI,GAAG,CAAC,YAAY,EAAE,CAAC;YACrB,GAAG,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;QAC1B,CAAC;IACH,CAAC;CACF;AA3BD,8BA2BC"}
@@ -0,0 +1,40 @@
1
+ import * as cron from 'node-cron';
2
+ export interface JobOptions {
3
+ name?: string;
4
+ retries?: number;
5
+ retryDelay?: number;
6
+ preventOverlap?: boolean;
7
+ onFailure?: (error: Error, job: Job) => void;
8
+ }
9
+ export interface Job {
10
+ name: string;
11
+ cronExpression: string;
12
+ handler: () => Promise<void>;
13
+ options: JobOptions;
14
+ scheduledJob?: cron.ScheduledTask;
15
+ isRunning: boolean;
16
+ lastRun?: Date;
17
+ }
18
+ export interface LogEntry {
19
+ jobName: string;
20
+ startTime: Date;
21
+ endTime: Date;
22
+ duration: number;
23
+ status: 'success' | 'failure';
24
+ errorMessage?: string;
25
+ }
26
+ export declare class SmartCron {
27
+ private registry;
28
+ private logger;
29
+ private notifier;
30
+ private retryManager;
31
+ private jobRunner;
32
+ private scheduler;
33
+ schedule(cronExpression: string, handler: () => Promise<void>, options?: JobOptions): Job;
34
+ start(name: string): void;
35
+ stop(name: string): void;
36
+ remove(name: string): void;
37
+ listJobs(): Job[];
38
+ getLogs(): LogEntry[];
39
+ }
40
+ //# sourceMappingURL=smartCron.d.ts.map
@@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,GACvB,GAAG;IAiBN,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAMzB,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAMxB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAO1B,QAAQ,IAAI,GAAG,EAAE;IAIjB,OAAO,IAAI,QAAQ,EAAE;CAGtB"}
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SmartCron = void 0;
4
+ const jobRegistry_1 = require("./jobRegistry/jobRegistry");
5
+ const jobRunner_1 = require("./jobRunner/jobRunner");
6
+ const retryManager_1 = require("./retryManager/retryManager");
7
+ const logger_1 = require("./logger/logger");
8
+ const notifier_1 = require("./notifier/notifier");
9
+ const scheduler_1 = require("./scheduler/scheduler");
10
+ class SmartCron {
11
+ constructor() {
12
+ this.registry = new jobRegistry_1.JobRegistry();
13
+ this.logger = new logger_1.Logger();
14
+ this.notifier = new notifier_1.Notifier();
15
+ this.retryManager = new retryManager_1.RetryManager();
16
+ this.jobRunner = new jobRunner_1.JobRunner(this.logger, this.notifier, this.retryManager);
17
+ this.scheduler = new scheduler_1.Scheduler(this.jobRunner);
18
+ }
19
+ schedule(cronExpression, handler, options = {}) {
20
+ const name = options.name || `job-${Date.now()}`;
21
+ const job = {
22
+ name,
23
+ cronExpression,
24
+ handler,
25
+ options,
26
+ isRunning: false,
27
+ };
28
+ this.registry.add(job);
29
+ // Schedule the job
30
+ this.scheduler.scheduleJob(job);
31
+ return job;
32
+ }
33
+ start(name) {
34
+ const job = this.registry.get(name);
35
+ if (!job)
36
+ throw new Error(`Job ${name} not found`);
37
+ this.scheduler.startJob(job);
38
+ }
39
+ stop(name) {
40
+ const job = this.registry.get(name);
41
+ if (!job)
42
+ throw new Error(`Job ${name} not found`);
43
+ this.scheduler.stopJob(job);
44
+ }
45
+ remove(name) {
46
+ const job = this.registry.get(name);
47
+ if (!job)
48
+ throw new Error(`Job ${name} not found`);
49
+ this.scheduler.destroyJob(job);
50
+ this.registry.remove(name);
51
+ }
52
+ listJobs() {
53
+ return this.registry.list();
54
+ }
55
+ getLogs() {
56
+ return this.logger.getLogs();
57
+ }
58
+ }
59
+ exports.SmartCron = SmartCron;
60
+ //# sourceMappingURL=smartCron.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"smartCron.js","sourceRoot":"","sources":["../../src/smartCron.ts"],"names":[],"mappings":";;;AACA,2DAAwD;AACxD,qDAAkD;AAClD,8DAA2D;AAC3D,4CAAyC;AACzC,kDAA+C;AAC/C,qDAAkD;AA6BlD,MAAa,SAAS;IAAtB;QACU,aAAQ,GAAG,IAAI,yBAAW,EAAE,CAAC;QAC7B,WAAM,GAAG,IAAI,eAAM,EAAE,CAAC;QACtB,aAAQ,GAAG,IAAI,mBAAQ,EAAE,CAAC;QAC1B,iBAAY,GAAG,IAAI,2BAAY,EAAE,CAAC;QAClC,cAAS,GAAG,IAAI,qBAAS,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC;QACzE,cAAS,GAAG,IAAI,qBAAS,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAiDpD,CAAC;IA/CC,QAAQ,CACN,cAAsB,EACtB,OAA4B,EAC5B,UAAsB,EAAE;QAExB,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,OAAO,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;QACjD,MAAM,GAAG,GAAQ;YACf,IAAI;YACJ,cAAc;YACd,OAAO;YACP,OAAO;YACP,SAAS,EAAE,KAAK;SACjB,CAAC;QACF,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAEvB,mBAAmB;QACnB,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;QAEhC,OAAO,GAAG,CAAC;IACb,CAAC;IAED,KAAK,CAAC,IAAY;QAChB,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACpC,IAAI,CAAC,GAAG;YAAE,MAAM,IAAI,KAAK,CAAC,OAAO,IAAI,YAAY,CAAC,CAAC;QACnD,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;IAC/B,CAAC;IAED,IAAI,CAAC,IAAY;QACf,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACpC,IAAI,CAAC,GAAG;YAAE,MAAM,IAAI,KAAK,CAAC,OAAO,IAAI,YAAY,CAAC,CAAC;QACnD,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAC9B,CAAC;IAED,MAAM,CAAC,IAAY;QACjB,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACpC,IAAI,CAAC,GAAG;YAAE,MAAM,IAAI,KAAK,CAAC,OAAO,IAAI,YAAY,CAAC,CAAC;QACnD,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QAC/B,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;IAC9B,CAAC;IAED,OAAO;QACL,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;IAC/B,CAAC;CACF;AAvDD,8BAuDC"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "cron-guardian",
3
+ "version": "1.0.0",
4
+ "description": "A reliable cron job manager for Node.js applications with retries, monitoring, and job control",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "test": "jest",
10
+ "prepare": "npm run build"
11
+ },
12
+ "keywords": [
13
+ "cron",
14
+ "scheduler",
15
+ "nodejs",
16
+ "jobs",
17
+ "retries"
18
+ ],
19
+ "author": "Nisarg Satasiya",
20
+ "license": "MIT",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/NISARG-J/smart-cron-manager"
24
+ },
25
+ "homepage": "https://github.com/NISARG-J/smart-cron-manager#readme",
26
+ "bugs": {
27
+ "url": "https://github.com/NISARG-J/smart-cron-manager/issues"
28
+ },
29
+ "files": [
30
+ "dist/",
31
+ "README.md",
32
+ "LICENSE",
33
+ "DOCS.md",
34
+ "CONTRIBUTING.md"
35
+ ],
36
+ "dependencies": {
37
+ "node-cron": "^3.0.3"
38
+ },
39
+ "devDependencies": {
40
+ "@types/jest": "^29.5.12",
41
+ "@types/node": "^20.11.24",
42
+ "@types/node-cron": "^3.0.11",
43
+ "jest": "^29.7.0",
44
+ "ts-jest": "^29.1.2",
45
+ "typescript": "^5.3.3"
46
+ },
47
+ "engines": {
48
+ "node": ">=18.0.0"
49
+ }
50
+ }