Using instance instead of class

This commit is contained in:
Joop Schilder 2019-01-16 17:38:37 +01:00
parent 4dfd90646c
commit 411dc2aefc
1 changed files with 55 additions and 27 deletions

View File

@ -102,11 +102,7 @@ class Asynchronous
*/ */
public static function awaitChildren() public static function awaitChildren()
{ {
$instance = self::getInstance(); self::getInstance()->_awaitChildren();
while (count($instance->children) > 0) {
pcntl_wait($status);
array_shift($instance->children);
}
} }
/** /**
@ -114,15 +110,7 @@ class Asynchronous
*/ */
public static function killChildren() public static function killChildren()
{ {
$instance = self::getInstance(); self::getInstance()->_killChildren();
/*
* Require the children to terminate
*/
foreach ($instance->children as $index => $pid)
if (!posix_kill($pid, SIGKILL))
posix_kill($pid, SIGTERM);
} }
/** /**
@ -130,11 +118,7 @@ class Asynchronous
*/ */
public static function removeShmBlock() public static function removeShmBlock()
{ {
$instance = self::getInstance(); self::getInstance()->_removeShmBlock();
if (is_resource($instance->shm)) {
shm_remove($instance->shm);
shm_detach($instance->shm);
}
} }
/** /**
@ -165,6 +149,52 @@ class Asynchronous
$this->attachShm(); $this->attachShm();
} }
/**
* @return $this
*/
private function _awaitChildren()
{
/*
* Wait for the children to terminate
*/
while (count($this->children) > 0) {
pcntl_wait($status);
array_shift($this->children);
}
return $this;
}
/**
* @return $this
*/
private function _killChildren()
{
/*
* Require the children to terminate
*/
foreach ($this->children as $index => $pid)
if (!posix_kill($pid, SIGKILL))
posix_kill($pid, SIGTERM);
return $this;
}
/**
* @return $this
*/
private function _removeShmBlock()
{
/*
* Detach from the shared memory block
*/
if (is_resource($this->shm)) {
shm_remove($this->shm);
shm_detach($this->shm);
}
return $this;
}
/** /**
* @return $this * @return $this
@ -231,23 +261,21 @@ class Asynchronous
if ($instance->isChild) if ($instance->isChild)
return; return;
self::awaitChildren(); $instance->_awaitChildren();
self::removeShmBlock(); $instance->_removeShmBlock();
}); });
/* /*
* The signal handler * The signal handler
*/ */
foreach([SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGTERM] as $signal) foreach ([SIGINT, SIGKILL, SIGTERM] as $signal)
pcntl_signal($signal, function($s) use (&$instance) { pcntl_signal($signal, function ($signal) use (&$instance) {
if ($instance->isChild) if ($instance->isChild)
return; return;
self::killChildren(); $instance->_killChildren();
self::awaitChildren(); $instance->_removeShmBlock();
self::removeShmBlock();
}); });
} }
} }