Added support for offloaded oneoff jobs

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

- Adds support for persisted one off offloaded (worker thread) jobs
- To try them out run Ghost instance in "testmode" and shoo a request like so: `curl http://localhost:2368/ghost/api/oneoff/graceful-job` - this starts a one time job from graceful-job script (can only ever be executed once on the Ghost instance)
- Job's progress and runtime details are persisted in `jobs` table
- To play more with one off jobs use `addOneOffJob` method available on jobsService
This commit is contained in:
Naz 2022-07-22 18:18:53 +01:00
parent e9132d7572
commit 5f2967cf27
6 changed files with 32 additions and 8 deletions

View File

@ -0,0 +1,9 @@
const ghostBookshelf = require('./base');
const Job = ghostBookshelf.Model.extend({
tableName: 'jobs'
});
module.exports = {
Job: ghostBookshelf.model('Job', Job)
};

View File

@ -5,6 +5,7 @@
const JobManager = require('@tryghost/job-manager'); const JobManager = require('@tryghost/job-manager');
const logging = require('@tryghost/logging'); const logging = require('@tryghost/logging');
const models = require('../../models');
const sentry = require('../../../shared/sentry'); const sentry = require('../../../shared/sentry');
const errorHandler = (error, workerMeta) => { const errorHandler = (error, workerMeta) => {
@ -38,7 +39,7 @@ const initTestMode = () => {
}, 5000); }, 5000);
}; };
const jobManager = new JobManager({errorHandler, workerMessageHandler}); const jobManager = new JobManager({errorHandler, workerMessageHandler, JobModel: models.Job});
module.exports = jobManager; module.exports = jobManager;
module.exports.initTestMode = initTestMode; module.exports.initTestMode = initTestMode;

View File

@ -37,7 +37,7 @@ const internalContext = {context: {internal: true}};
if (shutdown) { if (shutdown) {
postParentPortMessage(`Job shutting down gracefully`); postParentPortMessage(`Job shutting down gracefully`);
process.exit(0); parentPort.postMessage('done');
} }
postParentPortMessage(`Fetching posts`); postParentPortMessage(`Fetching posts`);
@ -46,5 +46,5 @@ const internalContext = {context: {internal: true}};
postParentPortMessage(`Found ${posts.data.length} posts. First one: ${posts.data[0].toJSON().slug}`); postParentPortMessage(`Found ${posts.data.length} posts. First one: ${posts.data[0].toJSON().slug}`);
postParentPortMessage('Graceful job has completed!'); postParentPortMessage('Graceful job has completed!');
process.exit(0); parentPort.postMessage('done');
})(); })();

View File

@ -44,6 +44,20 @@ module.exports = function testRoutes() {
res.sendStatus(202); res.sendStatus(202);
}); });
router.get('/oneoff/:name', (req, res) => {
logging.info('Create Slow Job with timeout of', req.params.name);
const options = {};
options.solo = true;
options.name = req.params.name;
options.job = path.resolve(__dirname, 'jobs', `${options.name}.js`);
jobsService.addOneOffJob(options);
res.sendStatus(202);
});
router.get('/schedule/:schedule/:name*?', (req, res) => { router.get('/schedule/:schedule/:name*?', (req, res) => {
if (!req.params.schedule) { if (!req.params.schedule) {
return res.sendStatus(400, 'schedule parameter cannot be mepty'); return res.sendStatus(400, 'schedule parameter cannot be mepty');

View File

@ -77,7 +77,7 @@
"@tryghost/express-dynamic-redirects": "0.0.0", "@tryghost/express-dynamic-redirects": "0.0.0",
"@tryghost/helpers": "1.1.71", "@tryghost/helpers": "1.1.71",
"@tryghost/image-transform": "1.1.0", "@tryghost/image-transform": "1.1.0",
"@tryghost/job-manager": "0.8.25", "@tryghost/job-manager": "0.9.0",
"@tryghost/kg-card-factory": "3.1.3", "@tryghost/kg-card-factory": "3.1.3",
"@tryghost/kg-default-atoms": "3.1.2", "@tryghost/kg-default-atoms": "3.1.2",
"@tryghost/kg-default-cards": "5.16.2", "@tryghost/kg-default-cards": "5.16.2",

View File

@ -1748,10 +1748,10 @@
"@tryghost/errors" "^1.2.14" "@tryghost/errors" "^1.2.14"
jest-snapshot "^28.0.0" jest-snapshot "^28.0.0"
"@tryghost/job-manager@0.8.25": "@tryghost/job-manager@0.9.0":
version "0.8.25" version "0.9.0"
resolved "https://registry.yarnpkg.com/@tryghost/job-manager/-/job-manager-0.8.25.tgz#9c208e4fa4ca4719c64a432b67039396cfdc0796" resolved "https://registry.yarnpkg.com/@tryghost/job-manager/-/job-manager-0.9.0.tgz#ca6733f75cb927de3a52d633bc4ae3e73a026164"
integrity sha512-gkhhfWZFcpsfS4NqECqJvDIi8yIR+V7dngSsT9N011BOriwu5jr/weoGkGhIkZdIK79GNzVIHR9PBaAAEjZXNw== integrity sha512-UGB1qqwLvqjPLGKAiC/qgCAhSGIZHUD6nxcdHtjIB9G1yGFGi6Ki2bu7QQRsmuttUWHxZzYeKLmhuCBuuEOZmw==
dependencies: dependencies:
"@breejs/later" "^4.0.2" "@breejs/later" "^4.0.2"
"@tryghost/logging" "^2.0.0" "@tryghost/logging" "^2.0.0"