Skip to content

Instantly share code, notes, and snippets.

@medeirosinacio
Last active January 10, 2024 20:19
Show Gist options
  • Save medeirosinacio/8bb7db68671f6fb219bdad3cdb48e5d8 to your computer and use it in GitHub Desktop.
Save medeirosinacio/8bb7db68671f6fb219bdad3cdb48e5d8 to your computer and use it in GitHub Desktop.
Manual Graceful Shutdown PHP Shared Memory
<?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