From 0d0e09f173dcdd8d89d0f8dddd6dc9d5de8caf43 Mon Sep 17 00:00:00 2001 From: Naz Date: Wed, 23 Jun 2021 15:01:43 +0400 Subject: [PATCH] Moved update check scheduling logic out of boot.js refs https://github.com/TryGhost/Team/issues/754 - This is a minor cleanup. There should be no logic in the boot.js file other than calling services to "initialize themselves" --- core/boot.js | 15 +++------------ core/server/update-check.js | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/core/boot.js b/core/boot.js index 5cd6908ece..0e6b813b24 100644 --- a/core/boot.js +++ b/core/boot.js @@ -5,6 +5,8 @@ // - As we manage to break the codebase down into distinct components for e.g. the frontend, their boot logic can be offloaded to them // - app.js is separate as the first example of each component having it's own app.js file colocated with it, instead of inside of server/web +const updateCheck = require('./server/update-check'); + // IMPORTANT: The only global requires here should be overrides + debug so we can monitor timings with DEBUG=ghost:boot* node ghost require('./server/overrides'); const debug = require('@tryghost/debug')('boot'); @@ -207,18 +209,7 @@ async function initBackgroundServices({config}) { const themeService = require('./server/services/themes'); themeService.loadInactiveThemes(); - const jobsService = require('./server/services/jobs'); - - // use a random seconds/minutes/hours value to avoid spikes to the update service API - const s = Math.floor(Math.random() * 60); // 0-59 - const m = Math.floor(Math.random() * 60); // 0-59 - const h = Math.floor(Math.random() * 24); // 0-23 - - jobsService.addJob({ - at: `${s} ${m} ${h} * * *`, // Every day - job: require('path').resolve(__dirname, 'server', 'run-update-check.js'), - name: 'update-check' - }); + updateCheck.scheduleRecurringJobs(); debug('End: initBackgroundServices'); } diff --git a/core/server/update-check.js b/core/server/update-check.js index c96b8dda7e..bd443c8bbb 100644 --- a/core/server/update-check.js +++ b/core/server/update-check.js @@ -4,6 +4,7 @@ const api = require('./api').v2; const GhostMailer = require('./services/mail').GhostMailer; const config = require('../shared/config'); const urlUtils = require('./../shared/url-utils'); +const jobsService = require('./services/jobs'); const i18n = require('../shared/i18n'); const logging = require('@tryghost/logging'); @@ -59,3 +60,16 @@ module.exports = () => { updateChecker.check(); }; + +module.exports.scheduleRecurringJobs = () => { + // use a random seconds/minutes/hours value to avoid spikes to the update service API + const s = Math.floor(Math.random() * 60); // 0-59 + const m = Math.floor(Math.random() * 60); // 0-59 + const h = Math.floor(Math.random() * 24); // 0-23 + + jobsService.addJob({ + at: `${s} ${m} ${h} * * *`, // Every day + job: require('path').resolve(__dirname, 'run-update-check.js'), + name: 'update-check' + }); +};