Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Crons #85

Merged
merged 3 commits into from
Mar 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/markdown/features/crons.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Crons on Lenra are a way to schedule tasks to be executed at specific times. A cron can be managed using the CRUD operations.

## Creating a cron that runs each minute

To create a cron on Lenra, make a POST request to the `/app/crons` endpoint with the following required parameters:

- `listener_name`: a string corresponding to the name of the listener on the application.
- `schedule`: a string corresponding to the cron 5 star format `* * * * *`.

The `schedule` parameter consists of five fields, each separated by a space, that represent minute, hour, day of the month, month, and day of the week. Here is the format:

```scss
* * * * *
- - - - -
| | | | |
| | | | +----- day of the week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of the month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
```

Each field can take the following values:

- An asterisk (`*`) means "any value". For example, `* * * * *` would run the job every minute, every hour, every day of the month, every month, and every day of the week.

- A single value specifies a specific value for that field. For example, `0 * * * *` would run the job at the beginning of every hour, and `0 0 1 * *` would run the job at midnight on the first day of every month.

- A range of values specifies a range for that field. For example, `0 9-17 * * *` would run the job every minute between 9 AM and 5 PM.

- A list of values specifies a list of values for that field. For example, `0 0 1,15 * *` would run the job at midnight on the first and fifteenth days of every month.

- A step value specifies a value that the field must be evenly divisible by. For example, `*/15 * * * *` would run the job every 15 minutes, and `0 0 */2 * *` would run the job every other day.

You can also pass props to the request body for additional data that will be sent to the listener, for example:

```json
{
"listener_name": "myListener",
"schedule": "* * * * *",
"props": {
"userId": "1234"
}
}
```

The response will contain the created cron with the additional generated name parameter:

```json
{
"name": "5c18337c4de4a4a60ce4a6ee",
"listener_name": "myListener",
"schedule": "* * * * *",
"props": {
"userId": "1234"
}
}
```

You will need to remember this `name` to properly call the `UPDATE` and `DELETE` endpoints that are under `/crons/:name`.