|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace JustBetter\Sentry\Model; |
| 4 | + |
| 5 | +use JustBetter\Sentry\Helper\Data; |
| 6 | +use Magento\Cron\Model\Schedule; |
| 7 | +use Magento\Customer\Model\Session; |
| 8 | +use Sentry\CheckInStatus; |
| 9 | +use Sentry\MonitorConfig; |
| 10 | +use Sentry\MonitorSchedule; |
| 11 | + |
| 12 | +class SentryCron |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var array |
| 16 | + */ |
| 17 | + protected array $runningCheckins = []; |
| 18 | + |
| 19 | + /** |
| 20 | + * SentryLog constructor. |
| 21 | + * |
| 22 | + * @param Data $data |
| 23 | + * @param Session $customerSession |
| 24 | + */ |
| 25 | + public function __construct( |
| 26 | + protected Data $data, |
| 27 | + protected Session $customerSession, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Send the status of a cron schedule to Sentry. |
| 33 | + * |
| 34 | + * @param Schedule $schedule |
| 35 | + */ |
| 36 | + public function sendScheduleStatus(Schedule $schedule) |
| 37 | + { |
| 38 | + if (!$this->data->isActive() || |
| 39 | + !$this->data->isCronMonitoringEnabled() || |
| 40 | + !in_array( |
| 41 | + $schedule->getJobCode(), |
| 42 | + $this->data->getTrackCrons() |
| 43 | + ) |
| 44 | + ) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + $status = $schedule->getStatus(); |
| 49 | + if (!in_array($status, [ |
| 50 | + Schedule::STATUS_RUNNING, |
| 51 | + Schedule::STATUS_SUCCESS, |
| 52 | + Schedule::STATUS_ERROR, |
| 53 | + ])) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + /** @var array|null $cronExpressionArr */ |
| 58 | + $cronExpressionArr = $schedule->getCronExprArr(); |
| 59 | + $monitorConfig = null; |
| 60 | + if (!empty($cronExpressionArr)) { |
| 61 | + $cronExpression = implode(' ', $cronExpressionArr); |
| 62 | + $monitorConfig = new MonitorConfig(MonitorSchedule::crontab($cronExpression)); |
| 63 | + } |
| 64 | + |
| 65 | + if ($status === Schedule::STATUS_RUNNING) { |
| 66 | + if (!isset($this->runningCheckins[$schedule->getId()])) { |
| 67 | + $this->startCheckin($schedule, $monitorConfig); |
| 68 | + } |
| 69 | + |
| 70 | + return; |
| 71 | + } else { |
| 72 | + $this->finishCheckin($schedule, $monitorConfig); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Start the check-in for a given schedule. |
| 78 | + * |
| 79 | + * @param Schedule $schedule |
| 80 | + * @param MonitorConfig|null $monitorConfig |
| 81 | + */ |
| 82 | + public function startCheckin(Schedule $schedule, ?MonitorConfig $monitorConfig = null) |
| 83 | + { |
| 84 | + $this->runningCheckins[$schedule->getId()] = [ |
| 85 | + 'started_at' => microtime(true), |
| 86 | + 'check_in_id' => \Sentry\captureCheckIn( |
| 87 | + slug: $schedule->getJobCode(), |
| 88 | + status: CheckInStatus::inProgress(), |
| 89 | + monitorConfig: $monitorConfig, |
| 90 | + ), |
| 91 | + ]; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Finish the check-in for a given schedule. |
| 96 | + * |
| 97 | + * @param Schedule $schedule |
| 98 | + * @param MonitorConfig|null $monitorConfig |
| 99 | + */ |
| 100 | + public function finishCheckin(Schedule $schedule, ?MonitorConfig $monitorConfig = null) |
| 101 | + { |
| 102 | + if (!isset($this->runningCheckins[$schedule->getId()])) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + \Sentry\captureCheckIn( |
| 107 | + slug: $schedule->getJobCode(), |
| 108 | + status: $schedule->getStatus() === Schedule::STATUS_SUCCESS ? CheckInStatus::ok() : CheckInStatus::error(), |
| 109 | + duration: microtime(true) - $this->runningCheckins[$schedule->getId()]['started_at'], |
| 110 | + monitorConfig: $monitorConfig, |
| 111 | + checkInId: $this->runningCheckins[$schedule->getId()]['check_in_id'], |
| 112 | + ); |
| 113 | + |
| 114 | + unset($this->runningCheckins[$schedule->getId()]); |
| 115 | + } |
| 116 | +} |
0 commit comments