---
title: "SentryCron Decorator"
description: "Learn about Sentry's SentryCron decorator."
url: https://docs.sentry.io/platforms/javascript/guides/nestjs/features/sentry-cron-decorator/
---

# SentryCron Decorator for Nest.js

### [Monitor All `@Cron` Methods](https://docs.sentry.io/platforms/javascript/guides/nestjs/features/sentry-cron-decorator.md#monitor-all-cron-methods)

Available since: `v11.11.0`

We recommend enabling `monitorCronJobs` on the Nest integration, which monitors every `@Cron` method without adding `@SentryCron` to each one:

```typescript
import * as Sentry from "@sentry/nestjs";

Sentry.init({
  dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
  integrations: [
    Sentry.nestIntegration({
      monitorCronJobs: true,
      excludeCronJobs: ["cleanup-temp-files", /^Debug/], // Optional.
    }),
  ],
});
```

The monitor slug is the `name` option of `@Cron`, or `ClassName.methodName` if there's no `name`. Sentry lowercases slugs and drops characters other than letters, numbers, `-` and `_`, so `MyCronService.handleCron` shows up as `mycronservicehandlecron`. Set a `name` if your build minifies class names, so the slug doesn't change between deploys, or if the class and method names are long, since slugs are cut off at 50 characters. `excludeCronJobs` skips jobs whose slug matches a string exactly or a regular expression.

Each check-in sends the schedule and time zone of `@Cron`, as described in [Monitor Config From `@Cron`](https://docs.sentry.io/platforms/javascript/guides/nestjs/features/sentry-cron-decorator.md#monitor-config-from-cron). Jobs whose schedule can't be expressed in the Crons schedule format aren't monitored, and the SDK only logs this when `debug` is enabled. These include:

* Schedules that run more often than once a minute, such as every 10 seconds.
* `@Cron` with a `utcOffset`, or a `timeZone` that isn't an IANA name such as `America/Los_Angeles`.
* Month numbers with `@nestjs/schedule` 3 and earlier. Use names such as `MAY` instead.
* A `*/n` or full-range day field (such as `1-31` or `sun-sat`) combined with a restricted other day field, such as `0 9 */2 * 1`.

To monitor one of these jobs, create the monitor in Sentry first and add `@SentryCron` to the method. `@Cron` with a `Date` runs once and isn't monitored.

### [Monitor Specific Methods](https://docs.sentry.io/platforms/javascript/guides/nestjs/features/sentry-cron-decorator.md#monitor-specific-methods)

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.

```typescript
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
  }
}
```

`monitorCronJobs` skips methods that have `@SentryCron`, so they check in once, with the slug and options you pass to `@SentryCron`.

#### [Monitor Config From `@Cron`](https://docs.sentry.io/platforms/javascript/guides/nestjs/features/sentry-cron-decorator.md#monitor-config-from-cron)

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`:

```typescript
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 }`.

---

Find other pages: [Sitemap](https://docs.sentry.io/sitemap.md) lists every page with a one-line summary, and [llms.txt](https://docs.sentry.io/llms.txt) gives an overview with guidance for agents.
