Skip to content

Commit

Permalink
feat: Cron Monitor upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric committed Apr 5, 2023
1 parent e231db0 commit a939085
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/CheckIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ final class CheckIn
*/
private $duration;

/**
* @var \Sentry\MonitorConfig|null The monitor configuration
*/
private $monitorConfig;

/**
* @param int|float|null $duration The duration of the check-in in seconds
*/
Expand All @@ -47,7 +52,8 @@ public function __construct(
string $id = null,
?string $release = null,
?string $environment = null,
$duration = null
$duration = null,
?MonitorConfig $monitorConfig = null
) {
$this->setMonitorSlug($monitorSlug);
$this->setStatus($status);
Expand All @@ -56,6 +62,7 @@ public function __construct(
$this->setRelease($release ?? '');
$this->setEnvironment($environment ?? Event::DEFAULT_ENVIRONMENT);
$this->setDuration($duration);
$this->setMonitorConfig($monitorConfig);
}

public function getId(): string
Expand Down Expand Up @@ -123,4 +130,14 @@ public function setDuration($duration): void
{
$this->duration = $duration;
}

public function getMonitorConfig(): ?MonitorConfig
{
return $this->monitorConfig;
}

public function setMonitorConfig(?MonitorConfig $monitorConfig): void
{
$this->monitorConfig = $monitorConfig;
}
}
93 changes: 93 additions & 0 deletions src/MonitorConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace Sentry;

final class MonitorConfig
{
/**
* @var MonitorSchedule The schedule of the monitor
*/
private $schedule;

/**
* @var int|null The check-in margin in seconds
*/
private $checkinMargin;

/**
* @var int|null The maximum runtime in seconds
*/
private $maxRuntime;

/**
* @var string|null The timezone
*/
private $timezone;

public function __construct(
MonitorSchedule $schedule,
?int $checkinMargin,
?int $maxRuntime,
?string $timezone
) {
$this->schedule = $schedule;
$this->checkinMargin = $checkinMargin;
$this->maxRuntime = $maxRuntime;
$this->timezone = $timezone;
}

public function getShedule(): MonitorSchedule
{
return $this->schedule;
}

public function setSchedule(MonitorSchedule $schedule): void
{
$this->schedule = $schedule;
}

public function getCheckinMargin(): ?int
{
return $this->checkinMargin;
}

public function setCheckinMargin(?int $checkinMargin): void
{
$this->checkinMargin = $checkinMargin;
}

public function getMaxRuntime(): ?int
{
return $this->maxRuntime;
}

public function setMaxRuntime(?int $maxRuntime): void
{
$this->maxRuntime = $maxRuntime;
}

public function getTimezone(): ?string
{
return $this->timezone;
}

public function setTimezone(?string $timezone): void
{
$this->timezone = $timezone;
}

/**
* @return array<string, mixed>
*/
public function toArray(): array
{
return [
'schedule' => $this->schedule->toArray(),
'checkin_margin' => $this->checkinMargin,
'max_runtime' => $this->maxRuntime,
'timezone' => $this->timezone,
];
}
}
90 changes: 90 additions & 0 deletions src/MonitorSchedule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace Sentry;

final class MonitorSchedule
{
/**
* @var string The type of the schedule
*/
private $type;

/**
* @var string|int The value of the schedule
*/
private $value;

/**
* @var MonitorScheduleUnit|null The unit of the schedule
*/
private $unit;

public const TYPE_CRONTAB = 'crontab';

public const TYPE_INTERVAL = 'interval';

/**
* @param string $type The type of the schedule
* @param string|int $value The value of the schedule
* @param MonitorScheduleUnit|null $unit The unit of the schedule
*/
public function __construct(
string $type,
$value,
?MonitorScheduleUnit $unit = null
) {
$this->type = $type;
$this->value = $value;
$this->unit = $unit;
}

public function getType(): string
{
return $this->type;
}

public function setType(string $type): void
{
$this->type = $type;
}

/**
* @return string|int
*/
public function getValue()
{
return $this->value;
}

/**
* @param string|int $value
*/
public function setValue($value): void
{
$this->value = $value;
}

public function getUnit(): ?MonitorScheduleUnit
{
return $this->unit;
}

public function setUnit(?MonitorScheduleUnit $unit): void
{
$this->unit = $unit;
}

/**
* @return array<string, string|int>
*/
public function toArray(): array
{
return [
'type' => $this->type,
'value' => $this->value,
'unit' => (string) $this->unit,
];
}
}
67 changes: 67 additions & 0 deletions src/MonitorScheduleUnit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Sentry;

final class MonitorScheduleUnit implements \Stringable
{
/**
* @var string The value of the enum instance
*/
private $value;

/**
* @var array<string, self> A list of cached enum instances
*/
private static $instances = [];

private function __construct(string $value)
{
$this->value = $value;
}

public static function minute(): self
{
return self::getInstance('minute');
}

public static function hour(): self
{
return self::getInstance('hour');
}

public static function day(): self
{
return self::getInstance('day');
}

public static function week(): self
{
return self::getInstance('week');
}

public static function month(): self
{
return self::getInstance('month');
}

public static function year(): self
{
return self::getInstance('year');
}

public function __toString(): string
{
return $this->value;
}

private static function getInstance(string $value): self
{
if (!isset(self::$instances[$value])) {
self::$instances[$value] = new self($value);
}

return self::$instances[$value];
}
}
4 changes: 4 additions & 0 deletions src/Serializer/PayloadSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ private function serializeAsCheckInEvent(Event $event): string
'release' => $checkIn->getRelease(),
'environment' => $checkIn->getEnvironment(),
];

if (null !== $checkIn->getMonitorConfig()) {
$result['monitor_config'] = $checkIn->getMonitorConfig()->toArray();
}
}

return JSON::encode($result);
Expand Down

0 comments on commit a939085

Please sign in to comment.