Skip to content
On this page

Cron Plugin

This plugin adds support for running cronjob in Elysia server.

Install with:

bash
bun add @elysiajs/cron
bun add @elysiajs/cron

Then use it:

typescript
import { Elysia } from 'elysia'
import { cron } from '@elysiajs/cron'

new Elysia()
    .use(
        cron({
            name: 'heartbeat',
            pattern: '*/10 * * * * *',
            run() {
                console.log('Heartbeat')
            }
        })
    )
    .listen(8080)
import { Elysia } from 'elysia'
import { cron } from '@elysiajs/cron'

new Elysia()
    .use(
        cron({
            name: 'heartbeat',
            pattern: '*/10 * * * * *',
            run() {
                console.log('Heartbeat')
            }
        })
    )
    .listen(8080)

The above code will log heartbeat every 10 seconds.

cron

Create a cronjob for Elysia server.

type:

cron(config: CronConfig, callback: (Instance['store']) => void): this
cron(config: CronConfig, callback: (Instance['store']) => void): this

CronConfig accept the parameters specified below:

name

Job name to register to store.

This will register the cron instance to store with a specified name, which can be used to reference in later processes eg. stop the job.

pattern

Time to run the job as specified by cron syntax specified as below:

┌────────────── second (optional)
│ ┌──────────── minute
│ │ ┌────────── hour
│ │ │ ┌──────── day of month
│ │ │ │ ┌────── month
│ │ │ │ │ ┌──── day of week
│ │ │ │ │ │
* * * * * *
┌────────────── second (optional)
│ ┌──────────── minute
│ │ ┌────────── hour
│ │ │ ┌──────── day of month
│ │ │ │ ┌────── month
│ │ │ │ │ ┌──── day of week
│ │ │ │ │ │
* * * * * *

This can be generated by tools like Crontab Guru


This plugin extends cron method to Elysia using cronner.

Below are the config accepted by cronner.

timezone

Time zone in Europe/Stockholm format

startAt

Schedule start time for the job

stopAt

Schedule stop time for the job

maxRuns

Maximum number of executions

catch

Continue execution even if an unhandled error is thrown by a triggered function.

interval

The minimum interval between executions, in seconds.

Pattern

Below you can find the common patterns to use the plugin.

Stop cronjob

You can stop cronjob manually by accessing cronjob name registered to store.

typescript
import { Elysia } from 'elysia'
import { cron } from '@elysiajs/cron'

const app = new Elysia()
    use(
        cron({
            name: 'heartbeat',
            pattern: '*/1 * * * * *',
            run() {
                console.log("Heartbeat")
            }
        }
    )
    .get('/stop', ({ store: { cron: { heartbeat } } }) => {
        heartbeat.stop()

        return 'Stop heartbeat'
    })
    .listen(8080)
import { Elysia } from 'elysia'
import { cron } from '@elysiajs/cron'

const app = new Elysia()
    use(
        cron({
            name: 'heartbeat',
            pattern: '*/1 * * * * *',
            run() {
                console.log("Heartbeat")
            }
        }
    )
    .get('/stop', ({ store: { cron: { heartbeat } } }) => {
        heartbeat.stop()

        return 'Stop heartbeat'
    })
    .listen(8080)

This will sign a cookie with maxAge of 86400 and httpOnly to true.