https://github.com/viniciusgerevini/controlled-schedule
Recurring schedule for async tasks. Schedule next execution only after the current one finishes.
https://github.com/viniciusgerevini/controlled-schedule
Last synced: about 1 month ago
JSON representation
Recurring schedule for async tasks. Schedule next execution only after the current one finishes.
- Host: GitHub
- URL: https://github.com/viniciusgerevini/controlled-schedule
- Owner: viniciusgerevini
- License: mit
- Created: 2016-05-09T14:07:51.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2022-12-11T21:22:47.000Z (over 2 years ago)
- Last Synced: 2025-03-29T17:41:30.428Z (2 months ago)
- Language: JavaScript
- Homepage:
- Size: 279 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Controlled schedule
[](https://travis-ci.org/viniciusgerevini/controlled-schedule)
[](https://coveralls.io/github/viniciusgerevini/controlled-schedule?branch=master)Recurring schedule for async tasks. Schedule next execution only after the current one finishes.
e.g:
```javascript
const execute = require('controlled-schedule');function task() {
// some async operation
// should accept a callback as parameter
// or return a Promise
}execute(task)
.every('20s')
.startIn('1m')
.stopAfter('2h')
.on('error', function(err) {
// log error
})
.on('stop', function() {
// do something
});
```
For more examples check `/examples` folder and the end of this README.## Instalation
`npm install controlled-schedule --save`
## Usage
- Your task must return a `Promise` or accept a `callback(err, value)` as argument.
- Methods accept time in milliseconds or in string format.
e.g. '10 days', '10d', '10h', '2.5 hrs', '10h', '10m', '10s'- Every method returns the object reference so you can chain all operations.
Usage:
```javascript
const execute = require('controlled-schedule');//create schedule object
let schedule = execute(task);// define interval between each execution
schedule.every('20s');// define how long to start the first execution
schedule.startIn('1m');// start immediately
schedule.start();// define how long the task will be rescheduled
schedule.stopAfter('2h');// stop the schedule (waits for task to finish if it's executing)
schedule.stop();// events
// after a task runs
// err - error object passed by the task
// value - value returned by the task
schedule.on('run', function(err, value) {});
// after a task runs with success
// value - value returned by the task
schedule.on('success', function(value) {});
// after a task runs and return an error
// err - error object returned by the task
schedule.on('error', function(err) {});
// when schedule stop is triggered
schedule.on('stop', function() {});
// return Promise for the next execution
schedule.nextRun()
.then(function(value) {})
.catch(function(err) {});
```### More examples
Run task indefinitely with 1 hour interval between executions:
```javascript
execute(task)
.every('1h')
.start();
```Run for 1 minute starting every next task immediately after previous one finishes:
```javascript
execute(task)
.stopAfter('1m')
.start();
```Run task with 1 minute interval and stop if an error occurred:
```javascript
let schedule =
execute(task)
.every('1m')
.on('error', function(err) {
schedule.stop();
console.log(err);
})
.start()
```For more examples check `/examples` folder.
## License
MIT