Lines
0.00%
0 / 57
Functions and Methods
0.00%
0 / 6
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| PongBuilder | 0.00% | 0 / 57 | 0.00% | 0 / 6 | 240 | 0.00% | 0 / 1 | |||
| __construct | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| buildPongFor | 0.00% | 0 / 13 | 0.00% | 0 / 1 | 30 | |||||
| buildActivityReport | 0.00% | 0 / 20 | 0.00% | 0 / 1 | 2 | |||||
| buildSubscriptionReport | 0.00% | 0 / 12 | 0.00% | 0 / 1 | 6 | |||||
| buildScheduleReport | 0.00% | 0 / 6 | 0.00% | 0 / 1 | 2 | |||||
| findEarliestRun | 0.00% | 0 / 5 | 0.00% | 0 / 1 | 30 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Worker; | |
| 6 | ||
| 7 | use Stewart\Contracts\Time\Clock; | |
| 8 | use Stewart\Contracts\Time\Instant; | |
| 9 | use Stewart\Runtime\Dispatch\LocalDispatcher; | |
| 10 | use Stewart\Runtime\Dispatch\SubscriptionActivity; | |
| 11 | use Stewart\Runtime\Health\ProcessMemory; | |
| 12 | use Stewart\Runtime\Ipc\Message\AppActivityReport; | |
| 13 | use Stewart\Runtime\Ipc\Message\Ping; | |
| 14 | use Stewart\Runtime\Ipc\Message\Pong; | |
| 15 | use Stewart\Runtime\Ipc\Message\ScheduleActivityReport; | |
| 16 | use Stewart\Runtime\Ipc\Message\SubscriptionActivityReport; | |
| 17 | use Stewart\Runtime\Lifecycle\AppState; | |
| 18 | use Stewart\Runtime\Model\ResourceScope; | |
| 19 | use Stewart\Runtime\Schedule\ScheduleEntry; | |
| 20 | use Stewart\Runtime\Schedule\ScheduleRegistry; | |
| 21 | use Stewart\Store\GuardedStoreBackend; | |
| 22 | ||
| 23 | final readonly class PongBuilder | |
| 24 | { | |
| 25 | public function __construct( | |
| 26 | private AppLifecycle $apps, | |
| 27 | private LocalDispatcher $dispatcher, | |
| 28 | private ScheduleRegistry $schedules, | |
| 29 | private AppActivityCounters $activityCounters, | |
| 30 | private Clock $clock, | |
| 31 | private ProcessMemory $processMemory, | |
| 32 | private ?GuardedStoreBackend $store = null, | |
| 33 | ) {} | |
| 34 | ||
| 35 | public function buildPongFor(Ping $ping): Pong | |
| 36 | { | |
| 37 | $reports = []; | |
| 38 | ||
| 39 | foreach ($this->apps->listSlots() as $slot) { | |
| 40 | $reports[] = $this->buildActivityReport($slot->scope(), $slot->state); | |
| 41 | } | |
| 42 | ||
| 43 | $shared = $this->buildActivityReport(ResourceScope::shared(), AppState::Running); | |
| 44 | ||
| 45 | if ($shared->subscriptions > 0 || $shared->schedules > 0 || !$this->activityCounters->findOrCreateActivityForScope(ResourceScope::shared())->isIdle()) { | |
| 46 | $reports[] = $shared; | |
| 47 | } | |
| 48 | ||
| 49 | return new Pong( | |
| 50 | nonce: $ping->nonce, | |
| 51 | loopLag: $this->clock->getNow()->elapsedSince($ping->sentAt), | |
| 52 | memoryBytes: $this->processMemory->measureResidentBytes(), | |
| 53 | apps: $reports, | |
| 54 | store: $this->store?->getHealth(), | |
| 55 | ); | |
| 56 | } | |
| 57 | ||
| 58 | private function buildActivityReport(ResourceScope $scope, AppState $state): AppActivityReport | |
| 59 | { | |
| 60 | $activity = $this->activityCounters->findOrCreateActivityForScope($scope); | |
| 61 | $subscriptionReports = array_map($this->buildSubscriptionReport(...), $this->dispatcher->listActivitiesOf($scope)); | |
| 62 | $scheduleReports = array_map($this->buildScheduleReport(...), $this->schedules->listEntriesOf($scope)); | |
| 63 | ||
| 64 | return new AppActivityReport( | |
| 65 | scope: $scope, | |
| 66 | state: $state, | |
| 67 | subscriptions: \count($subscriptionReports), | |
| 68 | schedules: \count($scheduleReports), | |
| 69 | delivered: $activity->delivered, | |
| 70 | subscriptionDropped: $activity->dropped, | |
| 71 | scheduleRuns: $activity->scheduleRuns, | |
| 72 | publishes: $activity->publishes, | |
| 73 | failures: $activity->failures, | |
| 74 | suppressed: $activity->suppressed, | |
| 75 | handled: $activity->handled, | |
| 76 | lastHandledAt: $activity->lastHandledAt, | |
| 77 | nextRunAt: $this->findEarliestRun($scheduleReports), | |
| 78 | subscriptionReports: $subscriptionReports, | |
| 79 | scheduleReports: $scheduleReports, | |
| 80 | ); | |
| 81 | } | |
| 82 | ||
| 83 | private function buildSubscriptionReport(SubscriptionActivity $activity): SubscriptionActivityReport | |
| 84 | { | |
| 85 | $subscription = $activity->subscription; | |
| 86 | ||
| 87 | return new SubscriptionActivityReport( | |
| 88 | subscriptionId: $subscription->id, | |
| 89 | kind: $subscription->kind, | |
| 90 | match: $subscription->describeMatch(), | |
| 91 | exact: $subscription->entityFilter === null && $subscription->selector->findExactPattern() !== null, | |
| 92 | received: $activity->received, | |
| 93 | handled: $activity->handled, | |
| 94 | dropped: $activity->dropped, | |
| 95 | lastReceivedAt: $activity->lastReceivedAt, | |
| 96 | lastHandledAt: $activity->lastHandledAt, | |
| 97 | ); | |
| 98 | } | |
| 99 | ||
| 100 | private function buildScheduleReport(ScheduleEntry $entry): ScheduleActivityReport | |
| 101 | { | |
| 102 | return new ScheduleActivityReport( | |
| 103 | taskId: $entry->origin->taskId, | |
| 104 | description: $entry->origin->description, | |
| 105 | nextRunAt: $entry->getNextRunAt(), | |
| 106 | lastRunAt: $entry->lastRunAt, | |
| 107 | ); | |
| 108 | } | |
| 109 | ||
| 110 | /** @param list<ScheduleActivityReport> $scheduleReports */ | |
| 111 | private function findEarliestRun(array $scheduleReports): ?Instant | |
| 112 | { | |
| 113 | $earliest = null; | |
| 114 | ||
| 115 | foreach ($scheduleReports as $report) { | |
| 116 | if ($report->nextRunAt !== null && ($earliest === null || $earliest->isAfter($report->nextRunAt))) { | |
| 117 | $earliest = $report->nextRunAt; | |
| 118 | } | |
| 119 | } | |
| 120 | ||
| 121 | return $earliest; | |
| 122 | } | |
| 123 | } |