2020-11-05 06:39:51 +03:00
|
|
|
// Switch these lines once there are useful utils
|
|
|
|
// const testUtils = require('./utils');
|
|
|
|
require('./utils');
|
2020-11-19 07:58:41 +03:00
|
|
|
const path = require('path');
|
2020-11-10 06:35:04 +03:00
|
|
|
const sinon = require('sinon');
|
|
|
|
const delay = require('delay');
|
2020-12-02 11:26:15 +03:00
|
|
|
const FakeTimers = require('@sinonjs/fake-timers');
|
2020-11-05 06:39:51 +03:00
|
|
|
|
2020-11-10 06:35:04 +03:00
|
|
|
const JobManager = require('../index');
|
2020-11-05 06:39:51 +03:00
|
|
|
|
|
|
|
describe('Job Manager', function () {
|
2020-11-10 07:11:24 +03:00
|
|
|
let logging;
|
|
|
|
|
2020-11-10 07:15:08 +03:00
|
|
|
beforeEach(function () {
|
2020-11-10 07:11:24 +03:00
|
|
|
logging = {
|
|
|
|
info: sinon.stub(),
|
2020-11-17 08:14:37 +03:00
|
|
|
warn: sinon.stub(),
|
2020-11-10 07:11:24 +03:00
|
|
|
error: sinon.stub()
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-11-05 06:39:51 +03:00
|
|
|
it('public interface', function () {
|
2020-11-10 06:35:04 +03:00
|
|
|
const jobManager = new JobManager(logging);
|
2020-11-05 06:39:51 +03:00
|
|
|
|
|
|
|
should.exist(jobManager.addJob);
|
2020-11-05 07:36:29 +03:00
|
|
|
should.exist(jobManager.scheduleJob);
|
|
|
|
});
|
|
|
|
|
2020-11-10 06:35:04 +03:00
|
|
|
describe('Add a Job', function () {
|
|
|
|
it('adds a job to a queue', async function () {
|
|
|
|
const spy = sinon.spy();
|
|
|
|
const jobManager = new JobManager(logging);
|
|
|
|
|
|
|
|
jobManager.addJob(spy, 'test data');
|
|
|
|
should(jobManager.queue.idle()).be.false();
|
|
|
|
|
|
|
|
// give time to execute the job
|
|
|
|
await delay(1);
|
|
|
|
|
|
|
|
should(jobManager.queue.idle()).be.true();
|
|
|
|
should(spy.called).be.true();
|
|
|
|
should(spy.args[0][0]).equal('test data');
|
|
|
|
});
|
2020-11-10 07:11:24 +03:00
|
|
|
|
|
|
|
it('handles failed job gracefully', async function () {
|
|
|
|
const spy = sinon.stub().throws();
|
|
|
|
const jobManager = new JobManager(logging);
|
|
|
|
|
|
|
|
jobManager.addJob(spy, 'test data');
|
|
|
|
should(jobManager.queue.idle()).be.false();
|
|
|
|
|
|
|
|
// give time to execute the job
|
|
|
|
await delay(1);
|
|
|
|
|
|
|
|
should(jobManager.queue.idle()).be.true();
|
|
|
|
should(spy.called).be.true();
|
|
|
|
should(spy.args[0][0]).equal('test data');
|
|
|
|
should(logging.error.called).be.true();
|
|
|
|
});
|
2020-11-10 06:35:04 +03:00
|
|
|
});
|
|
|
|
|
2020-12-08 06:43:18 +03:00
|
|
|
describe('Schedule a Job', function () {
|
2020-11-19 07:58:41 +03:00
|
|
|
it('fails to schedule for invalid scheduling expression', function () {
|
2020-11-10 06:35:04 +03:00
|
|
|
const jobManager = new JobManager(logging);
|
2020-11-05 07:36:29 +03:00
|
|
|
|
|
|
|
try {
|
2020-11-19 07:58:41 +03:00
|
|
|
jobManager.scheduleJob('invalid expression', 'jobName', {});
|
2020-11-05 07:36:29 +03:00
|
|
|
} catch (err) {
|
|
|
|
err.message.should.equal('Invalid schedule format');
|
|
|
|
}
|
|
|
|
});
|
2020-11-19 07:58:41 +03:00
|
|
|
|
|
|
|
it('fails to schedule for no job name', function () {
|
|
|
|
const jobManager = new JobManager(logging);
|
|
|
|
|
|
|
|
try {
|
|
|
|
jobManager.scheduleJob('invalid expression', () => {}, {});
|
|
|
|
} catch (err) {
|
|
|
|
err.message.should.equal('Name parameter should be present if job is a function');
|
|
|
|
}
|
|
|
|
});
|
2020-12-02 11:26:15 +03:00
|
|
|
|
|
|
|
it('schedules a job using date format', async function () {
|
|
|
|
const jobManager = new JobManager(logging);
|
|
|
|
const timeInTenSeconds = new Date(Date.now() + 10);
|
|
|
|
const jobPath = path.resolve(__dirname, './jobs/simple.js');
|
|
|
|
|
|
|
|
const clock = FakeTimers.install({now: Date.now()});
|
|
|
|
jobManager.scheduleJob(timeInTenSeconds, jobPath, null, 'job-in-ten');
|
|
|
|
|
|
|
|
should(jobManager.bree.timeouts['job-in-ten']).type('object');
|
|
|
|
should(jobManager.bree.workers['job-in-ten']).type('undefined');
|
|
|
|
|
|
|
|
// allow to run the job and start the worker
|
|
|
|
await clock.nextAsync();
|
|
|
|
|
|
|
|
should(jobManager.bree.workers['job-in-ten']).type('object');
|
|
|
|
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
|
|
jobManager.bree.workers['job-in-ten'].on('error', reject);
|
|
|
|
jobManager.bree.workers['job-in-ten'].on('exit', (code) => {
|
|
|
|
should(code).equal(0);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// allow job to finish execution and exit
|
|
|
|
clock.next();
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
|
|
|
|
should(jobManager.bree.workers['job-in-ten']).type('undefined');
|
|
|
|
|
|
|
|
clock.uninstall();
|
|
|
|
});
|
2020-12-09 09:11:11 +03:00
|
|
|
|
|
|
|
it('schedules a job to run immediately', async function () {
|
|
|
|
const jobManager = new JobManager(logging);
|
|
|
|
const clock = FakeTimers.install({now: Date.now()});
|
|
|
|
|
|
|
|
const jobPath = path.resolve(__dirname, './jobs/simple.js');
|
|
|
|
jobManager.scheduleJob(undefined, jobPath, undefined, 'job-now');
|
|
|
|
|
|
|
|
should(jobManager.bree.timeouts['job-now']).type('object');
|
|
|
|
|
|
|
|
// allow scheduler to pick up the job
|
|
|
|
clock.tick(1);
|
|
|
|
|
|
|
|
should(jobManager.bree.workers['job-now']).type('object');
|
|
|
|
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
|
|
jobManager.bree.workers['job-now'].on('error', reject);
|
|
|
|
jobManager.bree.workers['job-now'].on('exit', (code) => {
|
|
|
|
should(code).equal(0);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
|
|
|
|
should(jobManager.bree.workers['job-now']).type('undefined');
|
|
|
|
|
|
|
|
clock.uninstall();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails to schedule a job with the same name to run immediately one after another', async function () {
|
|
|
|
const jobManager = new JobManager(logging);
|
|
|
|
const clock = FakeTimers.install({now: Date.now()});
|
|
|
|
|
|
|
|
const jobPath = path.resolve(__dirname, './jobs/simple.js');
|
|
|
|
jobManager.scheduleJob(undefined, jobPath, undefined, 'job-now');
|
|
|
|
|
|
|
|
should(jobManager.bree.timeouts['job-now']).type('object');
|
|
|
|
|
|
|
|
// allow scheduler to pick up the job
|
|
|
|
clock.tick(1);
|
|
|
|
|
|
|
|
should(jobManager.bree.workers['job-now']).type('object');
|
|
|
|
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
|
|
jobManager.bree.workers['job-now'].on('error', reject);
|
|
|
|
jobManager.bree.workers['job-now'].on('exit', (code) => {
|
|
|
|
should(code).equal(0);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
|
|
|
|
should(jobManager.bree.workers['job-now']).type('undefined');
|
|
|
|
|
|
|
|
// note: job name resolves to [Object object], test can be made more precise
|
|
|
|
// once that is fixed in Bree.
|
|
|
|
(() => {
|
|
|
|
jobManager.scheduleJob(undefined, jobPath, undefined, 'job-now');
|
|
|
|
}).should.throw('Job #1 has a duplicate job name of [object Object]');
|
|
|
|
|
|
|
|
clock.uninstall();
|
|
|
|
});
|
2020-11-05 06:39:51 +03:00
|
|
|
});
|
2020-11-17 08:14:37 +03:00
|
|
|
|
2020-12-08 06:43:18 +03:00
|
|
|
describe('Remove a Job', function () {
|
|
|
|
it('removes a scheduled job from the queue', async function () {
|
|
|
|
const jobManager = new JobManager(logging);
|
|
|
|
|
|
|
|
const timeInTenSeconds = new Date(Date.now() + 10);
|
|
|
|
const jobPath = path.resolve(__dirname, './jobs/simple.js');
|
|
|
|
|
|
|
|
jobManager.scheduleJob(timeInTenSeconds, jobPath, null, 'job-in-ten');
|
|
|
|
jobManager.bree.config.jobs[0].name.should.equal('job-in-ten');
|
|
|
|
|
|
|
|
await jobManager.removeJob('job-in-ten');
|
|
|
|
|
|
|
|
should(jobManager.bree.config.jobs[0]).be.undefined;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-17 08:14:37 +03:00
|
|
|
describe('Shutdown', function () {
|
2020-11-19 07:58:41 +03:00
|
|
|
it('gracefully shuts down a synchronous jobs', async function () {
|
2020-11-17 08:14:37 +03:00
|
|
|
const jobManager = new JobManager(logging);
|
|
|
|
|
|
|
|
jobManager.addJob(require('./jobs/timed-job'), 200);
|
|
|
|
|
|
|
|
should(jobManager.queue.idle()).be.false();
|
|
|
|
|
|
|
|
await jobManager.shutdown();
|
|
|
|
|
|
|
|
should(jobManager.queue.idle()).be.true();
|
|
|
|
});
|
2020-11-19 07:58:41 +03:00
|
|
|
|
|
|
|
it('gracefully shuts down an interval job', async function () {
|
|
|
|
const jobManager = new JobManager(logging);
|
|
|
|
|
|
|
|
jobManager.scheduleJob('every 5 seconds', path.resolve(__dirname, './jobs/graceful.js'));
|
|
|
|
|
|
|
|
await delay(1); // let the job execution kick in
|
|
|
|
|
2020-11-23 04:24:24 +03:00
|
|
|
should(Object.keys(jobManager.bree.workers).length).equal(0);
|
|
|
|
should(Object.keys(jobManager.bree.timeouts).length).equal(0);
|
|
|
|
should(Object.keys(jobManager.bree.intervals).length).equal(1);
|
2020-11-19 07:58:41 +03:00
|
|
|
|
|
|
|
await jobManager.shutdown();
|
|
|
|
|
2020-11-23 04:24:24 +03:00
|
|
|
should(Object.keys(jobManager.bree.intervals).length).equal(0);
|
2020-11-19 07:58:41 +03:00
|
|
|
});
|
2020-11-17 08:14:37 +03:00
|
|
|
});
|
2020-11-05 06:39:51 +03:00
|
|
|
});
|