Skip to content

Cell - Schedule Component ​

Cron jobs for Cell backends, on the cron package.

bash
npm install @celljs/schedule
ts
import { Autowired } from '@celljs/core';
import { Cron, CronExpression, Scheduler } from '@celljs/schedule';

@Scheduler()
export class ReportScheduler {
    @Autowired(ReportService)
    protected readonly reports: ReportService;

    @Cron(CronExpression.EVERY_5_MINUTES)
    async refresh(): Promise<void> {
        await this.reports.refresh();
    }

    @Cron('0 30 2 * * *', { name: 'nightly', timeZone: 'Asia/Shanghai' })
    nightly(): Promise<void> {
        return this.reports.archive();
    }
}

Import the file from the backend DI module like any component.

  • @Scheduler(cronJobOptions?, componentOption?) makes the class a component whose @Cron methods are jobs. Its cronJobOptions apply to every job of the class.
  • @Cron(cronTime, options?): cronTime is a cron expression with an optional seconds field ('*/10 * * * * *') or a Date. options are those of the cron package's CronJob (timeZone, utcOffset, runOnInit, ...) plus name. CronExpression has common expressions (EVERY_SECOND, EVERY_5_MINUTES, EVERY_DAY_AT_MIDNIGHT, ...).
  • Jobs start with the application and stop with it. An error thrown by a job is logged; the job keeps its schedule.
  • schedule.disabled: true starts no job, for example in cell.test.yml.
  • SchedulerRegistry adds, gets and deletes jobs at runtime: registry.add('cleanup', { cronTime: '0 * * * *', onTick: () => ... }).start().

Jobs run inside the server process: they only run while it runs. On serverless presets (fc, scf, lambda, vercel, cloudflare) instances are created and stopped by requests, so use the platform's timers instead.