Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danieldiekmeier/async-worker
A simple worker to do work in intervals
https://github.com/danieldiekmeier/async-worker
Last synced: about 7 hours ago
JSON representation
A simple worker to do work in intervals
- Host: GitHub
- URL: https://github.com/danieldiekmeier/async-worker
- Owner: danieldiekmeier
- License: mit
- Created: 2019-02-19T21:12:13.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-06-20T12:30:09.000Z (over 1 year ago)
- Last Synced: 2024-11-09T13:57:40.135Z (6 days ago)
- Language: JavaScript
- Size: 34.2 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Async Worker
```sh
npm i @danieldiekmeier/async-worker
```## Usage
Do you have a task that needs to run every now and then? Do you not want to worry about setting up a cronjob, but also not just slap a `setInterval` on it and call it a day? Then this package is the middle ground for you.
Put your worker logic somewhere in your application logic, reuse all your stuff, it's going to be fine!
✨ async/await is fully supported and encouraged ✨
Here is a code example:
```js
import Worker from '@danieldiekmeier/async-worker'new Worker({
// This function is called on every task execution.
async task () {
await database.removeOldStuff()
},// Set the minimum time between task executions, in milliseconds (ms).
// In this case, it would run once per hour.
interval: 60 * 60 * 1000,// Whenever the task errors, this function is called with the error.
onError (err) {
fancyLogger(err)
}
})
```The Worker doesn't start the task if the previous execution is still running. That way, you don't have to worry about having long running tasks like complex database cleanup or slow download tasks.