0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Revert "Transformed addJob to be an async method"

This reverts commit f8b1877253.
This commit is contained in:
Naz 2022-07-21 18:56:17 +01:00
parent f8b1877253
commit 13bfc0746b
2 changed files with 39 additions and 25 deletions

View file

@ -56,7 +56,7 @@ class JobManager {
* @prop {Object} [GhostJob.data] - data to be passed into the job
* @prop {Boolean} [GhostJob.offloaded] - creates an "offloaded" job running in a worker thread by default. If set to "false" runs an "inline" job on the same event loop
*/
async addJob({name, at, job, data, offloaded = true}) {
addJob({name, at, job, data, offloaded = true}) {
if (offloaded) {
logging.info('Adding offloaded job to the queue');
let schedule;

View file

@ -34,7 +34,7 @@ describe('Job Manager', function () {
const spy = sinon.spy();
const jobManager = new JobManager({});
await jobManager.addJob({
jobManager.addJob({
job: spy,
data: 'test data',
offloaded: false
@ -53,11 +53,12 @@ describe('Job Manager', function () {
const spy = sinon.stub().throws();
const jobManager = new JobManager({});
await jobManager.addJob({
jobManager.addJob({
job: spy,
data: 'test data',
offloaded: false
});
should(jobManager.queue.idle()).be.false();
// give time to execute the job
await delay(1);
@ -70,11 +71,11 @@ describe('Job Manager', function () {
});
describe('Offloaded jobs', function () {
it('fails to schedule for invalid scheduling expression', async function () {
it('fails to schedule for invalid scheduling expression', function () {
const jobManager = new JobManager({});
try {
await jobManager.addJob({
jobManager.addJob({
at: 'invalid expression',
name: 'jobName'
});
@ -83,11 +84,11 @@ describe('Job Manager', function () {
}
});
it('fails to schedule for no job name', async function () {
it('fails to schedule for no job name', function () {
const jobManager = new JobManager({});
try {
await jobManager.addJob({
jobManager.addJob({
at: 'invalid expression',
job: () => {}
});
@ -102,7 +103,7 @@ describe('Job Manager', function () {
const jobPath = path.resolve(__dirname, './jobs/simple.js');
const clock = FakeTimers.install({now: Date.now()});
await jobManager.addJob({
jobManager.addJob({
at: timeInTenSeconds,
job: jobPath,
name: 'job-in-ten'
@ -166,30 +167,43 @@ describe('Job Manager', function () {
clock.uninstall();
});
it('fails to schedule a job with the same name to run one after another', async function () {
it('fails to schedule a job with the same name to run immediately one after another', async function () {
const jobManager = new JobManager({});
const clock = FakeTimers.install({now: Date.now()});
const jobPath = path.resolve(__dirname, './jobs/simple.js');
await jobManager.addJob({
jobManager.addJob({
job: jobPath,
name: 'job-now',
at: 'every 1 sec'
name: 'job-now'
});
should(jobManager.bree.intervals['job-now']).type('object');
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');
try {
await jobManager.addJob({
(() => {
jobManager.addJob({
job: jobPath,
name: 'job-now'
});
throw new Error('should have failed to initialize');
} catch (error) {
error.message.should.equal('Job #1 has a duplicate job name of job-now');
}
}).should.throw('Job #1 has a duplicate job name of job-now');
jobManager.shutdown();
clock.uninstall();
});
it('uses custom error handler when job fails', async function (){
@ -199,7 +213,7 @@ describe('Job Manager', function () {
const spyHandler = sinon.spy();
const jobManager = new JobManager({errorHandler: spyHandler});
await jobManager.addJob({
jobManager.addJob({
job,
name: 'will-fail'
});
@ -219,7 +233,7 @@ describe('Job Manager', function () {
const workerMessageHandlerSpy = sinon.spy();
const jobManager = new JobManager({workerMessageHandler: workerMessageHandlerSpy});
await jobManager.addJob({
jobManager.addJob({
job: path.resolve(__dirname, './jobs/message.js'),
name: 'will-send-msg'
});
@ -244,7 +258,7 @@ describe('Job Manager', function () {
const timeInTenSeconds = new Date(Date.now() + 10);
const jobPath = path.resolve(__dirname, './jobs/simple.js');
await jobManager.addJob({
jobManager.addJob({
at: timeInTenSeconds,
job: jobPath,
name: 'job-in-ten'
@ -261,7 +275,7 @@ describe('Job Manager', function () {
it('gracefully shuts down an inline jobs', async function () {
const jobManager = new JobManager({});
await jobManager.addJob({
jobManager.addJob({
job: require('./jobs/timed-job'),
data: 200,
offloaded: false
@ -277,7 +291,7 @@ describe('Job Manager', function () {
it('gracefully shuts down an interval job', async function () {
const jobManager = new JobManager({});
await jobManager.addJob({
jobManager.addJob({
at: 'every 5 seconds',
job: path.resolve(__dirname, './jobs/graceful.js')
});