noctcore-nestjs/schedule-states-time-zone
noctcore-nestjs/schedule-states-time-zone
- Package
- nestjs v0.2.0
- Preset
- recommended
- Severity
- error
- Autofix
- No
- Suggestions
- No
- Options
- Optional
- Type info
- Not needed
A @Cron states its timeZone and a BullMQ cron schedule states its tz.
A cron expression names a wall-clock time, and a wall clock needs a zone. Left out, @Cron from
@nestjs/schedule uses the zone of the process, and so does a BullMQ job scheduler. “Every day at
03:00” then means 03:00 on a developer laptop in Warsaw, 03:00 UTC in the container, and a different
instant again after the clocks change, so the nightly report runs during business hours and nobody
notices until a customer does.
export class ReportsService { @Cron(CronExpression.EVERY_DAY_AT_3AM, { name: 'nightly-report' }) async nightly() {}}export class ReportsService { @Cron(CronExpression.EVERY_DAY_AT_3AM, { name: 'nightly-report', timeZone: 'Europe/Warsaw' }) async nightly() {}}What it flags
Section titled “What it flags”@Cron(expr)with no options, or options without atimeZone(orutcOffset), or withtimeZone: undefined.<x>.upsertJobScheduler(id, spec, template)whosespechas notz, or aspecpassed by name.<queue>.add(name, data, { repeat: { pattern } }), the older repeatable-job form, withouttz.- With the
zonesoption set, any stated zone that is not one of those string literals, a variable included.
await this.reportsQueue.upsertJobScheduler('nightly', { pattern: '0 3 * * *' }, { name: 'report' });await this.reportsQueue.add('report', {}, { repeat: { pattern: '0 3 * * *' } });await this.reportsQueue.upsertJobScheduler('nightly', { pattern: '0 3 * * *', tz: 'UTC' }, { name: 'report' });await this.reportsQueue.add('report', {}, { repeat: { pattern: '0 3 * * *', tz: 'UTC' } });What it does not flag
Section titled “What it does not flag”- An
every:interval: it has no wall-clock hour to state. - A zone stated through a constant (
timeZone: APP_TIME_ZONE) whenzonesis not set. @Intervaland@Timeout, which are durations, and aCronJobbuilt by hand throughSchedulerRegistry.- A decorator named
Cronfrom another library: the check is by name.
await this.reportsQueue.upsertJobScheduler('heartbeat', { every: 60_000 }, { name: 'ping' });Options
Section titled “Options”| Option | Type | Default | Meaning |
|---|---|---|---|
zones |
string[] |
unset | When set, the zone must be one of these string literals. A variable, utcOffset or any other zone is reported. |
'noctcore-nestjs/schedule-states-time-zone': ['error', { zones: ['UTC', 'Europe/Warsaw'] }]// rule options: {"zones":["UTC"]}export class ReportsService { @Cron(CronExpression.EVERY_DAY_AT_3AM, { timeZone: SERVER_ZONE }) async nightly() {}}export class ReportsService { @Cron(CronExpression.EVERY_DAY_AT_3AM, { timeZone: 'UTC' }) async nightly() {}}When not to use it
Section titled “When not to use it”If every process you run is pinned to one zone by its environment (TZ=UTC in every image and on
every laptop), the implicit zone is already a decision. Even then, stating it in the schedule costs
one property and survives the day someone runs the worker without that environment.