https://github.com/1999/scheduler
Periodical tasks for node.js
https://github.com/1999/scheduler
nodejs periodic-jobs typescript
Last synced: about 1 month ago
JSON representation
Periodical tasks for node.js
- Host: GitHub
- URL: https://github.com/1999/scheduler
- Owner: 1999
- License: mit
- Created: 2017-10-29T12:38:14.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T17:13:08.000Z (over 2 years ago)
- Last Synced: 2025-03-31T23:33:55.894Z (about 2 months ago)
- Topics: nodejs, periodic-jobs, typescript
- Language: TypeScript
- Size: 267 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Scheduler
[](https://travis-ci.org/1999/scheduler)
Node.js library for periodical tasks written in Typescript.
## How to install
`npm install @1999/scheduler --save`
## API
The only concept of scheduler is a `Task` which represents a periodically run task. It should be a callback which should return a Promise.
### Simple tasks
Use `scheduler.addTask(task: Task, period: number)` function to set up task period.
```typescript
import {
default as Scheduler,
Task,
} from '@1999/scheduler';const task: Task = () => Promise.resolve(2);
const scheduler = new Scheduler();
scheduler.addTask(task, 1000);scheduler.start();
```### Named tasks
In this case you can pass task groups in scheduler constructor. Then use `scheduler.addTask(task: Task, periodId: string)` function to assign task to task group.
```typescript
import {
default as Scheduler,
Task,
} from '@1999/scheduler';const task1: Task = () => got('https://api.facebook/id/1');
const task2: Task = () => got('https://api.facebook/id/2');const scheduler = new Scheduler({ api: 1000 });
scheduler.addTask(task1, 'api');
scheduler.addTask(task2, 'api');scheduler.start();
```## When period should depend on task execution
Sometimes it's not enough to have execution periodicity for tasks. For instance when you have an API which allows you to make requests once per N seconds: in this case it can be safer to send next request only N seconds after you get the response from the previous request. For this purpose you can use `Marker` callback which is the only argument for `Task`:
```typescript
import {
default as Scheduler,
Marker,
Task,
} from '@1999/scheduler';const task: Task = (marker: Marker) => {
return got('https://api.facebook/id/1').then(() => {
// you can run marker function anywhere inside your task
// and the period pause will be started from this moment
marker();
});
};const scheduler = new Scheduler();
scheduler.addTask(task);
scheduler.start();
```### Events
Scheduler instance extends node.js [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter). You can use it to subscribe to events happening inside Scheduler instance:
* `taskCompleted` - emits when task is successfully finished. Also emits an object `{ name: string, execTime: number }` where runTime is the task execution time in milliseconds.
* `taskFailed` - emits when task execution fails. Also emits an object `{ err: Error, execTime: number, name: string }`