https://github.com/cityssm/node-scheduled-task
Schedules recurring tasks while managing on-demand executions and limiting simultaneous executions.
https://github.com/cityssm/node-scheduled-task
async child-process cron schedule task
Last synced: over 1 year ago
JSON representation
Schedules recurring tasks while managing on-demand executions and limiting simultaneous executions.
- Host: GitHub
- URL: https://github.com/cityssm/node-scheduled-task
- Owner: cityssm
- License: mit
- Created: 2025-02-04T18:33:32.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-10T10:44:32.000Z (over 1 year ago)
- Last Synced: 2025-03-20T22:49:50.722Z (over 1 year ago)
- Topics: async, child-process, cron, schedule, task
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/@cityssm/scheduled-task
- Size: 178 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# Scheduled Task
[](https://www.npmjs.com/package/@cityssm/scheduled-task)
[](https://codeclimate.com/github/cityssm/node-scheduled-task/maintainability)
[](https://codecov.io/gh/cityssm/node-scheduled-task)
[](https://app.deepsource.com/gh/cityssm/node-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
```sh
npm install @cityssm/scheduled-task
```
## Usage
### Child Process
```javascript
// 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](https://www.npmjs.com/package/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
```javascript
// 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](https://github.com/cityssm/faster-web-helper) application.
The application does a lot of background syncing work in child processes.