Skip to content

Module - Cron

You wan to execute a function regularly? This module is for you! It’s using cron package in the background with a few things on top (You start to be used to it in firstly 😉).

Once you have it setup, assign you the role "Cron.Admin" (You can get it via Roles_Cron.Cron_Admin), and in Admin UI, you will be able to see all crons in the entity named FF Crons.

Terminal window
npm add firstly@latest -D
import { cron, cronTime } from 'firstly/cron/server'
export const api = remultApi({
modules: [
cron([
{
// REQUIRED
topic: 'first_cron', // Will be used for logs
cronTime: cronTime.every_second, // You can use the cron syntax `* * * * * *` or some built in helpers.
onTick: () => {
console.log('hello')
return { status: 'success' } // Must return a Record<string, any>
},
// onTick: fnUpdateFormSomewhereElse // Usually doing this in real life !
// OPTIONAL (but you want them 😉)
start: !dev // Start in production
// runOnInit: dev, // nice in dev environement
// OPTIONAL
concurrent: 1, // Default is 1, if we are at the limit, the job will be skipped.
logs: {}, // to log more or less stuff, see Logs below.
}
])
]
})

By default, a tick only logs when it took at least 100ms:

Terminal window
cron [first_cron] done in 230ms

Fast ticks stay silent. Failures and concurrency skips are always logged. The rest is configurable per job:

logs: {
setup: true, // at registration: "setup done (running, next at ...)"
ended: 100, // min duration (ms) to log "done in Xms". true = always, false = never
starting: false, // before each tick: "starting..."
result: false, // attach onTick's return value to the "done" line (when it logs)
}

The full run history (results, errors, skips) is always stored in the FF Crons entity, so quiet logs don’t lose anything.

If onTick throws, the run is stored with the status failed (the error message lands in result) and an error line is logged. The job keeps running on schedule.