Cell - Schedule Component
Cron jobs for Cell backends, on the cron package.
bash
npm install @celljs/schedulets
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@Cronmethods are jobs. ItscronJobOptionsapply to every job of the class.@Cron(cronTime, options?):cronTimeis a cron expression with an optional seconds field ('*/10 * * * * *') or aDate.optionsare those of the cron package'sCronJob(timeZone,utcOffset,runOnInit, ...) plusname.CronExpressionhas 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: truestarts no job, for example incell.test.yml.SchedulerRegistryadds, 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.