@tryghost/job-manager
Advanced tools
Comparing version 0.1.1 to 0.1.2
const fastq = require('fastq'); | ||
const later = require('@breejs/later'); | ||
const pWaitFor = require('p-wait-for'); | ||
const isCronExpression = require('./is-cron-expression'); | ||
@@ -24,6 +26,15 @@ const worker = async (task, callback) => { | ||
this.queue = fastq(this, worker, 1); | ||
this.schedule = []; | ||
this.logging = logging; | ||
} | ||
/** | ||
* Adds job to queue | ||
* | ||
* @param {Function} job - function to be executed in the queue | ||
* @param {Object} [data] - data to be passed into the job | ||
*/ | ||
addJob(job, data) { | ||
this.logging.info('Adding one off job to the queue'); | ||
this.queue.push(async () => { | ||
@@ -34,3 +45,40 @@ await job(data); | ||
/** | ||
* Schedules recuring job | ||
* | ||
* @param {Function|String} job - function or path to a file defining a job | ||
* @param {Object} data - data to be passed into the joba | ||
* @param {String} when - cron or human readable schedule format | ||
*/ | ||
scheduleJob(job, data, when) { | ||
let schedule; | ||
schedule = later.parse.text(when); | ||
if (isCronExpression(when)) { | ||
schedule = later.parse.cron(when); | ||
} | ||
if ((schedule.error && schedule.error !== -1) || schedule.schedules.length === 0) { | ||
throw new Error('Invalid schedule format'); | ||
} | ||
this.logging.info(`Scheduling job. Next run on: ${later.schedule(schedule).next()}`); | ||
const cancelInterval = later.setInterval(() => { | ||
this.logging.info(`Scheduled job added to the queue.`); | ||
this.addJob(job, data); | ||
}, schedule); | ||
this.schedule.push(cancelInterval); | ||
} | ||
/** | ||
* @param {import('p-wait-for').Options} [options] | ||
*/ | ||
async shutdown(options) { | ||
this.schedule.forEach((cancelHandle) => { | ||
cancelHandle.clear(); | ||
}); | ||
if (this.queue.idle()) { | ||
@@ -37,0 +85,0 @@ return; |
{ | ||
"name": "@tryghost/job-manager", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"repository": "https://github.com/TryGhost/Ghost-Utils/tree/master/packages/job-manager", | ||
@@ -22,11 +22,13 @@ "author": "Ghost Foundation", | ||
"devDependencies": { | ||
"mocha": "8.1.3", | ||
"mocha": "8.2.1", | ||
"should": "13.2.3", | ||
"sinon": "9.0.3" | ||
"sinon": "9.2.1" | ||
}, | ||
"dependencies": { | ||
"fastq": "1.8.0", | ||
"@breejs/later": "4.0.2", | ||
"cron-parser": "2.17.0", | ||
"fastq": "1.9.0", | ||
"p-wait-for": "3.1.0" | ||
}, | ||
"gitHead": "2c6232612f359ce13e54f8ca124b7f6739f3dd18" | ||
"gitHead": "7656be6fcd6081f65c473a0a7ff319bd404c7fc8" | ||
} |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
6017
6
106
0
4
+ Added@breejs/later@4.0.2
+ Addedcron-parser@2.17.0
+ Added@breejs/later@4.0.2(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedcron-parser@2.17.0(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedfastq@1.9.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedis-nan@1.3.2(transitive)
+ Addedmoment@2.30.1(transitive)
+ Addedmoment-timezone@0.5.45(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedset-function-length@1.2.2(transitive)
- Removedfastq@1.8.0(transitive)
Updatedfastq@1.9.0