cron 2.2.0 → 2.3.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/CHANGELOG.md CHANGED
File without changes
package/LICENSE CHANGED
File without changes
package/README.md CHANGED
@@ -1,7 +1,6 @@
1
- # Looking for maintainers/contributors
2
-
3
- This project is looking for help! If you're interested in helping with the project please reach out to me [ncb000gt](https://twitter.com/ncb000gt) and let me know. I'd love for it to continue on, but it needs a lot of attention.
4
-
1
+ <p align="center">
2
+ <img src="logo.svg" alt="Node Cron Alarm Clock Star Logo" width="100" />
3
+ </p>
5
4
 
6
5
  # node-cron
7
6
 
@@ -13,178 +12,123 @@ This project is looking for help! If you're interested in helping with the proje
13
12
  [![Known Vulnerabilities](https://snyk.io/test/github/kelektiv/node-cron/badge.svg)](https://snyk.io/test/github/kelektiv/node-cron)
14
13
  [![Minified size](https://badgen.net/bundlephobia/min/cron)](https://badgen.net/bundlephobia/min/cron)
15
14
  [![Minzipped size](https://badgen.net/bundlephobia/minzip/cron)](https://badgen.net/bundlephobia/minzip/cron)
16
- [![monthly downloads](https://badgen.net/npm/dm/cron?icon=npm)](https://badgen.net/npm/dm/cron)
17
-
18
- Cron is a tool that allows you to execute _something_ on a schedule. This is
19
- typically done using the cron syntax. We allow you to execute a function
20
- whenever your scheduled job triggers. We also allow you to execute a job
21
- external to the javascript process using `child_process`. Additionally, this
22
- library goes beyond the basic cron syntax and allows you to
23
- supply a Date object. This will be used as the trigger for your callback. Cron
24
- syntax is still an acceptable CronTime format. Although the Cron patterns
25
- supported here extend on the standard Unix format to support seconds digits,
26
- leaving it off will default to 0 and match the Unix behavior.
15
+ [![monthly downloads](https://badgen.net/npm/dm/cron?icon=npm)](https://badgen.net/npm/dm/cron)
27
16
 
28
- # Installation
17
+ Cron is a tool that allows you to execute _something_ on a schedule. This is typically done using the cron syntax. We allow you to:
29
18
 
30
- npm install cron
19
+ - execute a function whenever your scheduled job triggers
20
+ - execute a job external to the javascript process using `child_process`
21
+ - use a Date object instead of cron syntax as the trigger for your callback
22
+ - use an additional slot for seconds (leaving it off will default to 0 and match the Unix behavior)
31
23
 
32
- # If You Are Submitting Bugs/Issues
24
+ ## Installation
33
25
 
34
- Because we can't magically know what you are doing to expose an issue, it is
35
- best if you provide a snippet of code. This snippet need not include your secret
36
- sauce, but it must replicate the issue you are describing. The issues that get
37
- closed without resolution tend to be the ones without code examples. Thanks.
26
+ ```
27
+ npm install cron
28
+ ```
38
29
 
39
- # Versions and Backwards compatibility breaks:
30
+ ## Versions and Backwards compatibility breaks
40
31
 
41
- As goes with semver, breaking backwards compatibility should be explicit in the
42
- versioning of your library. As such, we'll upgrade the version of this module
43
- in accordance with breaking changes (I'm not always great about doing it this
44
- way so if you notice that there are breaking changes that haven't been bumped
45
- appropriately please let me know).
32
+ As goes with semver, breaking backwards compatibility should be explicit in the versioning of your library. As such, we'll upgrade the version of this module in accordance with breaking changes (We're not always great about doing it this way so if you notice that there are breaking changes that haven't been bumped appropriately please let us know).
46
33
 
47
- # Usage (basic cron usage):
34
+ ## Usage (basic cron usage)
48
35
 
49
36
  ```javascript
50
37
  var CronJob = require('cron').CronJob;
51
38
  var job = new CronJob(
52
- '* * * * * *',
53
- function() {
54
- console.log('You will see this message every second');
55
- },
56
- null,
57
- true,
58
- 'America/Los_Angeles'
39
+ '* * * * * *',
40
+ function() {
41
+ console.log('You will see this message every second');
42
+ },
43
+ null,
44
+ true,
45
+ 'America/Los_Angeles'
59
46
  );
60
- // Use this if the 4th param is default value(false)
61
- // job.start()
62
- ```
47
+ // job.start() - See note below when to use this
48
+ ```
63
49
 
64
- Note - You don't need to explicitly start a job in order to make it run since the 4th parameter is set to `true`. However, by default you need to call `job.start()` to start the cron job, which gives a little more control over running your jobs.
50
+ Note - In the example above, the 4th parameter of `CronJob()` automatically starts the job on initialization. If this parameter is falsy or not provided, the job needs to be explicitly started using `job.start()`.
65
51
 
66
- There are more examples available in this repository at:
67
- [/examples](https://github.com/kelektiv/node-cron/tree/master/examples)
52
+ There are more examples available in this repository at: [/examples](https://github.com/kelektiv/node-cron/tree/master/examples)
68
53
 
69
- # Available Cron patterns:
54
+ ## Available Cron patterns
70
55
 
71
- Asterisk. E.g. *
72
- Ranges. E.g. 1-3,5
73
- Steps. E.g. */2
56
+ ```
57
+ Asterisks e.g. *
58
+ Ranges e.g. 1-3,5
59
+ Steps e.g. */2
60
+ ```
74
61
 
75
- [Read up on cron patterns here](http://crontab.org). Note the examples in the
76
- link have five fields, and 1 minute as the finest granularity, but this library
77
- has six fields, with 1 second as the finest granularity.
62
+ [Read up on cron patterns here](http://crontab.org). Note the examples in the link have five fields, and 1 minute as the finest granularity, but this library has six fields, with 1 second as the finest granularity.
78
63
 
79
- There are tools that help when constructing your cronjobs. You might find
80
- something like https://crontab.guru/ or https://cronjob.xyz/ helpful. But,
81
- note that these don't necessarily accept the exact same syntax as this
82
- library, for instance, it doesn't accept the `seconds` field, so keep that in
83
- mind.
64
+ There are tools that help when constructing your cronjobs. You might find something like https://crontab.guru/ or https://cronjob.xyz/ helpful. But, note that these don't necessarily accept the exact same syntax as this library, for instance, it doesn't accept the `seconds` field, so keep that in mind.
84
65
 
85
- # Cron Ranges
66
+ ### Cron Ranges
86
67
 
87
- When specifying your cron values you'll need to make sure that your values fall
88
- within the ranges. For instance, some cron's use a 0-7 range for the day of
89
- week where both 0 and 7 represent Sunday. We do not. And that is an optimisation.
68
+ When specifying your cron values you'll need to make sure that your values fall within the ranges. For instance, some cron's use a 0-7 range for the day of week where both 0 and 7 represent Sunday. We do not. And that is an optimisation.
90
69
 
91
70
  - Seconds: 0-59
92
71
  - Minutes: 0-59
93
72
  - Hours: 0-23
94
73
  - Day of Month: 1-31
95
74
  - Months: 0-11 (Jan-Dec)
96
- - Day of Week: 0-6 (Sun-Sat)
97
-
98
- # Gotchas
99
-
100
- - Millisecond level granularity in JS or moment date objects.
101
- Because computers take time to do things, there may be some delay in execution.
102
- This should be on the order of milliseconds. This module doesn't allow MS level
103
- granularity for the regular cron syntax, but _does_ allow you to specify a real
104
- date of execution in either a javascript date object or a moment object.
105
- When this happens you may find that you aren't able to execute a job that
106
- _should_ run in the future like with `new Date().setMilliseconds(new Date().getMilliseconds() + 1)`. This is due to those cycles of execution
107
- above. This wont be the same for everyone because of compute speed. When I
108
- tried it locally I saw that somewhere around the 4-5 ms mark was where I got
109
- consistent ticks using real dates, but anything less than that would result
110
- in an exception. This could be really confusing. We could restrict the
111
- granularity for all dates to seconds, but felt that it wasn't a huge problem
112
- so long as you were made aware. If this becomes more of an issue, We can
113
- revisit it.
114
- - Arrow Functions for `onTick`
115
- Arrow functions get their `this` context from their parent scope. Thus, if you use them, you will not get
116
- the `this` context of the cronjob. You can read a little more in this ticket [GH-40](https://github.com/kelektiv/node-cron/issues/47#issuecomment-459762775)
117
-
118
- # API
119
-
120
- Parameter Based
75
+ - Day of Week: 0-6 (Sun-Sat)
76
+
77
+ ## Gotchas
78
+
79
+ - Millisecond level granularity in JS `Date` or Luxon `DateTime` objects: Because computers take time to do things, there may be some delay in execution. This should be on the order of milliseconds. This module doesn't allow MS level granularity for the regular cron syntax, but _does_ allow you to specify a real date of execution in either a javascript `Date` object or a Luxon `DateTime` object. When this happens you may find that you aren't able to execute a job that _should_ run in the future like with `new Date().setMilliseconds(new Date().getMilliseconds() + 1)`. This is due to those cycles of execution above. This wont be the same for everyone because of compute speed. When we tried it locally we saw that somewhere around the 4-5 ms mark was where we got consistent ticks using real dates, but anything less than that would result in an exception. This could be really confusing. We could restrict the granularity for all dates to seconds, but felt that it wasn't a huge problem so long as you were made aware. If this becomes more of an issue, We can revisit it.
80
+ - Arrow Functions for `onTick`: Arrow functions get their `this` context from their parent scope. Thus, if you use them, you will not get the `this` context of the cronjob. You can read a little more in issue [GH-47](https://github.com/kelektiv/node-cron/issues/47#issuecomment-459762775)
81
+
82
+ ## API
83
+
84
+ Parameter Based
121
85
 
122
86
  - `job` - shortcut to `new cron.CronJob()`.
123
87
  - `time` - shortcut to `new cron.CronTime()`.
124
88
  - `sendAt` - tells you when a `CronTime` will be run.
125
89
  - `timeout` - tells you when the next timeout is.
126
90
  - `CronJob`
127
- - `constructor(cronTime, onTick, onComplete, start, timezone, context, runOnInit, utcOffset, unrefTimeout)` - Of note, the first parameter here can be a JSON object that
128
- has the below names and associated types (see examples above).
129
- - `cronTime` - [REQUIRED] - The time to fire off your job. This can be in
130
- the form of cron syntax or a JS
131
- [Date](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) object.
132
- - `onTick` - [REQUIRED] - The function to fire at the specified time. If an
133
- `onComplete` callback was provided, `onTick` will receive it as an argument.
134
- `onTick` may call `onComplete` when it has finished its work.
135
- - `onComplete` - [OPTIONAL] - A function that will fire when the job is
136
- stopped with `job.stop()`, and may also be called by `onTick` at the end of each run.
137
- - `start` - [OPTIONAL] - Specifies whether to start the job just before
138
- exiting the constructor. By default this is set to false. If left at default
139
- you will need to call `job.start()` in order to start the job (assuming
140
- `job` is the variable you set the cronjob to). This does not immediately
141
- fire your `onTick` function, it just gives you more control over the
142
- behavior of your jobs.
143
- - `timeZone` - [OPTIONAL] - Specify the timezone for the execution. This
144
- will modify the actual time relative to your timezone. If the timezone is
145
- invalid, an error is thrown. You can check all timezones available at
146
- [Moment Timezone Website](http://momentjs.com/timezone/). Probably don't use
147
- both `timeZone` and `utcOffset` together or weird things may happen.
148
- - `context` - [OPTIONAL] - The context within which to execute the onTick
149
- method. This defaults to the cronjob itself allowing you to call
150
- `this.stop()`. However, if you change this you'll have access to the
151
- functions and values within your context object.
152
- - `runOnInit` - [OPTIONAL] - This will immediately fire your `onTick`
153
- function as soon as the requisite initialization has happened. This option
154
- is set to `false` by default for backwards compatibility.
155
- - `utcOffset` - [OPTIONAL] - This allows you to specify the offset of your
156
- timezone rather than using the `timeZone` param. Probably don't use both
157
- `timeZone` and `utcOffset` together or weird things may happen.
158
- - `unrefTimeout` - [OPTIONAL] - If you have code that keeps the event loop
159
- running and want to stop the node process when that finishes regardless of
160
- the state of your cronjob, you can do so making use of this parameter. This
161
- is off by default and cron will run as if it needs to control the event
162
- loop. For more information take a look at
163
- [timers#timers_timeout_unref](https://nodejs.org/api/timers.html#timers_timeout_unref)
164
- from the NodeJS docs.
165
- - `start` - Runs your job.
166
- - `stop` - Stops your job.
167
- - `setTime` - Stops and changes the time for the `CronJob`. Param must be a `CronTime`.
168
- - `lastDate` - Tells you the last execution date.
169
- - `nextDates` - Provides an array of the next set of dates that will trigger an `onTick`.
170
- - `fireOnTick` - Allows you to override the `onTick` calling behavior. This
171
- matters so only do this if you have a really good reason to do so.
172
- - `addCallback` - Allows you to add `onTick` callbacks.
91
+ - `constructor(cronTime, onTick, onComplete, start, timezone, context, runOnInit, utcOffset, unrefTimeout)` - Of note, the first parameter here can be a JSON object that has the below names and associated types (see examples above).
92
+ - `cronTime` - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS [Date](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) object.
93
+ - `onTick` - [REQUIRED] - The function to fire at the specified time. If an `onComplete` callback was provided, `onTick` will receive it as an argument. `onTick` may call `onComplete` when it has finished its work.
94
+ - `onComplete` - [OPTIONAL] - A function that will fire when the job is stopped with `job.stop()`, and may also be called by `onTick` at the end of each run.
95
+ - `start` - [OPTIONAL] - Specifies whether to start the job just before exiting the constructor. By default this is set to false. If left at default you will need to call `job.start()` in order to start the job (assuming `job` is the variable you set the cronjob to). This does not immediately fire your `onTick` function, it just gives you more control over the behavior of your jobs.
96
+ - `timeZone` - [OPTIONAL] - Specify the timezone for the execution. This will modify the actual time relative to your timezone. If the timezone is invalid, an error is thrown. You can check all timezones available at [Moment Timezone Website](http://momentjs.com/timezone/). Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
97
+ - `context` - [OPTIONAL] - The context within which to execute the onTick method. This defaults to the cronjob itself allowing you to call `this.stop()`. However, if you change this you'll have access to the functions and values within your context object.
98
+ - `runOnInit` - [OPTIONAL] - This will immediately fire your `onTick` function as soon as the requisite initialization has happened. This option is set to `false` by default for backwards compatibility.
99
+ - `utcOffset` - [OPTIONAL] - This allows you to specify the offset of your timezone rather than using the `timeZone` param. This should be an integer amount representing the number of minutes offset (like `120` for +2 hours or `-90` for -1.5 hours) Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
100
+ - `unrefTimeout` - [OPTIONAL] - If you have code that keeps the event loop running and want to stop the node process when that finishes regardless of the state of your cronjob, you can do so making use of this parameter. This is off by default and cron will run as if it needs to control the event loop. For more information take a look at [timers#timers_timeout_unref](https://nodejs.org/api/timers.html#timers_timeout_unref) from the NodeJS docs.
101
+ - `start` - Runs your job.
102
+ - `stop` - Stops your job.
103
+ - `setTime` - Stops and changes the time for the `CronJob`. Param must be a `CronTime`.
104
+ - `lastDate` - Tells you the last execution date.
105
+ - `nextDates` - Provides an array of the next set of dates that will trigger an `onTick`.
106
+ - `fireOnTick` - Allows you to override the `onTick` calling behavior. This matters so only do this if you have a really good reason to do so.
107
+ - `addCallback` - Allows you to add `onTick` callbacks.
173
108
  - `CronTime`
174
- - `constructor(time)`
175
- - `time` - [REQUIRED] - The time to fire off your job. This can be in the
176
- form of cron syntax or a JS
177
- [Date](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date)
178
- object.
109
+ - `constructor(time)`
110
+ - `time` - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS [Date](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) object.
111
+
112
+ ## Community
113
+
114
+ Join the [Discord server](https://discord.gg/yyKns29zch)! Here you can discuss issues and get help in a more casual forum than GitHub.
115
+
116
+ ## Contributing
117
+
118
+ ### Looking for maintainers/contributors
119
+
120
+ This project is looking for help! If you're interested in helping with the project please reach out to me (ncb000gt) on [Twitter](https://twitter.com/ncb000gt). We'd love for it to continue on, but it needs a lot of attention. You can also join the [Discord server](https://discord.gg/yyKns29zch) to learn more about what needs to be done.
121
+
122
+ ### Submitting Bugs/Issues
123
+
124
+ Before submitting a bug, please search the existing issues, [Discord](https://discord.gg/yyKns29zch) conversations, and the web to see if someone else has run into the same issue before.
125
+
126
+ Because we can't magically know what you are doing to expose an issue, it is best if you provide a snippet of code. This snippet need not include your secret sauce, but it must replicate the issue you are describing. The issues that get closed without resolution tend to be the ones without code examples. Thanks.
179
127
 
180
- # Contributions
128
+ ### Acknowledgements
181
129
 
182
- This is a community effort project. In the truest sense, this project started as
183
- an open source project from [cron.js](http://github.com/padolsey/cron.js) and
184
- grew into something else. Other people have contributed code, time, and
185
- oversight to the project. At this point there are too many to name here so I'll
186
- just say thanks.
130
+ This is a community effort project. In the truest sense, this project started as an open source project from [cron.js](http://github.com/padolsey/cron.js) and grew into something else. Other people have contributed code, time, and oversight to the project. At this point there are too many to name here so We'll just say thanks.
187
131
 
188
- # License
132
+ ## License
189
133
 
190
134
  MIT
package/lib/cron.js CHANGED
@@ -7,8 +7,8 @@
7
7
  root.Cron = factory(root.luxon);
8
8
  }
9
9
  })(this, function (luxon, childProcess) {
10
- var exports = {};
11
- var spawn = childProcess && childProcess.spawn;
10
+ const exports = {};
11
+ const spawn = childProcess && childProcess.spawn;
12
12
  const CronTime = require('./time')(luxon);
13
13
  const CronJob = require('./job')(CronTime, spawn);
14
14
 
package/lib/job.js CHANGED
@@ -1,7 +1,7 @@
1
1
  function CronJob(CronTime, spawn) {
2
2
  function fnWrap(cmd) {
3
- var command;
4
- var args;
3
+ let command;
4
+ let args;
5
5
 
6
6
  switch (typeof cmd) {
7
7
  case 'string':
@@ -14,7 +14,7 @@ function CronJob(CronTime, spawn) {
14
14
  command = cmd && cmd.command;
15
15
  if (command) {
16
16
  args = cmd.args;
17
- var options = cmd.options;
17
+ const options = cmd.options;
18
18
 
19
19
  return spawn.bind(undefined, command, args, options);
20
20
  }
@@ -35,9 +35,9 @@ function CronJob(CronTime, spawn) {
35
35
  utcOffset,
36
36
  unrefTimeout
37
37
  ) {
38
- var _cronTime = cronTime;
39
- var argCount = 0;
40
- for (var i = 0; i < arguments.length; i++) {
38
+ let _cronTime = cronTime;
39
+ let argCount = 0;
40
+ for (let i = 0; i < arguments.length; i++) {
41
41
  if (arguments[i] !== undefined) {
42
42
  argCount++;
43
43
  }
@@ -76,7 +76,7 @@ function CronJob(CronTime, spawn) {
76
76
  return this;
77
77
  }
78
78
 
79
- var addCallback = function (callback) {
79
+ const addCallback = function (callback) {
80
80
  if (typeof callback === 'function') {
81
81
  this._callbacks.push(callback);
82
82
  }
@@ -88,17 +88,18 @@ function CronJob(CronTime, spawn) {
88
88
  // crontime is an object...
89
89
  throw new Error('time must be an instance of CronTime.');
90
90
  }
91
+ const wasRunning = this.running;
91
92
  this.stop();
92
93
  this.cronTime = time;
93
- this.start();
94
+ if (wasRunning) this.start();
94
95
  };
95
96
 
96
97
  CJ.prototype.nextDate = function () {
97
98
  return this.cronTime.sendAt();
98
99
  };
99
100
 
100
- var fireOnTick = function () {
101
- for (var i = this._callbacks.length - 1; i >= 0; i--) {
101
+ const fireOnTick = function () {
102
+ for (let i = this._callbacks.length - 1; i >= 0; i--) {
102
103
  this._callbacks[i].call(this.context, this.onComplete);
103
104
  }
104
105
  };
@@ -108,16 +109,16 @@ function CronJob(CronTime, spawn) {
108
109
  return this.cronTime.sendAt(i);
109
110
  };
110
111
 
111
- var start = function () {
112
+ const start = function () {
112
113
  if (this.running) {
113
114
  return;
114
115
  }
115
116
 
116
- var MAXDELAY = 2147483647; // The maximum number of milliseconds setTimeout will wait.
117
- var self = this;
118
- var timeout = this.cronTime.getTimeout();
119
- var remaining = 0;
120
- var startTime;
117
+ const MAXDELAY = 2147483647; // The maximum number of milliseconds setTimeout will wait.
118
+ const self = this;
119
+ let timeout = this.cronTime.getTimeout();
120
+ let remaining = 0;
121
+ let startTime;
121
122
 
122
123
  if (this.cronTime.realDate) {
123
124
  this.runOnce = true;
@@ -134,10 +135,10 @@ function CronJob(CronTime, spawn) {
134
135
  // The callback wrapper checks if it needs to sleep another period or not
135
136
  // and does the real callback logic when it's time.
136
137
  function callbackWrapper() {
137
- var diff = startTime + timeout - Date.now();
138
+ const diff = startTime + timeout - Date.now();
138
139
 
139
140
  if (diff > 0) {
140
- var newTimeout = self.cronTime.getTimeout();
141
+ let newTimeout = self.cronTime.getTimeout();
141
142
 
142
143
  if (newTimeout > diff) {
143
144
  newTimeout = diff;