Better fork management

This commit is contained in:
Daniil Gentili 2018-03-04 14:17:12 +01:00
parent ef0059bf08
commit 6887837b31
3 changed files with 20 additions and 7 deletions

View File

@ -123,9 +123,13 @@ class API extends APIFactory
public function __set($name, $value)
{
if ($name === 'settings') {
if (Logger::is_fork()) {
if (Logger::is_fork() && !Logger::$processed_fork) {
\danog\MadelineProto\Logger::log('Detected fork');
$this->API->__wakeup();
$this->API->reset_session();
foreach ($this->API->datacenter->sockets as $datacenter) {
$datacenter->close_and_reopen();
}
Logger::$processed_fork = true;
}
return $this->API->__construct($value);

View File

@ -109,7 +109,6 @@ class APIFactory
public $namespace = '';
public $API;
public $lua = false;
public $pid;
public function __construct($namespace, $API)
{
@ -119,12 +118,13 @@ class APIFactory
public function __call($name, $arguments)
{
if (Logger::is_fork()) {
if (Logger::is_fork() && !Logger::$processed_fork) {
\danog\MadelineProto\Logger::log('Detected fork');
$this->API->reset_session();
foreach ($this->API->datacenter->sockets as $datacenter) {
$datacenter->close_and_reopen();
}
Logger::$processed_fork = true;
}
if ($this->API->setdem) {

View File

@ -32,6 +32,9 @@ class Logger
public static $bigint = true;
public static $colors = [];
public static $isatty = false;
public static $is_fork = false;
public static $can_getmypid = true;
public static $processed_fork = false;
private static $pid;
const ULTRA_VERBOSE = 5;
@ -70,14 +73,20 @@ class Logger
public static function is_fork()
{
if (self::$is_fork) {
return true;
}
if (!self::$can_getmypid) {
return false;
}
try {
if (self::$pid === null) {
self::$pid = getmypid();
}
return self::$pid !== getmypid();
return self::$is_fork = self::$pid !== getmypid();
} catch (\danog\MadelineProto\Exception $e) {
return false;
return self::$can_getmypid = false;
}
}