From 822549c9efc716d51cb41f6425cea6e6ed138f32 Mon Sep 17 00:00:00 2001 From: Naz Date: Tue, 26 Jul 2022 18:00:40 +0100 Subject: [PATCH] Added defensive code for non-persistent job manager refs https://github.com/TryGhost/Toolbox/issues/359 - It's up to a user to decide initializing the job manager without a "jobModel". In these cases the regular recurring job scheduling should work as it did before --- ghost/job-manager/lib/job-manager.js | 50 ++++++++++++++++------------ 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/ghost/job-manager/lib/job-manager.js b/ghost/job-manager/lib/job-manager.js index 5c623857ee..0e39917a6e 100644 --- a/ghost/job-manager/lib/job-manager.js +++ b/ghost/job-manager/lib/job-manager.js @@ -66,38 +66,44 @@ class JobManager { this._jobMessageHandler({name, message: 'started'}); }); - this._jobsRepository = new JobsRepository({JobModel}); + if (JobModel) { + this._jobsRepository = new JobsRepository({JobModel}); + } } async _jobMessageHandler({name, message}) { - if (message === 'started') { - const job = await this._jobsRepository.read(name); + if (this._jobsRepository) { + if (message === 'started') { + const job = await this._jobsRepository.read(name); - if (job) { - await this._jobsRepository.update(job.id, { - status: 'started', - started_at: new Date() - }); - } - } else if (message === 'done') { - const job = await this._jobsRepository.read(name); + if (job) { + await this._jobsRepository.update(job.id, { + status: 'started', + started_at: new Date() + }); + } + } else if (message === 'done') { + const job = await this._jobsRepository.read(name); - if (job) { - await this._jobsRepository.update(job.id, { - status: 'finished', - finished_at: new Date() - }); + if (job) { + await this._jobsRepository.update(job.id, { + status: 'finished', + finished_at: new Date() + }); + } } } } - async _jobErrorHandler(error, workerMeta) { - const job = await this._jobsRepository.read(workerMeta.name); + async _jobErrorHandler(error, jobMeta) { + if (this._jobsRepository) { + const job = await this._jobsRepository.read(jobMeta.name); - if (job) { - await this._jobsRepository.update(job.id, { - status: 'failed' - }); + if (job) { + await this._jobsRepository.update(job.id, { + status: 'failed' + }); + } } }