0.2.0 • Published 8 months ago
@cityssm/scheduled-task v0.2.0
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-task
Usage
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
Option | Description | Default |
---|---|---|
schedule | The frequency the task should run. See node-schedule for acceptable schedule formats. | Midnight |
lastRunMillis | The last time the task was executed. Helpful to avoid rerunning a task too soon after a restart. | 0 |
minimumIntervalMillis | The minimum amount of time between executions. Helpful if the task can be run on demand. | 0 |
startTask | Whether the task should be started immediately after initialization. | false |
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.