0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/core/server/services/email-analytics/jobs/index.js
Kevin Ansfield d675278b0b
Prevented scheduling of recurring analytics jobs when not using emails (#12441)
no issue

- recurring jobs spin up worker threads which can be quite CPU intensive even when not performing much processing, this can be problematic in environments where there are many Ghost instances running
- updated the email job scheduling to be skipped on bootup when there are no emails in the database and to be started when the first email is created as long as we're not in testing env
- increase analytics job schedule from every 2 minutes to every 5 minutes to help spread the load further across instances
2020-12-02 08:17:44 +00:00

38 lines
1.3 KiB
JavaScript

const path = require('path');
const config = require('../../../../shared/config');
const models = require('../../../models');
const jobsService = require('../../jobs');
let hasScheduled = false;
module.exports = {
async scheduleRecurringJobs() {
if (
!hasScheduled &&
config.get('backgroundJobs:emailAnalytics') &&
!process.env.NODE_ENV.match(/^testing/)
) {
// don't register email analytics job if we have no emails,
// processer usage from many sites spinning up threads can be high
const emailCount = await models.Email.count();
if (emailCount > 0) {
// use a random seconds value to avoid spikes to external APIs on the minute
const s = Math.floor(Math.random() * 60); // 0-59
// run every 5 minutes, on 1,6,11..., 2,7,12..., 3,8,13..., etc
const m = Math.floor(Math.random() * 5); // 0-4
jobsService.scheduleJob(
`${s} ${m}/5 * * * *`,
path.resolve(__dirname, 'fetch-latest.js'),
undefined,
'email-analytics-fetch-latest'
);
hasScheduled = true;
}
}
return hasScheduled;
}
};