Cron Jobs
For scheduled jobs in AbsoluteJS, use Elysia's official cron plugin directly.
#Install
Add the plugin to your app:
BASH
bun add @elysiajs/cron#Basic Usage
Register a cron job with a name, pattern, and handler:
TS
import { Elysia } from 'elysia';
import { cron } from '@elysiajs/cron';
new Elysia()
.use(
cron({
name: 'heartbeat',
pattern: '*/10 * * * * *',
run() {
console.log('Heartbeat');
}
})
)
.listen(3000);#Config Options
In addition to name, pattern, and run, the plugin supports schedule controls:
timezoneIANA timezone string such as
America/New_YorkstartAtDelay start until a specific date
stopAtStop scheduling after a specific date
maxRunsMaximum number of executions
catchContinue execution after unhandled errors
intervalMinimum interval between runs (seconds)
TS
import { Elysia } from 'elysia';
import { cron } from '@elysiajs/cron';
new Elysia()
.use(
cron({
name: 'report',
pattern: '0 0 9 * * 1-5',
timezone: 'America/New_York',
startAt: new Date('2026-04-01T00:00:00Z'),
stopAt: new Date('2026-12-31T23:59:59Z'),
maxRuns: 100,
interval: 60,
catch: true,
run() {
console.log('Generate weekday report');
}
})
);#Stop a Job
Cron instances are available in store.cron.[name], so you can stop them at runtime:
TS
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 'Stopped heartbeat';
})
.listen(3000);#Predefined Patterns
Use Patterns helpers from @elysiajs/cron for common schedules:
TS
import { Elysia } from 'elysia';
import { cron, Patterns } from '@elysiajs/cron';
new Elysia()
.use(
cron({
name: 'every-second',
pattern: Patterns.everySecond(),
run() {
console.log('Every second');
}
})
)
.use(
cron({
name: 'weekday-5pm',
pattern: Patterns.everyWeekdayAt('17:00'),
run() {
console.log('Weekday at 5 PM');
}
})
);