Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PHP Cron Monitor upserts #7133

Merged
merged 5 commits into from
Jun 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 67 additions & 3 deletions src/platform-includes/crons/setup/php.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ SentrySdk::getCurrentHub()->captureEvent($event);
// 🟢 Notify Sentry your job has completed successfully:
$event = Event::createCheckIn();
$event->setCheckIn(new CheckIn(
id: $checkIn->getId(),
monitorSlug: '<monitor-slug>',
status: CheckInStatus::ok(),
id: $checkIn->getId(),
));
SentrySdk::getCurrentHub()->captureEvent($event);
```
Expand All @@ -30,9 +30,9 @@ If your job execution fails, you can notify Sentry about the failure:
// 🔴 Notify Sentry your job has failed:
$event = Event::createCheckIn();
$event->setCheckIn(new CheckIn(
id: $checkIn->getId(),
monitorSlug: '<monitor-slug>',
status: CheckInStatus::error(),
id: $checkIn->getId(),
));
SentrySdk::getCurrentHub()->captureEvent($event);
```
Expand All @@ -49,6 +49,9 @@ $event = Event::createCheckIn();
$checkIn = new CheckIn(
monitorSlug: '<monitor-slug>',
status: CheckInStatus::ok(),
release: '1.0.0', // Optional release of your application
environment: 'production', // Optional environment your cron is running on
duration: 10, // Optional duration in seconds
);
$event->setCheckIn($checkIn);
SentrySdk::getCurrentHub()->captureEvent($event);
Expand All @@ -60,9 +63,70 @@ If your job execution fails, you can:
// 🔴 Notify Sentry your job has failed:
$event = Event::createCheckIn();
$event->setCheckIn(new CheckIn(
id: $checkIn->getId(),
monitorSlug: '<monitor-slug>',
status: CheckInStatus::error(),
id: $checkIn->getId(),
release: '1.0.0', // Optional release of your application
environment: 'production', // Optional environment your cron is running on
duration: 10, // Optional duration in seconds
));
SentrySdk::getCurrentHub()->captureEvent($event);
```

## Upserting Cron Monitors

You can create and update your Monitors programmatically with code
rather than [creating and configuring them in Sentry.io](https://sentry.io/crons/create/).

```php
// Create a crontab schedule object (every 10 minutes)
$monitorSchedule = MonitorSchedule::crontab('*/10 * * * *');

// Or create an interval schedule object (every 10 minutes)
$monitorSchedule = MonitorSchedule::interval(10, MonitorScheduleUnit::minute());
```

Supported units are:

- `MonitorScheduleUnit::minute()`
- `MonitorScheduleUnit::hour()`
- `MonitorScheduleUnit::day()`
- `MonitorScheduleUnit::week()`
- `MonitorScheduleUnit::month()`
- `MonitorScheduleUnit::year()`

```php
// Create a config object
$monitorConfig = new MonitorConfig(
$monitorSchedule,
checkinMargin: 5, // Optional check-in margin in minutes
maxRuntime: 15, // Optional max runtime in minutes
timezone: 'Europe/Vienna', // Optional timezone
);

// 🟡 Notify Sentry your job is running:
$event = Event::createCheckIn();
$checkIn = new CheckIn(
monitorSlug: '<monitor-slug>',
status: CheckInStatus::inProgress(),
release: '1.0.0', // Optional release of your application
environment: 'production', // Optional environment your cron is running on
monitorConfig: $monitorConfig,
);
$event->setCheckIn($checkIn);
SentrySdk::getCurrentHub()->captureEvent($event);

// Execute your scheduled task here...

// 🟢 Notify Sentry your job has completed successfully:
$event = Event::createCheckIn();
$event->setCheckIn(new CheckIn(
monitorSlug: '<monitor-slug>',
status: CheckInStatus::ok(),
id: $checkIn->getId(),
release: '1.0.0', // Optional release of your application
environment: 'production', // Optional environment your cron is running on
monitorConfig: $monitorConfig,
));
SentrySdk::getCurrentHub()->captureEvent($event);
```