mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-01 13:54:35 +03:00
Move the update-check service to a scheduled job
issue https://github.com/TryGhost/Team/issues/729
This commit is contained in:
parent
03bf1c9671
commit
a17403ab6a
13
core/boot.js
13
core/boot.js
@ -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');
|
||||
}
|
||||
|
||||
|
66
core/server/run-update-check.js
Normal file
66
core/server/run-update-check.js
Normal 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);
|
||||
}
|
||||
})();
|
Loading…
Reference in New Issue
Block a user