Lines
100.00%
24 / 24
Functions and Methods
100.00%
3 / 3
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| SupervisionConfig | 100.00% | 24 / 24 | 100.00% | 3 / 3 | 4 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| fromSection | 100.00% | 22 / 22 | 100.00% | 1 / 1 | 2 | |||||
| killsUnresponsiveWorkers | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Config; | |
| 6 | ||
| 7 | use Stewart\Contracts\Time\Duration; | |
| 8 | use Stewart\Runtime\Exception\ConfigurationError; | |
| 9 | use Stewart\Runtime\Exception\ConfigurationException; | |
| 10 | ||
| 11 | final readonly class SupervisionConfig | |
| 12 | { | |
| 13 | public function __construct( | |
| 14 | public int $restartAttempts, | |
| 15 | public Duration $restartWindow, | |
| 16 | public BackoffPolicy $restartBackoff, | |
| 17 | public OptionalDuration $pingInterval, | |
| 18 | public Duration $lagThreshold, | |
| 19 | public int $unresponsiveAfter, | |
| 20 | public OptionalDuration $initializeTimeout, | |
| 21 | public OptionalDuration $readyTimeout, | |
| 22 | ) {} | |
| 23 | ||
| 24 | /** @throws ConfigurationException */ | |
| 25 | public static function fromSection(ConfigSection $supervision): self | |
| 26 | { | |
| 27 | $oneMillisecond = Duration::milliseconds(1); | |
| 28 | $restartAttempts = $supervision->readInt('restart_attempts'); | |
| 29 | $restartWindow = $supervision->readDuration('restart_window', $oneMillisecond); | |
| 30 | $restartBackoff = $supervision->readBackoff('restart_'); | |
| 31 | $restartBackoffTotal = $restartBackoff->sumDelaysUpTo($restartAttempts); | |
| 32 | ||
| 33 | if (!$restartWindow->isLongerThan($restartBackoffTotal)) { | |
| 34 | throw ConfigurationException::createForReason( | |
| 35 | ConfigurationError::RestartWindowTooShort, | |
| 36 | window: (string) $restartWindow, | |
| 37 | attempts: $restartAttempts, | |
| 38 | backoffTotal: (string) $restartBackoffTotal, | |
| 39 | ); | |
| 40 | } | |
| 41 | ||
| 42 | return new self( | |
| 43 | restartAttempts: $restartAttempts, | |
| 44 | restartWindow: $restartWindow, | |
| 45 | restartBackoff: $restartBackoff, | |
| 46 | pingInterval: $supervision->readOptionalDuration('ping_interval', $oneMillisecond), | |
| 47 | lagThreshold: $supervision->readDuration('lag_threshold', $oneMillisecond), | |
| 48 | unresponsiveAfter: $supervision->readInt('unresponsive_after'), | |
| 49 | initializeTimeout: $supervision->readOptionalDuration('initialize_timeout', $oneMillisecond), | |
| 50 | readyTimeout: $supervision->readOptionalDuration('ready_timeout', $oneMillisecond), | |
| 51 | ); | |
| 52 | } | |
| 53 | ||
| 54 | public function killsUnresponsiveWorkers(): bool | |
| 55 | { | |
| 56 | return $this->unresponsiveAfter > 0; | |
| 57 | } | |
| 58 | } |