Moved jobmanager initialization logic

refs https://github.com/TryGhost/Toolbox/issues/357

- In upcoming iteration job manager will need "models" fully initialized to be able to persiste one time jobs. This bit of code of in a bad place as it initialized (through require) the job manager without having models module initialized first.
- The change moves that code from a bad to less bad place (wasn't able to think of any better location for now). Checking for `server:testmode' config in job initialization has a little bit of a smell, but don't think it's super critical. Could be improved if a better structure appears in the future!
This commit is contained in:
Naz 2022-07-21 17:00:34 +01:00
parent 17bf358b47
commit 3e57e6fe10
3 changed files with 27 additions and 22 deletions

View File

@ -120,6 +120,11 @@ async function initCore({ghostServer, config, bootLogger, frontend}) {
// Job Service allows parts of Ghost to run in the background
debug('Begin: Job Service');
const jobService = require('./server/services/jobs');
if (config.get('server:testmode')) {
jobService.initTestMode();
}
ghostServer.registerCleanupTask(async () => {
await jobService.shutdown();
});

View File

@ -213,9 +213,6 @@ class GhostServer {
* Internal Method for TestMode.
*/
_startTestMode() {
// This is horrible and very temporary
const jobService = require('./services/jobs');
// Output how many connections are open every 5 seconds
const connectionInterval = setInterval(() => this.httpServer.getConnections(
(err, connections) => logging.warn(`${connections} connections currently open`)
@ -226,25 +223,6 @@ class GhostServer {
clearInterval(connectionInterval);
logging.warn('Server has fully closed');
});
// Output job queue length every 5 seconds
setInterval(() => {
logging.warn(`${jobService.queue.length()} jobs in the queue. Idle: ${jobService.queue.idle()}`);
const runningScheduledjobs = Object.keys(jobService.bree.workers);
if (Object.keys(jobService.bree.workers).length) {
logging.warn(`${Object.keys(jobService.bree.workers).length} jobs running: ${runningScheduledjobs}`);
}
const scheduledJobs = Object.keys(jobService.bree.intervals);
if (Object.keys(jobService.bree.intervals).length) {
logging.warn(`${Object.keys(jobService.bree.intervals).length} scheduled jobs: ${scheduledJobs}`);
}
if (runningScheduledjobs.length === 0 && scheduledJobs.length === 0) {
logging.warn('No scheduled or running jobs');
}
}, 5000);
}
/**

View File

@ -17,6 +17,28 @@ const workerMessageHandler = ({name, message}) => {
logging.info(`Worker for job ${name} sent a message: ${message}`);
};
const initTestMode = () => {
// Output job queue length every 5 seconds
setInterval(() => {
logging.warn(`${jobManager.queue.length()} jobs in the queue. Idle: ${jobManager.queue.idle()}`);
const runningScheduledjobs = Object.keys(jobManager.bree.workers);
if (Object.keys(jobManager.bree.workers).length) {
logging.warn(`${Object.keys(jobManager.bree.workers).length} jobs running: ${runningScheduledjobs}`);
}
const scheduledJobs = Object.keys(jobManager.bree.intervals);
if (Object.keys(jobManager.bree.intervals).length) {
logging.warn(`${Object.keys(jobManager.bree.intervals).length} scheduled jobs: ${scheduledJobs}`);
}
if (runningScheduledjobs.length === 0 && scheduledJobs.length === 0) {
logging.warn('No scheduled or running jobs');
}
}, 5000);
};
const jobManager = new JobManager({errorHandler, workerMessageHandler});
module.exports = jobManager;
module.exports.initTestMode = initTestMode;