
Security News
Critical Security Vulnerability in React Server Components
React disclosed a CVSS 10.0 RCE in React Server Components and is advising users to upgrade affected packages and frameworks to patched versions now.
@tryghost/job-manager
Advanced tools
A manager for background jobs in Ghost, supporting one-off tasks and recurring jobs.
A manager for background jobs in Ghost, supporting one-off tasks and recurring jobs.
const JobManager = require('@tryghost/job-manager');
const jobManager = new JobManager({JobModel: models.Job});
// Simple one-off job
jobManager.addJob({
name: 'hello-world',
job: () => console.log('Hello World'),
offloaded: false
});
// Recurring job every 5 minutes
jobManager.addJob({
name: 'check-emails',
at: 'every 5 minutes',
job: './jobs/check-emails.js'
});
Ghost supports two types of jobs:
Inline Jobs
Offloaded Jobs
Offloaded jobs must:
Below is a sample code to wire up job manger and initialize jobs. This is the simplest way to interact with the job manager - these jobs do not persist after reboot:
const JobManager = require('@tryghost/job-manager');
const jobManager = new JobManager({JobModel: models.Job});
// register a job "function" with queued execution in current event loop
jobManager.addJob({
job: (word) => console.log(word),
name: 'hello',
offloaded: false
});
// register a job "module" with queued execution in current even loop
jobManager.addJob({
job:'./path/to/email-module.js',
data: {email: 'send@here.com'},
offloaded: false
});
// register recurring job which needs execution outside of current event loop
jobManager.addJob({
at: 'every 5 minutes',
job: './path/to/jobs/check-emails.js',
name: 'email-checker'
});
// register recurring job with cron syntax running every 5 minutes
// job needs execution outside of current event loop
// for cron builder check https://crontab.guru/ (first value is seconds)
jobManager.addJob({
at: '0 1/5 * * * *',
job: './path/to/jobs/check-emails.js',
name: 'email-checker-cron'
});
// register a job to run immediately running outside of current even loop
jobManager.addJob({
job: './path/to/jobs/check-emails.js',
name: 'email-checker-now'
});
// register a one-off job to be executed immediately within current event loop
jobManager.addOneOffJob({
name: 'members-migrations',
offloaded: false,
job: stripeService.migrations.execute.bind(stripeService.migrations)
});
// register a one-off job to be executed immediately outside of current event loop
jobManager.addOneOffJob({
name: 'generate-backup-2022-09-15',
job: './path/to/jobs/backup.js',
});
// optionally await completion of the one-off job in case
// there are state changes expected to execute the rest of the process
// NOTE: if multiple jobs are submitted using the same name, the first completion will resolve
await jobManager.awaitOneOffCompletion('members-migrations');
// check if previously registered one-off job has been executed
// successfully - it exists and doesn't have a "failed" state.
// NOTE: this is stored in memory and cleared on reboot
const backupSuccessful = await jobManager.hasExecutedSuccessfully('generate-backup-2022-09-15');
if (!backupSuccessful) {
// One-off jobs with "failed" state can be rescheduled
jobManager.addOneOffJob({
name: 'generate-backup-2022-09-15',
job: './path/to/jobs/backup.js',
});
}
For more examples of JobManager initialization check test/examples directory.
Background jobs run in separate processes, meaning:
Offloaded jobs are running on dedicated worker threads which makes their lifecycle a bit different from inline jobs:
parentPort.postMessage('done') (example use). Finishing work this way terminates the thread through worker.terminate(), which logs termination in parent process and flushes any pipes opened in thread.'cancel' message coming from parent thread (example use).For more nuances on job structure best practices check bree documentation.
FAQs
A manager for background jobs in Ghost, supporting one-off tasks and recurring jobs.
The npm package @tryghost/job-manager receives a total of 2,051 weekly downloads. As such, @tryghost/job-manager popularity was classified as popular.
We found that @tryghost/job-manager demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
React disclosed a CVSS 10.0 RCE in React Server Components and is advising users to upgrade affected packages and frameworks to patched versions now.

Research
/Security News
We spotted a wave of auto-generated “elf-*” npm packages published every two minutes from new accounts, with simple malware variants and early takedowns underway.

Security News
TypeScript 6.0 will be the last JavaScript-based major release, as the project shifts to the TypeScript 7 native toolchain with major build speedups.