Socket
Socket
Sign inDemoInstall

@tryghost/job-manager

Package Overview
Dependencies
24
Maintainers
13
Versions
42
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.1 to 0.1.2

lib/is-cron-expression.js

48

lib/job-manager.js
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;

12

package.json
{
"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"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc