This commit is contained in:
xl
2024-09-04 09:40:31 +08:00
parent 0e1fcef911
commit 80b9ae8f1e
4 changed files with 113 additions and 77 deletions
+34 -9
View File
@@ -3,8 +3,8 @@
namespace Kiri\Server\Processes;
use Kiri\Di\Context;
use Swoole\Coroutine;
use Swoole\Process;
/**
*
@@ -15,6 +15,9 @@ abstract class AbstractProcess implements OnProcessInterface
private bool $stop = false;
public Process $process;
/**
* @var bool
*/
@@ -109,24 +112,46 @@ abstract class AbstractProcess implements OnProcessInterface
/**
* @return $this
* @return void
*/
abstract public function onSigterm(): static;
abstract public function onSigterm(): void;
/**
* @param Process $process
* @return AbstractProcess
*/
public function onShutdown(Process $process): static
{
$this->process = $process;
if ($this->enable_coroutine) {
Coroutine::create(fn () => $this->coroutineWaitSignal());
} else {
pcntl_signal(SIGTERM, [$this, 'pointWaitSignal']);
}
return $this;
}
/**
* @param $data
* @return void
*/
protected function onShutdown($data): void
private function pointWaitSignal($data): void
{
$this->stop = true;
$value = Context::get('waite:process:message');
\Kiri::getLogger()->alert('AbstractProcess ' . $this->getName() . ' stop');
if (!is_null($value) && Coroutine::exists((int)$value)) {
Coroutine::cancel((int)$value);
}
}
/**
* @return void
*/
private function coroutineWaitSignal(): void
{
$value = Coroutine::waitSignal(SIGTERM);
if ($value) {
$this->stop = true;
}
}
}