Socket
Socket
Sign inDemoInstall

bossman

Package Overview
Dependencies
204
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.1 to 2.0.0

61

lib/__tests__/Bossman.test.js

@@ -62,3 +62,3 @@ /* eslint-env jest */

boss.hire('scheduled', {
every: '0.5 seconds',
every: '200 ms',
work: function () {

@@ -87,3 +87,3 @@ function work() {

boss.hire('qas', {
every: '0.5 seconds',
every: '200 ms',
work: function () {

@@ -106,3 +106,3 @@ function work() {

boss.hire('one', {
every: '0.5 seconds',
every: '200 ms',
work: function () {

@@ -128,3 +128,3 @@ function work() {

b.hire('one', {
every: '0.5 seconds',
every: '200 ms',
work: function () {

@@ -146,3 +146,3 @@ function work() {

boss.hire('fired', {
every: '0.5 seconds',
every: '200 ms',
work: function () {

@@ -166,3 +166,3 @@ function work() {

function work() {
done('Work performed non-scheduled.');
done();
}

@@ -189,3 +189,3 @@

boss.hire('both', {
every: '0.5 seconds',
every: '200 ms',
work: function () {

@@ -203,2 +203,49 @@ function work() {

});
it('allows colons in names', function (done) {
boss.hire('something:with:colons', {
every: '200 ms',
work: function () {
function work() {
done();
}
return work;
}()
});
});
it('allows cron formats', function (done) {
boss.hire('cronjob', {
cron: '*/1 * * * * *',
work: function () {
function work() {
done();
}
return work;
}()
});
});
it('allows numeric values for every', function (done) {
boss.hire('numeric', {
every: 200,
work: function () {
function work() {
done();
}
return work;
}()
});
});
it('throws when given an invalid type for every', function () {
expect(function () {
boss.hire('throws', {
every: {}
});
}).toThrowError();
});
});

44

lib/Bossman.js

@@ -5,2 +5,4 @@ Object.defineProperty(exports, "__esModule", {

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

@@ -16,8 +18,10 @@

var _humanInterval = require('human-interval');
var _timestring = require('timestring');
var _humanInterval2 = _interopRequireDefault(_humanInterval);
var _timestring2 = _interopRequireDefault(_timestring);
var _throwback = require('throwback');
var _cronParser = require('cron-parser');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

@@ -64,3 +68,3 @@

var jobName = message.split(':').pop();
var jobName = message.startsWith(String(_this.prefix) + ':work:demand:') ? message.replace(String(_this.prefix) + ':work:demand:', '') : message.replace(String(_this.prefix) + ':work:', '');

@@ -73,4 +77,4 @@ if (_this.jobs[jobName]) {

// just a simple set command, and is okay to run on top of eachother.
if (_this.jobs[jobName].every) {
_this.scheduleRun(jobName, _this.jobs[jobName].every);
if (_this.jobs[jobName].every || _this.jobs[jobName].cron) {
_this.scheduleRun(jobName, _this.jobs[jobName]);
}

@@ -96,4 +100,4 @@ }

this.jobs[name] = definition;
if (definition.every) {
this.scheduleRun(name, definition.every);
if (definition.every || definition.cron) {
this.scheduleRun(name, definition);
}

@@ -198,8 +202,26 @@ }

value: function () {
function scheduleRun(name, interval) {
// If there's no interval, it's a demand, let's schedule as tight as we can:
if (!interval) {
function scheduleRun(name, definition) {
// If there's no definition passed, it's a demand, let's schedule as tight as we can:
if (!definition) {
return this.client.set(this.getDemandKey(name), name, 'PX', 1, 'NX');
}
var timeout = (0, _humanInterval2['default'])(interval);
var timeout = void 0;
if (definition.every) {
var typeOfEvery = _typeof(definition.every);
if (typeOfEvery === 'string') {
// Passed a human interval:
timeout = (0, _timestring2['default'])(definition.every, 'ms');
} else if (typeOfEvery === 'number') {
// Passed a ms interval:
timeout = definition.every;
} else {
throw new Error('Unknown interval of type "' + String(typeOfEvery) + '" passed to hire.');
}
} else if (definition.cron) {
var nextTime = (0, _cronParser.parseExpression)(definition.cron, { iterator: false }).next().getTime();
var cronTimeout = nextTime - Date.now();
// Force timeout to be at least 1:
timeout = cronTimeout >= 1 ? cronTimeout : 1;
}
return this.client.set(this.getJobKey(name), name, 'PX', timeout, 'NX');

@@ -206,0 +228,0 @@ }

{
"name": "bossman",
"version": "1.1.1",
"version": "2.0.0",
"description": "Distributed job scheduling in node, based on redis.",

@@ -34,6 +34,7 @@ "main": "lib/index.js",

"dependencies": {
"human-interval": "^0.1.6",
"cron-parser": "^2.3.0",
"ioredis": "^2.4.3",
"redlock": "^2.1.0",
"throwback": "^1.1.1"
"throwback": "^1.1.1",
"timestring": "^3.2.0"
},

@@ -40,0 +41,0 @@ "jest": {

@@ -79,9 +79,12 @@ # bossman

- `jobName`: A unique name for the job.
- `jobDefinition`: The job definition can contain two properties: `work`, and `every`. The `work` function is invoked when the job is completed. `every` is a human-friendly string which describes the interval the job will be run.
- `jobDefinition`: The job definition can contain three properties: `work`, `every`, and `cron`.
- `jobDefinition.work`: A function that is invoked to perform the job. If the work is async, this function should return a promise to properly reflect the completion of the job.
- `jobDefinition.every`: A string which describes the interval the job will be run. This can either be a human-friendly string (which will be parsed by the [`timestring`](https://www.npmjs.com/package/timestring) module), or a number reflecting the milliseconds to wait between each job run.
- `jobDefinition.cron`: A string used to schedule work in more complex intervals. These are parsed with [`cron-parser`](https://www.npmjs.com/package/cron-parser).
It's possible to omit the `every` property if you don't wish to schedule recurring work, and just want to register a job.
It's possible to omit the `every` or `cron` properties if you don't wish to schedule recurring work, and just want to register a job.
#### `bossman.demand(jobName: String)`
Runs a job as soon as possible, outside of the scheduled jobs.
Runs a job as soon as possible, outside of the scheduled jobs. Demanded jobs still run with the same locking mechanism that scheduled jobs run with, ensuring only one instance runs the job at a time.
This does **not** prevent any scheduled jobs from running, unless the demand is running at the same time as a scheduled job and all instances fail to acquire a lock on the job.

@@ -88,0 +91,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc