Skip to main content

Goal

Run a Function at regular intervals (cleanup jobs, report generation, health checks) using the Cron module.

Steps

1. Enable the Cron module

iii-config.yaml
modules:
  - class: modules::cron::CronModule
    config:
      adapter:
        class: modules::cron::KvCronAdapter

2. Register the Function

cron-task.ts
import { registerWorker, Logger } from 'iii-sdk'

const iii = registerWorker(process.env.III_URL ?? 'ws://localhost:49134')

iii.registerFunction({ id: 'cleanup::expired-sessions' }, async () => {
  const logger = new Logger()
  // ...cleanup logic here...
  logger.info('Cleanup ran', { timestamp: new Date().toISOString() })
  return { deleted: 0 }
})

3. Register the Cron trigger

cron-trigger.ts
iii.registerTrigger({
  type: 'cron',
  function_id: 'cleanup::expired-sessions',
  config: { expression: '* * * * * * *' }, // runs every second
})
This runs the Function every second. The expression field uses a 7-field cron format with seconds support (second minute hour day month weekday year).

Common schedules

ExpressionFrequency
* * * * * * *Every second
0 * * * * * *Every minute
0 */5 * * * * *Every 5 minutes
0 0 * * * * *Every hour
0 0 */6 * * * *Every 6 hours
0 0 0 * * * *Daily at midnight
0 0 9 * * 1 *Every Monday at 9 AM

Result

The Function executes automatically on the defined schedule. The Engine handles scheduling, no external cron daemon needed.