Lines
32.14%
9 / 28
Functions and Methods
33.33%
4 / 12
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| AppActivityRecorder | 32.14% | 9 / 28 | 33.33% | 4 / 12 | 107.30 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| handlerFailed | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | |||||
| eventReceived | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| handlerCalled | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 2 | |||||
| eventDelivered | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| eventSuppressed | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 2 | |||||
| eventDropped | 0.00% | 0 / 8 | 0.00% | 0 / 1 | 6 | |||||
| scheduledRunStarted | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 2 | |||||
| scheduledRunSuppressed | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 2 | |||||
| scheduledRunFailed | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | |||||
| scheduleFailed | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| isWaitAbandonedByStop | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 3 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Worker; | |
| 6 | ||
| 7 | use Stewart\Contracts\Exception\WaitError; | |
| 8 | use Stewart\Contracts\Exception\WaitException; | |
| 9 | use Stewart\Contracts\Schedule\ScheduledRun; | |
| 10 | use Stewart\Contracts\Time\Instant; | |
| 11 | use Stewart\Runtime\Dispatch\DispatchListener; | |
| 12 | use Stewart\Runtime\Dispatch\RegisteredSubscription; | |
| 13 | use Stewart\Runtime\Lifecycle\AppFailurePhase; | |
| 14 | use Stewart\Runtime\Logging\EveryNthOccurrence; | |
| 15 | use Stewart\Runtime\Model\ResourceScope; | |
| 16 | use Stewart\Runtime\Schedule\ScheduleListener; | |
| 17 | use Stewart\Runtime\Schedule\ScheduleOrigin; | |
| 18 | use Stewart\Runtime\Scope\ScopeLifecycle; | |
| 19 | use Throwable; | |
| 20 | ||
| 21 | final readonly class AppActivityRecorder implements DispatchListener, ScheduleListener | |
| 22 | { | |
| 23 | private EveryNthOccurrence $dropReports; | |
| 24 | ||
| 25 | public function __construct( | |
| 26 | private AppFailureReporter $failures, | |
| 27 | private AppActivityCounters $activityCounters, | |
| 28 | private WorkerLogger $logger, | |
| 29 | private ScopeLifecycle $scopes, | |
| 30 | private AppTracer $tracer, | |
| 31 | ) { | |
| 32 | $this->dropReports = EveryNthOccurrence::forRepeatedWarnings(); | |
| 33 | } | |
| 34 | ||
| 35 | public function handlerFailed(RegisteredSubscription $subscription, Throwable $error): void | |
| 36 | { | |
| 37 | if ($this->isWaitAbandonedByStop($subscription->scope, $error)) { | |
| 38 | return; | |
| 39 | } | |
| 40 | ||
| 41 | $origin = \sprintf('subscription %s (%s)', $subscription->id, $subscription->describeMatch()); | |
| 42 | ||
| 43 | $this->failures->report($subscription->scope, AppFailurePhase::Handler, $error, $origin); | |
| 44 | } | |
| 45 | ||
| 46 | public function eventReceived(RegisteredSubscription $subscription, object $event): void | |
| 47 | { | |
| 48 | $this->tracer->traceEventReceived($subscription, $event); | |
| 49 | } | |
| 50 | ||
| 51 | public function handlerCalled(RegisteredSubscription $subscription, Instant $calledAt): void | |
| 52 | { | |
| 53 | $this->activityCounters->findOrCreateActivityForScope($subscription->scope)->recordHandledEvent($calledAt); | |
| 54 | $this->tracer->traceHandlerCalled($subscription); | |
| 55 | } | |
| 56 | ||
| 57 | public function eventDelivered(RegisteredSubscription $subscription): void | |
| 58 | { | |
| 59 | $this->activityCounters->findOrCreateActivityForScope($subscription->scope)->recordDeliveredEvent(); | |
| 60 | } | |
| 61 | ||
| 62 | public function eventSuppressed(RegisteredSubscription $subscription, ?object $event): void | |
| 63 | { | |
| 64 | $this->activityCounters->findOrCreateActivityForScope($subscription->scope)->recordSuppressedWork(); | |
| 65 | $this->tracer->traceSkippedWhilePaused($subscription, $event); | |
| 66 | } | |
| 67 | ||
| 68 | public function eventDropped(RegisteredSubscription $subscription, int $droppedSoFar): void | |
| 69 | { | |
| 70 | $this->activityCounters->findOrCreateActivityForScope($subscription->scope)->recordDroppedEvent(); | |
| 71 | ||
| 72 | if (!$this->dropReports->includesOccurrence($droppedSoFar)) { | |
| 73 | return; | |
| 74 | } | |
| 75 | ||
| 76 | $this->logger->forScope($subscription->scope)->warning('Handler is falling behind; dropping queued events', [ | |
| 77 | 'subscription' => $subscription->id->value, | |
| 78 | 'selector' => $subscription->describeMatch(), | |
| 79 | 'dropped' => $droppedSoFar, | |
| 80 | ]); | |
| 81 | } | |
| 82 | ||
| 83 | public function scheduledRunStarted(ScheduleOrigin $origin, ScheduledRun $run): void | |
| 84 | { | |
| 85 | $this->activityCounters->findOrCreateActivityForScope($origin->scope)->recordScheduleRun(); | |
| 86 | $this->tracer->traceScheduleRun($origin, $run); | |
| 87 | } | |
| 88 | ||
| 89 | public function scheduledRunSuppressed(ScheduleOrigin $origin, ScheduledRun $run): void | |
| 90 | { | |
| 91 | $this->activityCounters->findOrCreateActivityForScope($origin->scope)->recordSuppressedWork(); | |
| 92 | $this->tracer->traceScheduleRunSkippedWhilePaused($origin, $run); | |
| 93 | } | |
| 94 | ||
| 95 | public function scheduledRunFailed(ScheduleOrigin $origin, Throwable $error): void | |
| 96 | { | |
| 97 | if ($this->isWaitAbandonedByStop($origin->scope, $error)) { | |
| 98 | return; | |
| 99 | } | |
| 100 | ||
| 101 | $this->failures->report($origin->scope, AppFailurePhase::Handler, $error, $origin->label()); | |
| 102 | } | |
| 103 | ||
| 104 | public function scheduleFailed(ScheduleOrigin $origin, Throwable $error): void | |
| 105 | { | |
| 106 | $this->failures->report($origin->scope, AppFailurePhase::Schedule, $error, $origin->label()); | |
| 107 | } | |
| 108 | ||
| 109 | // Stopping an app abandons its pending waits; that is part of the stop, not a handler failure. | |
| 110 | private function isWaitAbandonedByStop(ResourceScope $scope, Throwable $error): bool | |
| 111 | { | |
| 112 | return $error instanceof WaitException && $error->reason === WaitError::Abandoned && $this->scopes->findOrCreateStateOf($scope)->isClosed(); | |
| 113 | } | |
| 114 | } |