-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |