设置定时任务
Sentry Crons 允许您监控应用程序中任何计划的、重复的任务的正常运行时间和性能。
一旦实现,它将允许您获取警报和指标,帮助您解决错误、检测超时并防止服务中断。
- Use our getting started guide to install and configure the Sentry PHP SDK (min v3.16.0) for your recurring job.
- Create and configure your first Monitor.
Check-in monitoring allows you to track a job's progress by completing two check-ins: one at the start of your job and another at the end of your job. This two-step process allows Sentry to notify you if your job didn't start when expected (missed) or if it exceeded its maximum runtime (failed).
// 🟡 Notify Sentry your job is running:
$checkInId = \Sentry\captureCheckIn(
slug: '<monitor-slug>',
status: CheckInStatus::inProgress()
);
// Execute your scheduled task here...
// 🟢 Notify Sentry your job has completed successfully:
\Sentry\captureCheckIn(
slug: '<monitor-slug>',
status: CheckInStatus::ok(),
checkInId: $checkInId,
);
If your job execution fails, you can notify Sentry about the failure:
// 🔴 Notify Sentry your job has failed:
\Sentry\captureCheckIn(
slug: '<monitor-slug>',
status: CheckInStatus::error(),
checkInId: $checkInId,
);
Alternatively, you can use withMonitor()
to monitor a callback.
\Sentry\withMonitor(
slug: '<monitor-slug>',
callback: fn () => doSomething(),
);
This will create an in-progress
check-in and either an ok
or error
check-in, depending on if your provided callback threw an exception.
Heartbeat monitoring notifies Sentry of a job's status through one check-in. This setup will only notify you if your job didn't start when expected (missed). If you need to track a job to see if it exceeded its maximum runtime (failed), use check-ins instead.
// Execute your scheduled task...
// 🟢 Notify Sentry your job completed successfully:
\Sentry\captureCheckIn(
slug: '<monitor-slug>',
status: CheckInStatus::ok(),
duration: 10, // Optional duration in seconds
);
If your job execution fails, you can:
// 🔴 Notify Sentry your job has failed:
\Sentry\captureCheckIn(
slug: '<monitor-slug>',
status: CheckInStatus::error(),
duration: 10, // Optional duration in seconds
);
You can create and update your Monitors programmatically with code rather than creating and configuring them in Sentry.io.
// Create a crontab schedule object (every 10 minutes)
$monitorSchedule = \Sentry\MonitorSchedule::crontab('*/10 * * * *');
// Or create an interval schedule object (every 10 minutes)
$monitorSchedule = \Sentry\MonitorSchedule::interval(10, MonitorScheduleUnit::minute());
Supported units are:
MonitorScheduleUnit::minute()
MonitorScheduleUnit::hour()
MonitorScheduleUnit::day()
MonitorScheduleUnit::week()
MonitorScheduleUnit::month()
MonitorScheduleUnit::year()
// Create a config object
$monitorConfig = new \Sentry\MonitorConfig(
$monitorSchedule,
checkinMargin: 5, // Optional check-in margin in minutes
maxRuntime: 15, // Optional max runtime in minutes
timezone: 'Europe/Vienna', // Optional timezone
failureIssueThreshold: 2 //Optional failure issue threshold
recoveryThreshold: 5 // Optional recovery threshold
);
// 🟡 Notify Sentry your job is running:
$checkInId = \Sentry\captureCheckIn(
slug: '<monitor-slug>',
status: CheckInStatus::inProgress(),
monitorConfig: $monitorConfig,
);
// Execute your scheduled task here...
// 🟢 Notify Sentry your job has completed successfully:
\Sentry\captureCheckIn(
slug: '<monitor-slug>',
status: CheckInStatus::ok(),
checkInId: $checkInId,
);
当您的重复任务未能签到(错过)、运行时间超过配置的最大运行时间(失败),或手动报告失败时,Sentry 会创建一个带有监控标签的错误事件。
要接收这些事件的警报:
- 在侧边栏中导航到 警报。
- 创建一个新的警报,并在“错误”下选择“问题”作为警报类型。
- 配置您的警报并定义一个过滤器匹配:
事件的标签匹配 {key} {match} {value}
。
示例:事件的标签匹配 monitor.slug 等于 my-monitor-slug-here
了解更多请参阅 问题警报配置。
Crons 对签到实施速率限制以防止滥用和资源过度使用。具体来说,每个现有监控环境每分钟最多只能发送 6 个签到。此限制按项目强制执行,意味着该速率限制适用于给定项目中的所有监控环境。您可以在 Usage Stats 页面检查是否有任何签到被丢弃。
为了避免签到被丢弃,重要的是要高效地管理和分配签到,使其在速率限制内。这将有助于保持准确的监控,并确保所有关键