croner 4.0.74 → 4.0.79

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
@@ -1,47 +1,100 @@
1
1
  <p align="center">
2
- <img src="https://cdn.jsdelivr.net/gh/hexagon/croner@master/croner.png" alt="Croner" width="150" height="108"><br>
3
- Trigger functions in javascript using Cron syntax.<br><br>Try it live on <a href="https://jsfiddle.net/hexag0n/hoa8kwsb/">jsfiddle</a>.<br>
2
+ <img src="/croner.png" alt="Croner" width="150" height="150"><br>
3
+ Trigger functions in JavaScript using Cron syntax.<br><br>Try it live on <a href="https://jsfiddle.net/hexag0n/hoa8kwsb/">jsfiddle</a>.<br>
4
4
  </p>
5
5
 
6
6
  # Croner
7
7
 
8
8
  ![Node.js CI](https://github.com/Hexagon/croner/workflows/Node.js%20CI/badge.svg?branch=master) [![npm version](https://badge.fury.io/js/croner.svg)](https://badge.fury.io/js/croner) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/4978bdbf495941c087ecb32b120f28ff)](https://www.codacy.com/gh/Hexagon/croner/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=Hexagon/croner&amp;utm_campaign=Badge_Grade)
9
- [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Hexagon/croner/blob/master/LICENSE) [![jsdelivr](https://data.jsdelivr.com/v1/package/npm/croner/badge?style=rounded)](https://www.jsdelivr.com/package/npm/croner)
9
+ [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Hexagon/croner/blob/master/LICENSE) [![jsdelivr](https://data.jsdelivr.com/v1/package/gh/hexagon/croner/badge?style=rounded)](https://www.jsdelivr.com/package/gh/hexagon/croner)
10
+ [![Support Server](https://img.shields.io/discord/912830749948915713.svg?color=7289da&label=Croner&logo=discord&style=flat-square)](https://discord.gg/VgpuJ48K8K)
10
11
 
11
- * Trigger functions in JavaScript using [Cron](https://en.wikipedia.org/wiki/Cron#CRON_expression) syntax
12
- * Pause, resume or stop execution efter a task is scheduled
12
+ * Trigger functions in JavaScript using [Cron](https://en.wikipedia.org/wiki/Cron#CRON_expression) syntax.
13
+ * Pause, resume or stop execution after a task is scheduled.
13
14
  * Find first date of next month, find date of next tuesday, etc.
14
- * Works in Node.js >=4.0 (both require and import)
15
- * Works in Deno >=1.16
15
+ * Works in Node.js >=4.0 (both require and import).
16
+ * Works in Deno >=1.16.
16
17
  * Works in browsers as standalone, UMD or ES-module.
17
- * **Experimental feature:** Schedule in specific target timezones
18
- * Includes [TypeScript](https://www.typescriptlang.org/) typings
18
+ * **Experimental feature:** Schedule in specific target timezones.
19
+ * Includes [TypeScript](https://www.typescriptlang.org/) typings.
19
20
 
20
21
  Quick examples:
21
22
 
22
23
  ```javascript
23
- // Run a function at the interval set by a cron expression
24
- let job = Cron('* * * * * *', () => {
24
+ // Run a function at the interval defined by a cron expression
25
+ const job = Cron('* * * * * *', () => {
25
26
  console.log('This will run every second');
26
27
  });
27
28
 
28
29
  // What date is next sunday?
29
- let nextSunday = Cron('0 0 0 * * 7').next();
30
+ const nextSunday = Cron('0 0 0 * * 7').next();
30
31
  console.log(nextSunday.toLocaleDateString());
31
32
 
32
33
  // How many days left to christmas eve?
33
- let msLeft = Cron('59 59 23 24 DEC *').next() - new Date();
34
+ const msLeft = Cron('59 59 23 24 DEC *').next() - new Date();
34
35
  console.log(Math.floor(msLeft/1000/3600/24) + " days left to next christmas eve");
35
36
  ```
36
37
 
37
38
  More [examples](#examples)...
38
39
 
40
+ ## Why another javascript cron implementation?
41
+
42
+ Because the existing ones isn't good enough. They have serious bugs, use bloated dependencies, does not work in all environments and/or just doesn't work as expected.
43
+
44
+ Benchmark at 2021-11-26:
45
+
46
+ ```
47
+ > node cron-implementation-test.js
48
+
49
+ Test: When is next saturday 29th of february, pattern '0 0 0 29 2 6'
50
+
51
+ node-schedule: 2022-02-05 00:00:00 in 37.77ms
52
+ node-cron: ??? in 6.746ms
53
+ cron: 2022-03-05 00:00:00 in 8.282ms
54
+ croner: 2048-02-29 00:00:00 in 2.676ms
55
+
56
+ > node cron-implementation-test.js
57
+
58
+ Test: When is next 15th of february, pattern '0 0 0 15 2 *'
59
+
60
+ node-schedule: 2022-02-15 00:00:00 in 91.834ms
61
+ node-cron: ??? in 7.69ms
62
+ cron: 2022-03-15 00:00:00 in 12.354ms
63
+ croner: 2022-02-15 00:00:00 in 4.398ms
64
+
65
+ Test: When is next monday in october, pattern '0 0 0 * 10 1'
66
+
67
+ node-schedule: 2022-10-03 00:00:00 in 244.057ms
68
+ node-cron: ??? in 12.001ms
69
+ cron: 2021-11-29 00:00:00 in 19.322ms
70
+ croner: 2022-10-03 00:00:00 in 5.394ms
71
+
72
+ Test: When is next monday the 1st of any month, pattern '0 0 0 1 * 1'
73
+
74
+ node-schedule: 2021-11-29 00:00:00 in 39.591ms
75
+ node-cron: ??? in 7.078ms
76
+ cron: 2021-11-29 00:00:00 in 10.107ms
77
+ croner: 2022-08-01 00:00:00 in 9.473ms
78
+
79
+ Test: When is 23:00 next 31st march, pattern '0 0 23 31 3 *'
80
+
81
+ node-schedule: 2022-03-31 23:00:00 in 87.661ms
82
+ node-cron: ??? in 10.299ms
83
+ Month '3' is limited to '30' days.
84
+ cron: 2022-04-01 23:00:00 in 25.756ms
85
+ croner: 2022-03-31 23:00:00 in 5.569ms
86
+
87
+ ```
88
+ https://gist.github.com/Hexagon/703f85f2dd86443cc17eef8f5cc6cb70
89
+
39
90
  ## Installation
40
91
 
41
92
  ### Node.js
42
93
 
43
94
  ```npm install croner --save```
44
95
 
96
+ JavaScript
97
+
45
98
  ```javascript
46
99
  // ESM Import ...
47
100
  import Cron from "croner";
@@ -50,6 +103,18 @@ import Cron from "croner";
50
103
  const Cron = require("croner");
51
104
  ```
52
105
 
106
+ TypeScript
107
+
108
+ *Note that only default export is available in Node.js TypeScript, as the commonjs module is used internally.*
109
+
110
+ ```typescript
111
+ import Cron from "croner";
112
+
113
+ const scheduler : Cron = new Cron("* * * * * *", () => {
114
+ console.log("This will run every second.");
115
+ });
116
+ ```
117
+
53
118
  ### Deno
54
119
 
55
120
  JavaScript
@@ -67,7 +132,7 @@ TypeScript
67
132
  ```typescript
68
133
  import { Cron } from "https://cdn.jsdelivr.net/gh/hexagon/croner@4/src/croner.js";
69
134
 
70
- let scheduler : Cron = new Cron("* * * * * *", () => {
135
+ const _scheduler : Cron = new Cron("* * * * * *", () => {
71
136
  console.log("This will run every second.");
72
137
  });
73
138
  ```
@@ -112,16 +177,16 @@ Cron takes three arguments
112
177
  * scheduled function (optional)
113
178
 
114
179
  ```javascript
115
- var job = Cron("* * * * * *" , /*optional*/ { maxRuns: 1 } , /*optional*/ () => {} );
180
+ const job = Cron("* * * * * *" , /*optional*/ { maxRuns: 1 } , /*optional*/ () => {} );
116
181
 
117
182
  // If function is omitted in constructor, it can be scheduled later
118
- job.schedule(() => {});
183
+ job.schedule((/* optional */ job, /* optional */ context) => {});
119
184
 
120
185
  // States
121
- let nextRun = job.next( /*optional*/ previousRun ); // Get a Date object representing next run
122
- let prevRun = job.previous( );
123
- let msToNext = job.msToNext( /*optional*/ previosRun ); // Milliseconds left to next execution
124
- let isRunning = job.running();
186
+ const nextRun = job.next( /*optional*/ previousRun ); // Get a Date object representing next run
187
+ const prevRun = job.previous( );
188
+ const msToNext = job.msToNext( /*optional*/ previousRun ); // Milliseconds left to next execution
189
+ const isRunning = job.running();
125
190
 
126
191
  // Control scheduled execution
127
192
  job.pause();
@@ -140,6 +205,7 @@ job.stop();
140
205
  | startAt | undefined | String | ISO 8601 formatted datetime (2021-10-17T23:43:00)<br>in local or specified timezone |
141
206
  | stopAt | undefined | String | ISO 8601 formatted datetime (2021-10-17T23:43:00)<br>in local or specified timezone |
142
207
  | paused | false | Boolean | If the job should be paused from start. |
208
+ | context | undefined | Any | Passed as the second parameter to triggered function |
143
209
 
144
210
  #### Pattern
145
211
 
@@ -171,16 +237,17 @@ job.stop();
171
237
  #### Expressions
172
238
  ```javascript
173
239
  // Run a function according to pattern
174
- Cron('0-4 */5 1,2,3 * JAN-MAR SAT', function () {
175
- console.log('This will run the first five seconds every fifth minute');
176
- console.log('of hour 1,2 and 3 every saturday in January to March.');
240
+ Cron('15-45/10 */5 1,2,3 * JAN-MAR SAT', function () {
241
+ console.log('This will run every tenth second between second 15-45');
242
+ console.log('every fifth minute of hour 1,2 and 3');
243
+ console.log('every saturday in January to March.');
177
244
  });
178
245
  ```
179
246
 
180
247
  #### Find dates
181
248
  ```javascript
182
249
  // Find next month
183
- let nextMonth = Cron('0 0 0 1 * *').next(),
250
+ const nextMonth = Cron('0 0 0 1 * *').next(),
184
251
  nextSunday = Cron('0 0 0 * * 7').next(),
185
252
  nextSat29feb = Cron("0 0 0 29 2 6").next();
186
253
 
@@ -192,7 +259,7 @@ console.log("Next saturday at 29th of february: " + nextSat29feb.toLocaleDateSt
192
259
  #### With options
193
260
  ```javascript
194
261
 
195
- var job = Cron(
262
+ const job = Cron(
196
263
  '* * * * *',
197
264
  {
198
265
  maxRuns: Infinity,
@@ -208,7 +275,7 @@ var job = Cron(
208
275
 
209
276
  #### Job controls
210
277
  ```javascript
211
- let job = Cron('* * * * * *', (self) => {
278
+ const job = Cron('* * * * * *', (self) => {
212
279
  console.log('This will run every second. Pause on second 10. Resume on second 15. And quit on second 20.');
213
280
  console.log('Current second: ', new Date().getSeconds());
214
281
  console.log('Previous run: ' + self.previous());
@@ -220,6 +287,27 @@ Cron('15 * * * * *', {maxRuns: 1}, () => job.resume());
220
287
  Cron('20 * * * * *', {maxRuns: 1}, () => job.stop());
221
288
  ```
222
289
 
290
+ #### Passing a context
291
+ ```javascript
292
+ const data = {
293
+ what: "stuff"
294
+ };
295
+
296
+ Cron('* * * * * *', { context: data }, (_self, context) => {
297
+ console.log('This will print stuff: ' + context.what);
298
+ });
299
+
300
+ Cron('*/5 * * * * *', { context: data }, (self, context) => {
301
+ console.log('After this, other stuff will be printed instead');
302
+ context.what = "other stuff";
303
+ self.stop();
304
+ });
305
+ ```
306
+
307
+ ## Contributing
308
+
309
+ See [Contribution Guide](/CONTRIBUTING.md)
310
+
223
311
  ## License
224
312
 
225
313
  MIT
package/croner.png CHANGED
Binary file