
Security News
Scaling Socket from Zero to 10,000+ Organizations
Socket CEO Feross Aboukhadijeh shares lessons from scaling a developer security startup to 10,000+ organizations in this founder interview.
@tryghost/job-manager
Advanced tools
A manager for jobs (aka tasks) that have to be performed asynchronously, optionally recurring, scheduled or one-off in their nature. The job queue is manage in memory without additional dependencies.
A manager for jobs (aka tasks) that have to be performed asynchronously, optionally recurring, scheduled or one-off in their nature. The job queue is manage in memory without additional dependencies.
npm install @tryghost/job-manager --save
or
yarn add @tryghost/job-manager
Below is a sample code to wire up job manger and initialize jobs:
const JobManager = require('@tryghost/job-manager');
const logging = {
info: console.log,
warn: console.log,
error: console.error
};
const jobManager = new JobManager(logging);
// register a job "function" with queued execution in current event loop
jobManager.addJob({
job: printWord(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 un immediately running outside of current even loop
jobManager.addJob({
job: './path/to/jobs/check-emails.js',
name: 'email-checker-now'
});
For more examples of JobManager initialization check test/examples directory.
There are two types of jobs distinguished based on purpose and environment they run in:
Job manager's instance registers jobs through addJob method. The offloaded parameter controls if the job is inline (executed in the same event loop) or is offloaded (executed in worker thread/separate process). By default offloaded is set to true - creates an "offloaded" job.
When offloaded: false parameter is passed into addJob method, job manager registers an inline function for execution in FIFO queue. The job should not be computationally intensive and should have small amount of asynchronous operations. The developer should always account that the function will be executed on the same event loop, thread and process as caller's process. inline jobs should be JavaScript functions or a path to a module that exports a function as default. Note, at the moment it's not possible to defined scheduled or recurring inline job.
When skipped or offloaded: true parameter is passed into addJob method, job manager registers execution of an offloaded job. The job can be scheduled to run immediately, in the future, or in recurring manner (through at parameter). Jobs created this way are managed by bree job scheduling library. For examples of job scripts check out this section of bree's documentation, test job examples.
To prevent complications around failed job retries and and handling of specific job states here are some rules that should be followed for all scheduled jobs:
Offloaded jobs are running on dedicated worker threads which makes their lifecycle a bit different to 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.
⚠️ to ensure worker thread back compatibility and correct inter-thread communication use btrheads polyfill instead of native worker_threads module in job scripts.
Instead of:
const {isMainThread, parentPort} = require('worker_threads');
use
const {isMainThread, parentPort} = require('bthreads');
It should be possible to use native worker_threads module once Node v10 hits EOL (2021-04-30).
This is a mono repository, managed with lerna.
Follow the instructions for the top-level repo.
git clone this repo & cd into it as usualyarn to install top-level dependencies.yarn devyarn lint run just eslintyarn test run lint and testsCopyright (c) 2013-2022 Ghost Foundation - Released under the MIT license.
FAQs
A manager for background jobs in Ghost, supporting one-off tasks and recurring jobs.
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
Socket CEO Feross Aboukhadijeh shares lessons from scaling a developer security startup to 10,000+ organizations in this founder interview.

Research
Socket Threat Research maps a rare inside look at OtterCookie’s npm-Vercel-GitHub chain, adding 197 malicious packages and evidence of North Korean operators.

Research
Socket researchers identified a malicious Chrome extension that manipulates Raydium swaps to inject an undisclosed SOL transfer, quietly routing fees to an attacker wallet.