Upserting Cron Monitors
Programmatic monitor creation and management for your cron jobs.
You can create and update your Monitors programmatically with code rather than creating and configuring them in Sentry.io.
To create/update a monitor, use Sentry.withMonitor()
and pass in your monitor configuration as a third parameter:
Copied
const monitorConfig = {
schedule: {
type: "crontab",
value: "* * * * *",
},
checkinMargin: 2, // In minutes. Optional.
maxRuntime: 10, // In minutes. Optional.
timezone: "America/Los_Angeles", // Optional.
};
Sentry.withMonitor(
"<monitor-slug>",
() => {
// Execute your scheduled task here...
},
monitorConfig,
);
To configure the monitor's check-ins, use Sentry.captureCheckIn()
and pass in your monitor configuration as a second parameter:
Copied
const monitorConfig = {
schedule: {
type: "crontab",
value: "* * * * *",
},
checkinMargin: 2, // In minutes. Optional.
maxRuntime: 10, // In minutes. Optional.
timezone: "America/Los_Angeles", // Optional.
};
// 🟡 Notify Sentry your job is running:
const checkInId = Sentry.captureCheckIn(
{
monitorSlug: "<monitor-slug>",
status: "in_progress",
},
monitorConfig,
);
// Execute your scheduled task here...
// 🟢 Notify Sentry your job has completed successfully:
Sentry.captureCheckIn(
{
// Make sure this variable is named `checkInId`
checkInId,
monitorSlug: "<monitor-slug>",
status: "ok",
},
monitorConfig,
);
When creating or updating monitors, you can configure the following properties:
monitorSlug
: Unique identifier for your monitor (must match the slug created in Sentry UI)status
: Current check-in status ('in_progress'
,'ok'
,'error'
)
checkInId
: Unique identifier for this specific check-in (auto-generated if not provided)duration
: How long the job took to run (in milliseconds)environment
: Environment where the job is running ('production'
,'staging'
,'development'
)
When creating monitors programmatically, you can also specify:
schedule
: Cron schedule expression (e.g.,'0 2 * * *'
for daily at 2 AM)checkinMargin
: Grace period in minutes after expected check-in time before marking as missedmaxRuntime
: Maximum runtime in minutes before marking as failed due to timeouttimezone
: Timezone for the schedule (e.g.,'America/Los_Angeles'
)failureIssueThreshold
: Number of consecutive failures before creating an issue (default: 1)recoveryThreshold
: Number of consecutive successes before resolving an issue (default: 1)
Copied
// Basic check-in
Sentry.captureCheckIn({
monitorSlug: "my-job",
status: "ok",
duration: 1500, // 1.5 seconds
environment: "production",
});
// Monitor creation/update with configuration
Sentry.captureCheckIn({
monitorSlug: "new-job",
status: "ok",
monitorConfig: {
schedule: {
type: "crontab",
value: "0 2 * * *",
},
checkinMargin: 5,
maxRuntime: 30,
timezone: "America/Los_Angeles",
failureIssueThreshold: 2,
recoveryThreshold: 1,
},
});
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").
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").