cron-cluster
Advanced tools
Comparing version 1.0.0 to 1.1.3
@@ -0,5 +1,3 @@ | ||
var wrap = require('lodash/wrap') | ||
var wrap = require('lodash.wrap') | ||
var toArray = require('lodash.toarray') | ||
var OriginCronJob = require('cron').CronJob | ||
@@ -18,22 +16,34 @@ var Leader = require('redis-leader') | ||
function CronJob (cronTime, onTick) { | ||
if (typeof cronTime !== 'string' && arguments.length === 1) { | ||
onTick = cronTime.onTick | ||
function CronJob (cronSettings, onTick, onComplete, start, timeZone) { | ||
if (typeof cronSettings === 'string') { | ||
cronSettings = {cronTime: cronSettings} | ||
} | ||
if (onTick) { | ||
cronSettings.onTick = onTick | ||
} | ||
if (onComplete) { | ||
cronSettings.onComplete = onComplete | ||
} | ||
if (start) { | ||
cronSettings.start = start | ||
} | ||
if (timeZone) { | ||
cronSettings.timeZone = timeZone | ||
} | ||
this.started = false | ||
// Wrap `onTick` for checking if is leader | ||
onTick = wrap(onTick, function (fn) { | ||
cronSettings.onTick = wrap(cronSettings.onTick, function (fn) { | ||
logger('On tick') | ||
leader.isLeader(function (err, isLeader) { | ||
if (err) return fn(err) | ||
logger('On tick: leader = %s', isLeader) | ||
if (!isLeader) return | ||
fn() | ||
}) | ||
setTimeout(function () { | ||
leader.isLeader(function (err, isLeader) { | ||
if (err) return fn(err) | ||
logger('On tick: leader = %s', isLeader) | ||
if (!isLeader) return | ||
fn() | ||
}) | ||
}, 50) | ||
}) | ||
// Create real CronJob | ||
var args = toArray(arguments) | ||
args.unshift({}) | ||
var job = new (Function.bind.apply(OriginCronJob, args)) | ||
var job = new (OriginCronJob.bind([], cronSettings)) | ||
// Wrap `stop` for stopping leader before | ||
@@ -40,0 +50,0 @@ job.stop = wrap(job.stop, function (stop) { |
{ | ||
"name": "cron-cluster", | ||
"version": "1.0.0", | ||
"version": "1.1.3", | ||
"description": "A Cluster version of node-cron using redis", | ||
@@ -25,6 +25,5 @@ "main": "index.js", | ||
"dependencies": { | ||
"cron": "^1.0.9", | ||
"debug": "^2.2.0", | ||
"lodash.toarray": "^3.0.2", | ||
"lodash.wrap": "^3.0.1", | ||
"cron": "^1.2.1", | ||
"debug": "^2.6.0", | ||
"lodash": "^4.17.11", | ||
"redis-leader": "^0.1.0" | ||
@@ -38,3 +37,6 @@ }, | ||
"tape": "^4.2.0" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
} | ||
} |
@@ -35,2 +35,20 @@ # Cron-cluster | ||
It is possible to initialize CronJob with an object passed as parameter and run the job only once for all the instances. | ||
Cron-cluster is compatible with original cron. More in test/cron-cluster-compatibility-check.js | ||
```js | ||
var redis = require('redis').createClient() | ||
var CronJob = require('cron-cluster')(redis).CronJob | ||
function doCron () { | ||
var job = new CronJob({ | ||
cronTime: '* * * * * *', | ||
onTick: function () { | ||
// Do some stuff here | ||
} | ||
}) | ||
job.start() | ||
} | ||
``` | ||
All you need is to provide a redis client to the cron-cluster module. | ||
@@ -37,0 +55,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
20183
4
631
76
0
+ Addedlodash@^4.17.11
+ Addedlodash@4.17.21(transitive)
- Removedlodash.toarray@^3.0.2
- Removedlodash.wrap@^3.0.1
- Removedlodash._arraycopy@3.0.0(transitive)
- Removedlodash._basevalues@3.0.0(transitive)
- Removedlodash._createwrapper@3.2.0(transitive)
- Removedlodash._getnative@3.9.1(transitive)
- Removedlodash._root@3.0.1(transitive)
- Removedlodash.isarguments@3.1.0(transitive)
- Removedlodash.isarray@3.0.4(transitive)
- Removedlodash.keys@3.1.2(transitive)
- Removedlodash.toarray@3.0.2(transitive)
- Removedlodash.wrap@3.0.1(transitive)
Updatedcron@^1.2.1
Updateddebug@^2.6.0