mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-17 23:44:39 -05:00
✨ Added support for immediate offloaded jobs
closes #117 - Having immediately executable offloaded jobs is necessary to be able to run usecases like: send batched emails now, or any other job that does not need to be scheduled - Changed "simple" job timeout to make tests run faster
This commit is contained in:
parent
9a6ed2f180
commit
7ece94f411
4 changed files with 71 additions and 5 deletions
|
@ -19,11 +19,11 @@ const assemble = (when, job, data, name) => {
|
|||
Object.assign(breeJob, {
|
||||
date: when
|
||||
});
|
||||
} else if (isCronExpression(when)) {
|
||||
} else if (when && isCronExpression(when)) {
|
||||
Object.assign(breeJob, {
|
||||
cron: when
|
||||
});
|
||||
} else {
|
||||
} else if (when !== undefined) {
|
||||
Object.assign(breeJob, {
|
||||
interval: when
|
||||
});
|
||||
|
|
|
@ -90,7 +90,7 @@ class JobManager {
|
|||
}
|
||||
}
|
||||
|
||||
if (!(at instanceof Date)) {
|
||||
if (at && !(at instanceof Date)) {
|
||||
if (isCronExpression(at)) {
|
||||
schedule = later.parse.cron(at, true);
|
||||
} else {
|
||||
|
@ -102,8 +102,10 @@ class JobManager {
|
|||
}
|
||||
|
||||
this.logging.info(`Scheduling job ${name} at ${at}. Next run on: ${later.schedule(schedule).next()}`);
|
||||
} else {
|
||||
} else if (at !== undefined) {
|
||||
this.logging.info(`Scheduling job ${name} at ${at}`);
|
||||
} else {
|
||||
this.logging.info(`Scheduling job ${name} to run immediately`);
|
||||
}
|
||||
|
||||
const breeJob = assembleBreeJob(at, job, data, name);
|
||||
|
|
|
@ -113,6 +113,70 @@ describe('Job Manager', function () {
|
|||
|
||||
clock.uninstall();
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Remove a Job', function () {
|
||||
|
|
|
@ -1 +1 @@
|
|||
setInterval(() => process.exit(0), 100);
|
||||
setInterval(() => process.exit(0), 10);
|
||||
|
|
Loading…
Add table
Reference in a new issue