Skip to content

Commit

Permalink
Add PHP Cron Monitor upserts (#7133)
Browse files Browse the repository at this point in the history
Co-authored-by: Shana Matthews <[email protected]>
  • Loading branch information
cleptric and shanamatthews authored Jun 22, 2023
1 parent 20369e6 commit 3f12b98
Showing 1 changed file with 67 additions and 3 deletions.
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);
```

1 comment on commit 3f12b98

@vercel
Copy link

@vercel vercel bot commented on 3f12b98 Jun 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

sentry-docs – ./

sentry-docs.sentry.dev
sentry-docs-git-master.sentry.dev
docs.sentry.io

Please sign in to comment.