Skip to content

Commit

Permalink
Add RedisProxyShutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
ricco24 committed Nov 13, 2024
1 parent 5a92c2d commit 99d9634
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]


### Added
- RedisProxyShutdown

## [1.1.0] - 2024-04-17
### Added
Expand Down
54 changes: 54 additions & 0 deletions src/Shutdown/RedisProxyShutdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);

namespace Efabrica\HermesExtension\Shutdown;

use DateTime;
use RedisProxy\RedisProxy;
use Tomaj\Hermes\Shutdown\ShutdownInterface;

class RedisProxyShutdown implements ShutdownInterface
{
/** @var string */
private $key;

/** @var RedisProxy */
private $redisProxy;

public function __construct(RedisProxy $redisProxy, string $key = 'hermes_shutdown')
{
$this->redisProxy = $redisProxy;
$this->key = $key;
}

public function shouldShutdown(DateTime $startTime): bool
{
// load UNIX timestamp from redis
$shutdownTime = $this->redisProxy->get($this->key);
if ($shutdownTime == null) {
return false;
}
$shutdownTime = (int) $shutdownTime;

// do not shutdown if shutdown time is in future
if ($shutdownTime > time()) {
return false;
}

// do not shutdown if hermes started after shutdown time
if ($shutdownTime < $startTime->getTimestamp()) {
return false;
}

return true;
}

public function shutdown(DateTime $shutdownTime = null): bool
{
if ($shutdownTime === null) {
$shutdownTime = new DateTime();
}

return $this->redisProxy->set($this->key, $shutdownTime->format('U'));
}
}

0 comments on commit 99d9634

Please sign in to comment.