Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/xstoudi/adonis-scheduler

Unopinionated scheduler for Adonis
https://github.com/xstoudi/adonis-scheduler

adonis adonis5 adonisjs adonisjs5 cron schedule scheduler task

Last synced: about 1 month ago
JSON representation

Unopinionated scheduler for Adonis

Awesome Lists containing this project

README

        



@stouder-io/adonis-scheduler


Unopinionated scheduler for Adonis






npm

## Installation
This package is available in the npm registry.
```
npm i @stouder-io/adonis-scheduler
```

Next, configure the package by running the following command.
```
node ace configure @stouder-io/adonis-scheduler
```

## Usage
The scheduler will start with your Adonis server, but no task is loaded.

To create a task, run the following command.
```
node ace make:task MyFirstTask
```

This will create the following file under `app/Tasks` directory:
```ts
import { TaskContract } from '@ioc:StouderIO/Scheduler'

export default class MyFirstTask implements TaskContract {
public readonly name: string = '{{ name }}'
public readonly cron: string = '* * * * *'

public async run(): Promise {

}
}
```

The `run` method is called when the scheduler run the task according to the cron expression you configure in the `cron` field. `name` is just for internal use, but it must be unique.

Please note that running CPU-intensive operations in the task has the potential to block your full web application as Node.js is single-threaded.

If you need to run high-intensive CPU task, you could run an Ace command using `execa`, for example:
```ts
import { TaskContract } from '@ioc:StouderIO/Scheduler'
import execa from 'execa'

export default class MyFirstTask implements TaskContract {
public readonly name: string = 'test-task'
public readonly cron: string = '* * * * *'

public async run(): Promise {
// will execute `node ace intensive` every minute
execa.node('ace', ['intensive'], { stdio: 'inherit' })
}
}
```

Also, if you run multiple instances of your Adonis application, the tasks will be ran on each instance.