Lines
100.00%
6 / 6
Methods
100.00%
3 / 3
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| shouldShutdown | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | ||
| shutdown | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| 17 | final readonly class PredisShutdown implements ShutdownInterface | |
| 18 | { | |
| 19 | public function __construct( | |
| 20 | private Client $redis, | |
| 21 | private string $key = 'hermes_shutdown', | |
| 22 | ) {} | |
| 23 | ||
| 24 | /** | |
| 25 | * Returns true when the stored shutdown time is not in the future | |
| 26 | * and is later than the moment this worker started ($startTime). | |
| 27 | */ | |
| 28 | #[\Override] | |
| 29 | public function shouldShutdown(DateTimeInterface $startTime): bool | |
| 30 | { | |
| 31 | // a missing key reads as false (phpredis) or null (Predis), which parse() turns into null | |
| 32 | $shutdownTime = ShutdownTime::parse($this->redis->get($this->key)); | |
| 33 | ||
| 34 | return $shutdownTime !== null && ShutdownTime::isRequested($shutdownTime, $startTime, microtime(true)); | |
| 35 | } | |
| 36 | ||
| 37 | /** | |
| 38 | * Sets to Redis value `$shutdownTime` (or the current time) to `$key` defined in constructor. | |
| 39 | */ | |
| 40 | #[\Override] | |
| 41 | public function shutdown(?DateTimeInterface $shutdownTime = null): bool | |
| 42 | { | |
| 43 | $shutdownTime ??= new DateTimeImmutable(); | |
| 44 | ||
| 45 | /** @var Status $response */ | |
| 46 | $response = $this->redis->set($this->key, ShutdownTime::format($shutdownTime)); | |
| 47 | ||
| 48 | return $response->getPayload() === 'OK'; | |
| 49 | } | |
| 50 | } |