API reference
Complete API reference for @robojs/cron.
Cron(expression, jobFunction)
Creates and immediately starts a cron job.
import { Cron } from '@robojs/cron'
const job = Cron('*/10 * * * * *', () => {
console.log('Running...')
})import { Cron } from '@robojs/cron'
const job = Cron('*/10 * * * * *', () => {
console.log('Running...')
})Parameters:
| Parameter | Type | Description |
|---|---|---|
expression | string | A cron expression string. See Cron expressions. |
jobFunction | string | (() => void) | An inline callback or a file path relative to .robo/build/. |
Returns: A CronJob instance.
When jobFunction is a string, the plugin resolves it as a file path and dynamically imports the module on each execution. The module must export a default function that receives the job ID as its argument.
// Function-based (not persistable)
Cron('*/5 * * * * *', () => console.log('tick'))
// File-based (persistable)
Cron('*/5 * * * * *', 'cron/task.js')// Function-based (not persistable)
Cron('*/5 * * * * *', () => console.log('tick'))
// File-based (persistable)
Cron('*/5 * * * * *', 'cron/task.js')An invalid cron expression throws an error at creation time.
Cron.get(id)
Retrieves a job instance by ID from the runtime state.
const job = Cron.get('my-job')const job = Cron.get('my-job')Parameters:
| Parameter | Type | Description |
|---|---|---|
id | string | The job ID (custom or auto-generated UUID). |
Returns: CronJob | null
This is a synchronous call. It reads from in-memory state, not from Flashcore. After a restart, jobs are available only after the plugin's startup restoration completes.
Cron.remove(id)
Removes a job from Flashcore persistence and in-memory state.
await Cron.remove('my-job')await Cron.remove('my-job')Parameters:
| Parameter | Type | Description |
|---|---|---|
id | string | The job ID to remove. |
Returns: Promise<void>
This does not stop a running job. Call job.stop() first for a complete cleanup.
const job = Cron.get('my-job')
job?.stop()
await Cron.remove('my-job')const job = Cron.get('my-job')
job?.stop()
await Cron.remove('my-job')CronJob instance
The object returned by Cron() and Cron.get(). The CronJob class is not exported directly — you interact with instances through the factory function.
save(id?)
Persists a file-based job to Flashcore so it survives restarts.
const job = Cron('0 0 * * *', 'cron/daily.js')
const jobId = await job.save('daily-task')const job = Cron('0 0 * * *', 'cron/daily.js')
const jobId = await job.save('daily-task')Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
id | string | Auto-generated UUID | Optional custom ID. Overwrites the job's internal ID if provided. |
Returns: Promise<string> — The job ID.
Function-based jobs are stored in runtime state only. They will not be written to Flashcore and will not survive restarts.
pause()
Temporarily halts the job. The job can be resumed later.
job.pause()job.pause()Returns: void
resume()
Resumes a paused job.
job.resume()job.resume()Returns: void
stop()
Permanently stops the job. A stopped job cannot be resumed — create a new one instead.
job.stop()job.stop()Returns: void
This does not remove persistence. If the job was saved, it will be restored on the next restart. Use Cron.remove(id) to also clear persistence.
nextRun()
Returns the next scheduled execution time.
const next = job.nextRun()
console.log('Next run:', next)const next = job.nextRun()
console.log('Next run:', next)Returns: Date | null — The next run time, or null if the job is stopped.
Patterns
A merged object of constant presets, day constants, and builder functions for cron expressions. See the full Patterns reference.
import { Cron, Patterns } from '@robojs/cron'
// Constant preset
Cron(Patterns.EVERY_HOUR, () => { /* ... */ })
// Builder function
Cron(Patterns.everyWeekdayAt('09:00'), () => { /* ... */ })
// Day constant with builder
Cron(Patterns.everyWeekOn(Patterns.FRIDAY, '17:00'), () => { /* ... */ })import { Cron, Patterns } from '@robojs/cron'
// Constant preset
Cron(Patterns.EVERY_HOUR, () => { /* ... */ })
// Builder function
Cron(Patterns.everyWeekdayAt('09:00'), () => { /* ... */ })
// Day constant with builder
Cron(Patterns.everyWeekOn(Patterns.FRIDAY, '17:00'), () => { /* ... */ })Constant presets
58 pre-built cron expression strings covering seconds, minutes, hours, daily times, weekly/monthly/yearly schedules, and business hours. See Patterns for the complete table.
Day constants
| Constant | Value |
|---|---|
Patterns.SUNDAY | 0 |
Patterns.MONDAY | 1 |
Patterns.TUESDAY | 2 |
Patterns.WEDNESDAY | 3 |
Patterns.THURSDAY | 4 |
Patterns.FRIDAY | 5 |
Patterns.SATURDAY | 6 |
Builder functions
| Function | Description |
|---|---|
everySenconds(seconds?) | Every N seconds |
everyMinutes(minutes?) | Every N minutes |
everyHours(hours?) | Every N hours |
everyHoursAt(hours, minutes?) | Every N hours at a minute offset |
everyDayAt(time?) | Daily at HH:mm |
everyWeekOn(day, time?) | Weekly on a specific day at HH:mm |
everyWeekdayAt(time?) | Monday–Friday at HH:mm |
everyWeekendAt(time?) | Saturday–Sunday at HH:mm |
Aliases
| Function | Equivalent |
|---|---|
everySecond() | EVERY_SECOND |
everyMinute() | EVERY_MINUTE |
hourly() | EVERY_HOUR |
daily() | EVERY_DAY_AT_MIDNIGHT |
everyWeekday() | EVERY_WEEKDAY |
everyWeekend() | EVERY_WEEKEND |
weekly() | EVERY_WEEK |
monthly() | EVERY_1ST_DAY_OF_MONTH_AT_MIDNIGHT |
everyQuarter() | EVERY_QUARTER |
yearly() | EVERY_YEAR |
