Last active
January 10, 2024 20:19
-
-
Save medeirosinacio/8bb7db68671f6fb219bdad3cdb48e5d8 to your computer and use it in GitHub Desktop.
Manual Graceful Shutdown PHP Shared Memory
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
<?php | |
declare(strict_types=1); | |
use SysvSharedMemory; | |
class GracefulShutdown | |
{ | |
public const KEY = 666; | |
public const SIGTERM = 'SIGTERM'; | |
public function signalReceived(): bool | |
{ | |
if (! shm_has_var($this->identifier(), self::KEY)) { | |
$this->clearSignal(); | |
} | |
return shm_get_var($this->identifier(), self::KEY) === self::SIGTERM; | |
} | |
public function sendSignal(string $signal): void | |
{ | |
shm_put_var($this->identifier(), self::KEY, $signal); | |
} | |
public function clearSignal(): void | |
{ | |
$this->sendSignal(''); | |
} | |
private function identifier(): SysvSharedMemory|false | |
{ | |
return shm_attach(self::KEY, 1024, 0777); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment