Move the update-check service to a scheduled job

issue https://github.com/TryGhost/Team/issues/729
This commit is contained in:
Thibaut Patel 2021-05-27 16:36:46 +02:00
parent 03bf1c9671
commit a17403ab6a
2 changed files with 79 additions and 0 deletions

View File

@ -207,6 +207,19 @@ 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'
});
debug('End: initBackgroundServices');
}

View File

@ -0,0 +1,66 @@
const {parentPort} = require('bthreads');
// Exit early when cancelled to prevent stalling shutdown. No cleanup needed when cancelling as everything is idempotent and will pick up
// where it left off on next run
function cancel() {
parentPort.postMessage('Update check job cancelled before completion');
if (parentPort) {
parentPort.postMessage('cancelled');
} else {
setTimeout(() => {
process.exit(0);
}, 1000);
}
}
if (parentPort) {
parentPort.once('message', (message) => {
if (message === 'cancel') {
return cancel();
}
});
}
(async () => {
const updateCheck = require('./update-check');
const logging = {
info(message) {
parentPort.postMessage(message);
},
warn(message) {
parentPort.postMessage(message);
},
error(message) {
parentPort.postMessage(message);
}
};
// INIT required services
const models = require('./models');
models.init();
const permissions = require('./services/permissions');
await permissions.init();
const settings = require('./services/settings');
await settings.init();
const i18n = require('../shared/i18n');
i18n.init();
// Finished INIT
await updateCheck({logging});
parentPort.postMessage(`Ran update check`);
if (parentPort) {
parentPort.postMessage('done');
} else {
// give the logging pipes time finish writing before exit
setTimeout(() => {
process.exit(0);
}, 1000);
}
})();