@cityssm/scheduled-task
v1.0.0
Published
Schedules recurring tasks while managing on-demand executions and limiting simultaneous executions.
Maintainers
Readme
Scheduled Task
Schedules recurring tasks while managing on-demand executions and limiting simultaneous executions. Helpful for managing process-heavy tasks running in child processes.
Installation
npm install @cityssm/scheduled-taskUsage
Child Process
// childProcess.js
import { ScheduledTask } from '@cityssm/scheduled-task'
// Initialize task
const task = new ScheduledTask(
childProcessTaskName,
() => {
/*
* Process-heavy code running in the child process.
*/
},
{
schedule: {
second: 0,
minute: 0,
hour: 0,
dayOfWeek: '*',
month: '*',
year: '*'
},
minimumIntervalMillis: 10 * 60 * 1000,
startTask: true
}
)
// Listen for message to run the task on demand.
process.on('message', (_message) => {
void task.runTask()
})Options
schedule
The frequency the task should run. See
node-schedule for acceptable
schedule formats.
Default: Midnight
lastRunMillis
The last time the task was executed.
Helpful to avoid rerunning a task too soon after a restart.
Default: 0
minimumIntervalMillis
The minimum amount of time between executions.
Helpful if the task can be run on demand.
Default: 0
startTask
Whether the task should be started immediately after initialization.
Default: false
catchErrors
Whether errors thrown during task execution should be caught internally.
If false, errors will propagate to the parent process.
Default: true
Application
// app.js
import { fork } from 'node:child_process'
const childProcess = fork('childProcess.js')
childProcess.send('Run the task on demand.')Real World Example
This package was made for the City's FASTER Web Helper application. The application does a lot of background syncing work in child processes.
