SentryCron Decorator for Nest.js

Learn about Sentry's SentryCron decorator.

Available since: v8.16.0

The NestJS SDK includes a @SentryCron decorator that can be used to augment the native NestJS @Cron decorator to send check-ins to Sentry before and after each cron job run.

To get started, import SentryCron from @sentry/nestjs and use it to decorate a cron job function in your NestJS application.

The @SentryCron decorator needs to be applied after the @Cron decorator, or else the instrumentation does not work.

import { Cron } from "@nestjs/schedule";
import { SentryCron } from "@sentry/nestjs";

export class MyCronService {
  @Cron("* * * * *")
  @SentryCron("my-monitor-slug", {
    schedule: {
      type: "crontab",
      value: "* * * * *",
    },
    checkinMargin: 2, // In minutes. Optional.
    maxRuntime: 10, // In minutes. Optional.
    timezone: "America/Los_Angeles", // Optional.
  })
  handleCron() {
    // Your cron job logic here
  }
}

Available since: v11.11.0

If you don't pass a monitor config with a schedule, @SentryCron sends the schedule and time zone of the @Cron decorator on the same method with each check-in, so Sentry creates the monitor on the first run. You can still pass other monitor options, such as checkinMargin and maxRuntime:

import { Cron } from "@nestjs/schedule";
import { SentryCron } from "@sentry/nestjs";

export class MyCronService {
  @Cron("0 * * * *", { timeZone: "America/Los_Angeles" })
  @SentryCron("my-monitor-slug", { checkinMargin: 2 })
  handleCron() {
    // Your cron job logic here
  }
}

If @Cron has neither timeZone nor utcOffset, the job runs in your server's local time zone, so that time zone is sent. If your servers run in different time zones, set timeZone on @Cron, so every server sends the same one.

The monitor is created, and billed, on the first run. Each check-in overwrites the monitor's schedule and time zone with the ones from @Cron, so change them in your code rather than in Sentry.

A six-field expression is sent without its seconds field if that field is a single number, such as 0 30 9 * * *. Presets such as @daily and @weekly are sent too. Sub-minute schedules, Date values, utcOffset, month numbers with @nestjs/schedule 3 and earlier (use names such as MAY; steps such as */3 work), a */n or full-range day field (such as 1-31 or sun-sat) combined with the other day field, and time zones that aren't IANA names send no monitor config, including any other options you pass. In those cases, pass a full monitor config to @SentryCron or create the monitor in Sentry first.

To send check-ins without a monitor config, pass { fromCronDecorator: false }.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").