|
@@ -20,7 +20,7 @@ use Workerman\Connection\TcpConnection;
|
|
|
use Workerman\Connection\UdpConnection;
|
|
use Workerman\Connection\UdpConnection;
|
|
|
use Workerman\Lib\Timer;
|
|
use Workerman\Lib\Timer;
|
|
|
use Workerman\Events\Select;
|
|
use Workerman\Events\Select;
|
|
|
-use Exception;
|
|
|
|
|
|
|
+use \Exception;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Worker class
|
|
* Worker class
|
|
@@ -33,7 +33,7 @@ class Worker
|
|
|
*
|
|
*
|
|
|
* @var string
|
|
* @var string
|
|
|
*/
|
|
*/
|
|
|
- const VERSION = '3.5.23';
|
|
|
|
|
|
|
+ const VERSION = '3.5.28';
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Status starting.
|
|
* Status starting.
|
|
@@ -275,7 +275,7 @@ class Worker
|
|
|
/**
|
|
/**
|
|
|
* Global event loop.
|
|
* Global event loop.
|
|
|
*
|
|
*
|
|
|
- * @var Events\EventInterface
|
|
|
|
|
|
|
+ * @var EventInterface
|
|
|
*/
|
|
*/
|
|
|
public static $globalEvent = null;
|
|
public static $globalEvent = null;
|
|
|
|
|
|
|
@@ -301,6 +301,13 @@ class Worker
|
|
|
public static $eventLoopClass = '';
|
|
public static $eventLoopClass = '';
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * Process title
|
|
|
|
|
+ *
|
|
|
|
|
+ * @var string
|
|
|
|
|
+ */
|
|
|
|
|
+ public static $processTitle = 'WorkerMan';
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
* The PID of master process.
|
|
* The PID of master process.
|
|
|
*
|
|
*
|
|
|
* @var int
|
|
* @var int
|
|
@@ -434,7 +441,7 @@ class Worker
|
|
|
*
|
|
*
|
|
|
* @var string
|
|
* @var string
|
|
|
*/
|
|
*/
|
|
|
- protected static $_OS = OS_TYPE_LINUX;
|
|
|
|
|
|
|
+ protected static $_OS = \OS_TYPE_LINUX;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Processes for windows.
|
|
* Processes for windows.
|
|
@@ -461,7 +468,7 @@ class Worker
|
|
|
protected static $_availableEventLoops = array(
|
|
protected static $_availableEventLoops = array(
|
|
|
'libevent' => '\Workerman\Events\Libevent',
|
|
'libevent' => '\Workerman\Events\Libevent',
|
|
|
'event' => '\Workerman\Events\Event'
|
|
'event' => '\Workerman\Events\Event'
|
|
|
- // Temporarily removed swoole because it is not stable enough
|
|
|
|
|
|
|
+ // Temporarily removed swoole because it is not stable enough
|
|
|
//'swoole' => '\Workerman\Events\Swoole'
|
|
//'swoole' => '\Workerman\Events\Swoole'
|
|
|
);
|
|
);
|
|
|
|
|
|
|
@@ -483,21 +490,21 @@ class Worker
|
|
|
* @var array
|
|
* @var array
|
|
|
*/
|
|
*/
|
|
|
protected static $_errorType = array(
|
|
protected static $_errorType = array(
|
|
|
- E_ERROR => 'E_ERROR', // 1
|
|
|
|
|
- E_WARNING => 'E_WARNING', // 2
|
|
|
|
|
- E_PARSE => 'E_PARSE', // 4
|
|
|
|
|
- E_NOTICE => 'E_NOTICE', // 8
|
|
|
|
|
- E_CORE_ERROR => 'E_CORE_ERROR', // 16
|
|
|
|
|
- E_CORE_WARNING => 'E_CORE_WARNING', // 32
|
|
|
|
|
- E_COMPILE_ERROR => 'E_COMPILE_ERROR', // 64
|
|
|
|
|
- E_COMPILE_WARNING => 'E_COMPILE_WARNING', // 128
|
|
|
|
|
- E_USER_ERROR => 'E_USER_ERROR', // 256
|
|
|
|
|
- E_USER_WARNING => 'E_USER_WARNING', // 512
|
|
|
|
|
- E_USER_NOTICE => 'E_USER_NOTICE', // 1024
|
|
|
|
|
- E_STRICT => 'E_STRICT', // 2048
|
|
|
|
|
- E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR', // 4096
|
|
|
|
|
- E_DEPRECATED => 'E_DEPRECATED', // 8192
|
|
|
|
|
- E_USER_DEPRECATED => 'E_USER_DEPRECATED' // 16384
|
|
|
|
|
|
|
+ \E_ERROR => 'E_ERROR', // 1
|
|
|
|
|
+ \E_WARNING => 'E_WARNING', // 2
|
|
|
|
|
+ \E_PARSE => 'E_PARSE', // 4
|
|
|
|
|
+ \E_NOTICE => 'E_NOTICE', // 8
|
|
|
|
|
+ \E_CORE_ERROR => 'E_CORE_ERROR', // 16
|
|
|
|
|
+ \E_CORE_WARNING => 'E_CORE_WARNING', // 32
|
|
|
|
|
+ \E_COMPILE_ERROR => 'E_COMPILE_ERROR', // 64
|
|
|
|
|
+ \E_COMPILE_WARNING => 'E_COMPILE_WARNING', // 128
|
|
|
|
|
+ \E_USER_ERROR => 'E_USER_ERROR', // 256
|
|
|
|
|
+ \E_USER_WARNING => 'E_USER_WARNING', // 512
|
|
|
|
|
+ \E_USER_NOTICE => 'E_USER_NOTICE', // 1024
|
|
|
|
|
+ \E_STRICT => 'E_STRICT', // 2048
|
|
|
|
|
+ \E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR', // 4096
|
|
|
|
|
+ \E_DEPRECATED => 'E_DEPRECATED', // 8192
|
|
|
|
|
+ \E_USER_DEPRECATED => 'E_USER_DEPRECATED' // 16384
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -549,11 +556,11 @@ class Worker
|
|
|
protected static function checkSapiEnv()
|
|
protected static function checkSapiEnv()
|
|
|
{
|
|
{
|
|
|
// Only for cli.
|
|
// Only for cli.
|
|
|
- if (PHP_SAPI !== 'cli') {
|
|
|
|
|
- exit("only run in command line mode \n");
|
|
|
|
|
|
|
+ if (\PHP_SAPI !== 'cli') {
|
|
|
|
|
+ exit("Only run in command line mode \n");
|
|
|
}
|
|
}
|
|
|
- if (DIRECTORY_SEPARATOR === '\\') {
|
|
|
|
|
- self::$_OS = OS_TYPE_WINDOWS;
|
|
|
|
|
|
|
+ if (\DIRECTORY_SEPARATOR === '\\') {
|
|
|
|
|
+ self::$_OS = \OS_TYPE_WINDOWS;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -598,7 +605,7 @@ class Worker
|
|
|
static::$_statisticsFile = \sys_get_temp_dir() . "/$unique_prefix.status";
|
|
static::$_statisticsFile = \sys_get_temp_dir() . "/$unique_prefix.status";
|
|
|
|
|
|
|
|
// Process title.
|
|
// Process title.
|
|
|
- static::setProcessTitle('WorkerMan: master process start_file=' . static::$_startFile);
|
|
|
|
|
|
|
+ static::setProcessTitle(static::$processTitle . ': master process start_file=' . static::$_startFile);
|
|
|
|
|
|
|
|
// Init data for worker id.
|
|
// Init data for worker id.
|
|
|
static::initId();
|
|
static::initId();
|
|
@@ -615,7 +622,7 @@ class Worker
|
|
|
protected static function lock()
|
|
protected static function lock()
|
|
|
{
|
|
{
|
|
|
$fd = \fopen(static::$_startFile, 'r');
|
|
$fd = \fopen(static::$_startFile, 'r');
|
|
|
- if (!$fd || !flock($fd, LOCK_EX)) {
|
|
|
|
|
|
|
+ if ($fd && !flock($fd, LOCK_EX)) {
|
|
|
static::log('Workerman['.static::$_startFile.'] already running.');
|
|
static::log('Workerman['.static::$_startFile.'] already running.');
|
|
|
exit;
|
|
exit;
|
|
|
}
|
|
}
|
|
@@ -629,7 +636,7 @@ class Worker
|
|
|
protected static function unlock()
|
|
protected static function unlock()
|
|
|
{
|
|
{
|
|
|
$fd = \fopen(static::$_startFile, 'r');
|
|
$fd = \fopen(static::$_startFile, 'r');
|
|
|
- $fd && flock($fd, LOCK_UN);
|
|
|
|
|
|
|
+ $fd && flock($fd, \LOCK_UN);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -639,7 +646,7 @@ class Worker
|
|
|
*/
|
|
*/
|
|
|
protected static function initWorkers()
|
|
protected static function initWorkers()
|
|
|
{
|
|
{
|
|
|
- if (static::$_OS !== OS_TYPE_LINUX) {
|
|
|
|
|
|
|
+ if (static::$_OS !== \OS_TYPE_LINUX) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
foreach (static::$_workers as $worker) {
|
|
foreach (static::$_workers as $worker) {
|
|
@@ -679,6 +686,19 @@ class Worker
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * Reload all worker instances.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return void
|
|
|
|
|
+ */
|
|
|
|
|
+ public static function reloadAllWorkers()
|
|
|
|
|
+ {
|
|
|
|
|
+ static::init();
|
|
|
|
|
+ static::initWorkers();
|
|
|
|
|
+ static::displayUI();
|
|
|
|
|
+ static::$_status = static::STATUS_RELOADING;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
* Get all worker instances.
|
|
* Get all worker instances.
|
|
|
*
|
|
*
|
|
|
* @return array
|
|
* @return array
|
|
@@ -697,7 +717,7 @@ class Worker
|
|
|
{
|
|
{
|
|
|
return static::$globalEvent;
|
|
return static::$globalEvent;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Get main socket resource
|
|
* Get main socket resource
|
|
|
* @return resource
|
|
* @return resource
|
|
@@ -744,31 +764,31 @@ class Worker
|
|
|
if (\in_array('-q', $argv)) {
|
|
if (\in_array('-q', $argv)) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- if (static::$_OS !== OS_TYPE_LINUX) {
|
|
|
|
|
|
|
+ if (static::$_OS !== \OS_TYPE_LINUX) {
|
|
|
static::safeEcho("----------------------- WORKERMAN -----------------------------\r\n");
|
|
static::safeEcho("----------------------- WORKERMAN -----------------------------\r\n");
|
|
|
- static::safeEcho('Workerman version:'. static::VERSION. ' PHP version:'. PHP_VERSION. "\r\n");
|
|
|
|
|
|
|
+ static::safeEcho('Workerman version:'. static::VERSION. ' PHP version:'. \PHP_VERSION. "\r\n");
|
|
|
static::safeEcho("------------------------ WORKERS -------------------------------\r\n");
|
|
static::safeEcho("------------------------ WORKERS -------------------------------\r\n");
|
|
|
static::safeEcho("worker listen processes status\r\n");
|
|
static::safeEcho("worker listen processes status\r\n");
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//show version
|
|
//show version
|
|
|
- $line_version = 'Workerman version:' . static::VERSION . \str_pad('PHP version:', 22, ' ', STR_PAD_LEFT) . PHP_VERSION . PHP_EOL;
|
|
|
|
|
|
|
+ $line_version = 'Workerman version:' . static::VERSION . \str_pad('PHP version:', 22, ' ', \STR_PAD_LEFT) . \PHP_VERSION . \PHP_EOL;
|
|
|
!\defined('LINE_VERSIOIN_LENGTH') && \define('LINE_VERSIOIN_LENGTH', \strlen($line_version));
|
|
!\defined('LINE_VERSIOIN_LENGTH') && \define('LINE_VERSIOIN_LENGTH', \strlen($line_version));
|
|
|
$total_length = static::getSingleLineTotalLength();
|
|
$total_length = static::getSingleLineTotalLength();
|
|
|
- $line_one = '<n>' . \str_pad('<w> WORKERMAN </w>', $total_length + \strlen('<w></w>'), '-', STR_PAD_BOTH) . '</n>'. PHP_EOL;
|
|
|
|
|
- $line_two = \str_pad('<w> WORKERS </w>' , $total_length + \strlen('<w></w>'), '-', STR_PAD_BOTH) . PHP_EOL;
|
|
|
|
|
|
|
+ $line_one = '<n>' . \str_pad('<w> WORKERMAN </w>', $total_length + \strlen('<w></w>'), '-', \STR_PAD_BOTH) . '</n>'. \PHP_EOL;
|
|
|
|
|
+ $line_two = \str_pad('<w> WORKERS </w>' , $total_length + \strlen('<w></w>'), '-', \STR_PAD_BOTH) . \PHP_EOL;
|
|
|
static::safeEcho($line_one . $line_version . $line_two);
|
|
static::safeEcho($line_one . $line_version . $line_two);
|
|
|
|
|
|
|
|
//Show title
|
|
//Show title
|
|
|
$title = '';
|
|
$title = '';
|
|
|
foreach(static::getUiColumns() as $column_name => $prop){
|
|
foreach(static::getUiColumns() as $column_name => $prop){
|
|
|
$key = '_max' . \ucfirst(\strtolower($column_name)) . 'NameLength';
|
|
$key = '_max' . \ucfirst(\strtolower($column_name)) . 'NameLength';
|
|
|
- //just keep compatible with listen name
|
|
|
|
|
|
|
+ //just keep compatible with listen name
|
|
|
$column_name === 'socket' && $column_name = 'listen';
|
|
$column_name === 'socket' && $column_name = 'listen';
|
|
|
$title.= "<w>{$column_name}</w>" . \str_pad('', static::$$key + static::UI_SAFE_LENGTH - \strlen($column_name));
|
|
$title.= "<w>{$column_name}</w>" . \str_pad('', static::$$key + static::UI_SAFE_LENGTH - \strlen($column_name));
|
|
|
}
|
|
}
|
|
|
- $title && static::safeEcho($title . PHP_EOL);
|
|
|
|
|
|
|
+ $title && static::safeEcho($title . \PHP_EOL);
|
|
|
|
|
|
|
|
//Show content
|
|
//Show content
|
|
|
foreach (static::$_workers as $worker) {
|
|
foreach (static::$_workers as $worker) {
|
|
@@ -779,11 +799,11 @@ class Worker
|
|
|
$place_holder_length = !empty($matches) ? \strlen(\implode('', $matches[0])) : 0;
|
|
$place_holder_length = !empty($matches) ? \strlen(\implode('', $matches[0])) : 0;
|
|
|
$content .= \str_pad($worker->{$prop}, static::$$key + static::UI_SAFE_LENGTH + $place_holder_length);
|
|
$content .= \str_pad($worker->{$prop}, static::$$key + static::UI_SAFE_LENGTH + $place_holder_length);
|
|
|
}
|
|
}
|
|
|
- $content && static::safeEcho($content . PHP_EOL);
|
|
|
|
|
|
|
+ $content && static::safeEcho($content . \PHP_EOL);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//Show last line
|
|
//Show last line
|
|
|
- $line_last = \str_pad('', static::getSingleLineTotalLength(), '-') . PHP_EOL;
|
|
|
|
|
|
|
+ $line_last = \str_pad('', static::getSingleLineTotalLength(), '-') . \PHP_EOL;
|
|
|
!empty($content) && static::safeEcho($line_last);
|
|
!empty($content) && static::safeEcho($line_last);
|
|
|
|
|
|
|
|
if (static::$daemonize) {
|
|
if (static::$daemonize) {
|
|
@@ -841,7 +861,7 @@ class Worker
|
|
|
*/
|
|
*/
|
|
|
protected static function parseCommand()
|
|
protected static function parseCommand()
|
|
|
{
|
|
{
|
|
|
- if (static::$_OS !== OS_TYPE_LINUX) {
|
|
|
|
|
|
|
+ if (static::$_OS !== \OS_TYPE_LINUX) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
global $argv;
|
|
global $argv;
|
|
@@ -929,19 +949,19 @@ class Worker
|
|
|
// Waiting amoment.
|
|
// Waiting amoment.
|
|
|
\usleep(500000);
|
|
\usleep(500000);
|
|
|
// Display statisitcs data from a disk file.
|
|
// Display statisitcs data from a disk file.
|
|
|
- if(is_readable(static::$_statisticsFile)) {
|
|
|
|
|
- readfile(static::$_statisticsFile);
|
|
|
|
|
|
|
+ if(\is_readable(static::$_statisticsFile)) {
|
|
|
|
|
+ \readfile(static::$_statisticsFile);
|
|
|
}
|
|
}
|
|
|
exit(0);
|
|
exit(0);
|
|
|
case 'restart':
|
|
case 'restart':
|
|
|
case 'stop':
|
|
case 'stop':
|
|
|
if ($command2 === '-g') {
|
|
if ($command2 === '-g') {
|
|
|
static::$_gracefulStop = true;
|
|
static::$_gracefulStop = true;
|
|
|
- $sig = SIGTERM;
|
|
|
|
|
|
|
+ $sig = \SIGTERM;
|
|
|
static::log("Workerman[$start_file] is gracefully stopping ...");
|
|
static::log("Workerman[$start_file] is gracefully stopping ...");
|
|
|
} else {
|
|
} else {
|
|
|
static::$_gracefulStop = false;
|
|
static::$_gracefulStop = false;
|
|
|
- $sig = SIGINT;
|
|
|
|
|
|
|
+ $sig = \SIGINT;
|
|
|
static::log("Workerman[$start_file] is stopping ...");
|
|
static::log("Workerman[$start_file] is stopping ...");
|
|
|
}
|
|
}
|
|
|
// Send stop signal to master process.
|
|
// Send stop signal to master process.
|
|
@@ -975,9 +995,9 @@ class Worker
|
|
|
break;
|
|
break;
|
|
|
case 'reload':
|
|
case 'reload':
|
|
|
if($command2 === '-g'){
|
|
if($command2 === '-g'){
|
|
|
- $sig = SIGQUIT;
|
|
|
|
|
|
|
+ $sig = \SIGQUIT;
|
|
|
}else{
|
|
}else{
|
|
|
- $sig = SIGUSR1;
|
|
|
|
|
|
|
+ $sig = \SIGUSR1;
|
|
|
}
|
|
}
|
|
|
\posix_kill($master_pid, $sig);
|
|
\posix_kill($master_pid, $sig);
|
|
|
exit;
|
|
exit;
|
|
@@ -997,16 +1017,16 @@ class Worker
|
|
|
protected static function formatStatusData()
|
|
protected static function formatStatusData()
|
|
|
{
|
|
{
|
|
|
static $total_request_cache = array();
|
|
static $total_request_cache = array();
|
|
|
- if (!is_readable(static::$_statisticsFile)) {
|
|
|
|
|
|
|
+ if (!\is_readable(static::$_statisticsFile)) {
|
|
|
return '';
|
|
return '';
|
|
|
}
|
|
}
|
|
|
- $info = file(static::$_statisticsFile, FILE_IGNORE_NEW_LINES);
|
|
|
|
|
|
|
+ $info = \file(static::$_statisticsFile, \FILE_IGNORE_NEW_LINES);
|
|
|
if (!$info) {
|
|
if (!$info) {
|
|
|
return '';
|
|
return '';
|
|
|
}
|
|
}
|
|
|
$status_str = '';
|
|
$status_str = '';
|
|
|
$current_total_request = array();
|
|
$current_total_request = array();
|
|
|
- $worker_info = \json_decode($info[0], true);
|
|
|
|
|
|
|
+ $worker_info = \unserialize($info[0]);
|
|
|
\ksort($worker_info, SORT_NUMERIC);
|
|
\ksort($worker_info, SORT_NUMERIC);
|
|
|
unset($info[0]);
|
|
unset($info[0]);
|
|
|
$data_waiting_sort = array();
|
|
$data_waiting_sort = array();
|
|
@@ -1031,7 +1051,7 @@ class Worker
|
|
|
$pid = $pid_math[0];
|
|
$pid = $pid_math[0];
|
|
|
$data_waiting_sort[$pid] = $value;
|
|
$data_waiting_sort[$pid] = $value;
|
|
|
if(\preg_match('/^\S+?\s+?(\S+?)\s+?(\S+?)\s+?(\S+?)\s+?(\S+?)\s+?(\S+?)\s+?(\S+?)\s+?(\S+?)\s+?/', $value, $match)) {
|
|
if(\preg_match('/^\S+?\s+?(\S+?)\s+?(\S+?)\s+?(\S+?)\s+?(\S+?)\s+?(\S+?)\s+?(\S+?)\s+?(\S+?)\s+?/', $value, $match)) {
|
|
|
- $total_memory += \intval(str_ireplace('M','',$match[1]));
|
|
|
|
|
|
|
+ $total_memory += \intval(\str_ireplace('M','',$match[1]));
|
|
|
$maxLen1 = \max($maxLen1,\strlen($match[2]));
|
|
$maxLen1 = \max($maxLen1,\strlen($match[2]));
|
|
|
$maxLen2 = \max($maxLen2,\strlen($match[3]));
|
|
$maxLen2 = \max($maxLen2,\strlen($match[3]));
|
|
|
$total_connections += \intval($match[4]);
|
|
$total_connections += \intval($match[4]);
|
|
@@ -1079,23 +1099,24 @@ class Worker
|
|
|
*/
|
|
*/
|
|
|
protected static function installSignal()
|
|
protected static function installSignal()
|
|
|
{
|
|
{
|
|
|
- if (static::$_OS !== OS_TYPE_LINUX) {
|
|
|
|
|
|
|
+ if (static::$_OS !== \OS_TYPE_LINUX) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
+ $signalHandler = '\Workerman\Worker::signalHandler';
|
|
|
// stop
|
|
// stop
|
|
|
- \pcntl_signal(SIGINT, array('\Workerman\Worker', 'signalHandler'), false);
|
|
|
|
|
|
|
+ \pcntl_signal(\SIGINT, $signalHandler, false);
|
|
|
// graceful stop
|
|
// graceful stop
|
|
|
- \pcntl_signal(SIGTERM, array('\Workerman\Worker', 'signalHandler'), false);
|
|
|
|
|
|
|
+ \pcntl_signal(\SIGTERM, $signalHandler, false);
|
|
|
// reload
|
|
// reload
|
|
|
- \pcntl_signal(SIGUSR1, array('\Workerman\Worker', 'signalHandler'), false);
|
|
|
|
|
|
|
+ \pcntl_signal(\SIGUSR1, $signalHandler, false);
|
|
|
// graceful reload
|
|
// graceful reload
|
|
|
- \pcntl_signal(SIGQUIT, array('\Workerman\Worker', 'signalHandler'), false);
|
|
|
|
|
|
|
+ \pcntl_signal(\SIGQUIT, $signalHandler, false);
|
|
|
// status
|
|
// status
|
|
|
- \pcntl_signal(SIGUSR2, array('\Workerman\Worker', 'signalHandler'), false);
|
|
|
|
|
|
|
+ \pcntl_signal(\SIGUSR2, $signalHandler, false);
|
|
|
// connection status
|
|
// connection status
|
|
|
- \pcntl_signal(SIGIO, array('\Workerman\Worker', 'signalHandler'), false);
|
|
|
|
|
|
|
+ \pcntl_signal(\SIGIO, $signalHandler, false);
|
|
|
// ignore
|
|
// ignore
|
|
|
- \pcntl_signal(SIGPIPE, SIG_IGN, false);
|
|
|
|
|
|
|
+ \pcntl_signal(\SIGPIPE, \SIG_IGN, false);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -1105,33 +1126,34 @@ class Worker
|
|
|
*/
|
|
*/
|
|
|
protected static function reinstallSignal()
|
|
protected static function reinstallSignal()
|
|
|
{
|
|
{
|
|
|
- if (static::$_OS !== OS_TYPE_LINUX) {
|
|
|
|
|
|
|
+ if (static::$_OS !== \OS_TYPE_LINUX) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
+ $signalHandler = '\Workerman\Worker::signalHandler';
|
|
|
// uninstall stop signal handler
|
|
// uninstall stop signal handler
|
|
|
- \pcntl_signal(SIGINT, SIG_IGN, false);
|
|
|
|
|
|
|
+ \pcntl_signal(\SIGINT, \SIG_IGN, false);
|
|
|
// uninstall graceful stop signal handler
|
|
// uninstall graceful stop signal handler
|
|
|
- \pcntl_signal(SIGTERM, SIG_IGN, false);
|
|
|
|
|
|
|
+ \pcntl_signal(\SIGTERM, \SIG_IGN, false);
|
|
|
// uninstall reload signal handler
|
|
// uninstall reload signal handler
|
|
|
- \pcntl_signal(SIGUSR1, SIG_IGN, false);
|
|
|
|
|
|
|
+ \pcntl_signal(\SIGUSR1, \SIG_IGN, false);
|
|
|
// uninstall graceful reload signal handler
|
|
// uninstall graceful reload signal handler
|
|
|
- \pcntl_signal(SIGQUIT, SIG_IGN, false);
|
|
|
|
|
|
|
+ \pcntl_signal(\SIGQUIT, \SIG_IGN, false);
|
|
|
// uninstall status signal handler
|
|
// uninstall status signal handler
|
|
|
- \pcntl_signal(SIGUSR2, SIG_IGN, false);
|
|
|
|
|
|
|
+ \pcntl_signal(\SIGUSR2, \SIG_IGN, false);
|
|
|
// uninstall connections status signal handler
|
|
// uninstall connections status signal handler
|
|
|
- \pcntl_signal(SIGIO, SIG_IGN, false);
|
|
|
|
|
|
|
+ \pcntl_signal(\SIGIO, \SIG_IGN, false);
|
|
|
// reinstall stop signal handler
|
|
// reinstall stop signal handler
|
|
|
- static::$globalEvent->add(SIGINT, EventInterface::EV_SIGNAL, array('\Workerman\Worker', 'signalHandler'));
|
|
|
|
|
|
|
+ static::$globalEvent->add(\SIGINT, EventInterface::EV_SIGNAL, $signalHandler);
|
|
|
// reinstall graceful stop signal handler
|
|
// reinstall graceful stop signal handler
|
|
|
- static::$globalEvent->add(SIGTERM, EventInterface::EV_SIGNAL, array('\Workerman\Worker', 'signalHandler'));
|
|
|
|
|
|
|
+ static::$globalEvent->add(\SIGTERM, EventInterface::EV_SIGNAL, $signalHandler);
|
|
|
// reinstall reload signal handler
|
|
// reinstall reload signal handler
|
|
|
- static::$globalEvent->add(SIGUSR1, EventInterface::EV_SIGNAL, array('\Workerman\Worker', 'signalHandler'));
|
|
|
|
|
|
|
+ static::$globalEvent->add(\SIGUSR1, EventInterface::EV_SIGNAL, $signalHandler);
|
|
|
// reinstall graceful reload signal handler
|
|
// reinstall graceful reload signal handler
|
|
|
- static::$globalEvent->add(SIGQUIT, EventInterface::EV_SIGNAL, array('\Workerman\Worker', 'signalHandler'));
|
|
|
|
|
|
|
+ static::$globalEvent->add(\SIGQUIT, EventInterface::EV_SIGNAL, $signalHandler);
|
|
|
// reinstall status signal handler
|
|
// reinstall status signal handler
|
|
|
- static::$globalEvent->add(SIGUSR2, EventInterface::EV_SIGNAL, array('\Workerman\Worker', 'signalHandler'));
|
|
|
|
|
|
|
+ static::$globalEvent->add(\SIGUSR2, EventInterface::EV_SIGNAL, $signalHandler);
|
|
|
// reinstall connection status signal handler
|
|
// reinstall connection status signal handler
|
|
|
- static::$globalEvent->add(SIGIO, EventInterface::EV_SIGNAL, array('\Workerman\Worker', 'signalHandler'));
|
|
|
|
|
|
|
+ static::$globalEvent->add(\SIGIO, EventInterface::EV_SIGNAL, $signalHandler);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -1143,19 +1165,19 @@ class Worker
|
|
|
{
|
|
{
|
|
|
switch ($signal) {
|
|
switch ($signal) {
|
|
|
// Stop.
|
|
// Stop.
|
|
|
- case SIGINT:
|
|
|
|
|
|
|
+ case \SIGINT:
|
|
|
static::$_gracefulStop = false;
|
|
static::$_gracefulStop = false;
|
|
|
static::stopAll();
|
|
static::stopAll();
|
|
|
break;
|
|
break;
|
|
|
// Graceful stop.
|
|
// Graceful stop.
|
|
|
- case SIGTERM:
|
|
|
|
|
|
|
+ case \SIGTERM:
|
|
|
static::$_gracefulStop = true;
|
|
static::$_gracefulStop = true;
|
|
|
static::stopAll();
|
|
static::stopAll();
|
|
|
break;
|
|
break;
|
|
|
// Reload.
|
|
// Reload.
|
|
|
- case SIGQUIT:
|
|
|
|
|
- case SIGUSR1:
|
|
|
|
|
- if($signal === SIGQUIT){
|
|
|
|
|
|
|
+ case \SIGQUIT:
|
|
|
|
|
+ case \SIGUSR1:
|
|
|
|
|
+ if($signal === \SIGQUIT){
|
|
|
static::$_gracefulStop = true;
|
|
static::$_gracefulStop = true;
|
|
|
}else{
|
|
}else{
|
|
|
static::$_gracefulStop = false;
|
|
static::$_gracefulStop = false;
|
|
@@ -1164,11 +1186,11 @@ class Worker
|
|
|
static::reload();
|
|
static::reload();
|
|
|
break;
|
|
break;
|
|
|
// Show status.
|
|
// Show status.
|
|
|
- case SIGUSR2:
|
|
|
|
|
|
|
+ case \SIGUSR2:
|
|
|
static::writeStatisticsToStatusFile();
|
|
static::writeStatisticsToStatusFile();
|
|
|
break;
|
|
break;
|
|
|
// Show connection status.
|
|
// Show connection status.
|
|
|
- case SIGIO:
|
|
|
|
|
|
|
+ case \SIGIO:
|
|
|
static::writeConnectionsStatisticsToStatusFile();
|
|
static::writeConnectionsStatisticsToStatusFile();
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
@@ -1181,23 +1203,23 @@ class Worker
|
|
|
*/
|
|
*/
|
|
|
protected static function daemonize()
|
|
protected static function daemonize()
|
|
|
{
|
|
{
|
|
|
- if (!static::$daemonize || static::$_OS !== OS_TYPE_LINUX) {
|
|
|
|
|
|
|
+ if (!static::$daemonize || static::$_OS !== \OS_TYPE_LINUX) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
\umask(0);
|
|
\umask(0);
|
|
|
$pid = \pcntl_fork();
|
|
$pid = \pcntl_fork();
|
|
|
if (-1 === $pid) {
|
|
if (-1 === $pid) {
|
|
|
- throw new Exception('fork fail');
|
|
|
|
|
|
|
+ throw new Exception('Fork fail');
|
|
|
} elseif ($pid > 0) {
|
|
} elseif ($pid > 0) {
|
|
|
exit(0);
|
|
exit(0);
|
|
|
}
|
|
}
|
|
|
if (-1 === \posix_setsid()) {
|
|
if (-1 === \posix_setsid()) {
|
|
|
- throw new Exception("setsid fail");
|
|
|
|
|
|
|
+ throw new Exception("Setsid fail");
|
|
|
}
|
|
}
|
|
|
// Fork again avoid SVR4 system regain the control of terminal.
|
|
// Fork again avoid SVR4 system regain the control of terminal.
|
|
|
$pid = \pcntl_fork();
|
|
$pid = \pcntl_fork();
|
|
|
if (-1 === $pid) {
|
|
if (-1 === $pid) {
|
|
|
- throw new Exception("fork fail");
|
|
|
|
|
|
|
+ throw new Exception("Fork fail");
|
|
|
} elseif (0 !== $pid) {
|
|
} elseif (0 !== $pid) {
|
|
|
exit(0);
|
|
exit(0);
|
|
|
}
|
|
}
|
|
@@ -1210,7 +1232,7 @@ class Worker
|
|
|
*/
|
|
*/
|
|
|
public static function resetStd()
|
|
public static function resetStd()
|
|
|
{
|
|
{
|
|
|
- if (!static::$daemonize || static::$_OS !== OS_TYPE_LINUX) {
|
|
|
|
|
|
|
+ if (!static::$daemonize || static::$_OS !== \OS_TYPE_LINUX) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
global $STDOUT, $STDERR;
|
|
global $STDOUT, $STDERR;
|
|
@@ -1220,17 +1242,18 @@ class Worker
|
|
|
\set_error_handler(function(){});
|
|
\set_error_handler(function(){});
|
|
|
\fclose($STDOUT);
|
|
\fclose($STDOUT);
|
|
|
\fclose($STDERR);
|
|
\fclose($STDERR);
|
|
|
- \fclose(STDOUT);
|
|
|
|
|
- \fclose(STDERR);
|
|
|
|
|
|
|
+ \fclose(\STDOUT);
|
|
|
|
|
+ \fclose(\STDERR);
|
|
|
$STDOUT = \fopen(static::$stdoutFile, "a");
|
|
$STDOUT = \fopen(static::$stdoutFile, "a");
|
|
|
$STDERR = \fopen(static::$stdoutFile, "a");
|
|
$STDERR = \fopen(static::$stdoutFile, "a");
|
|
|
// change output stream
|
|
// change output stream
|
|
|
static::$_outputStream = null;
|
|
static::$_outputStream = null;
|
|
|
static::outputStream($STDOUT);
|
|
static::outputStream($STDOUT);
|
|
|
\restore_error_handler();
|
|
\restore_error_handler();
|
|
|
- } else {
|
|
|
|
|
- throw new Exception('can not open stdoutFile ' . static::$stdoutFile);
|
|
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ throw new Exception('Can not open stdoutFile ' . static::$stdoutFile);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -1240,7 +1263,7 @@ class Worker
|
|
|
*/
|
|
*/
|
|
|
protected static function saveMasterPid()
|
|
protected static function saveMasterPid()
|
|
|
{
|
|
{
|
|
|
- if (static::$_OS !== OS_TYPE_LINUX) {
|
|
|
|
|
|
|
+ if (static::$_OS !== \OS_TYPE_LINUX) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -1264,7 +1287,7 @@ class Worker
|
|
|
if (!\class_exists('\Swoole\Event', false)) {
|
|
if (!\class_exists('\Swoole\Event', false)) {
|
|
|
unset(static::$_availableEventLoops['swoole']);
|
|
unset(static::$_availableEventLoops['swoole']);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
$loop_name = '';
|
|
$loop_name = '';
|
|
|
foreach (static::$_availableEventLoops as $name=>$class) {
|
|
foreach (static::$_availableEventLoops as $name=>$class) {
|
|
|
if (\extension_loaded($name)) {
|
|
if (\extension_loaded($name)) {
|
|
@@ -1318,7 +1341,7 @@ class Worker
|
|
|
*/
|
|
*/
|
|
|
protected static function forkWorkers()
|
|
protected static function forkWorkers()
|
|
|
{
|
|
{
|
|
|
- if (static::$_OS === OS_TYPE_LINUX) {
|
|
|
|
|
|
|
+ if (static::$_OS === \OS_TYPE_LINUX) {
|
|
|
static::forkWorkersForLinux();
|
|
static::forkWorkersForLinux();
|
|
|
} else {
|
|
} else {
|
|
|
static::forkWorkersForWindows();
|
|
static::forkWorkersForWindows();
|
|
@@ -1512,7 +1535,7 @@ class Worker
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
Timer::delAll();
|
|
Timer::delAll();
|
|
|
- static::setProcessTitle('WorkerMan: worker process ' . $worker->name . ' ' . $worker->getSocketName());
|
|
|
|
|
|
|
+ static::setProcessTitle(self::$processTitle . ': worker process ' . $worker->name . ' ' . $worker->getSocketName());
|
|
|
$worker->setUserAndGroup();
|
|
$worker->setUserAndGroup();
|
|
|
$worker->id = $id;
|
|
$worker->id = $id;
|
|
|
$worker->run();
|
|
$worker->run();
|
|
@@ -1597,7 +1620,7 @@ class Worker
|
|
|
*/
|
|
*/
|
|
|
protected static function monitorWorkers()
|
|
protected static function monitorWorkers()
|
|
|
{
|
|
{
|
|
|
- if (static::$_OS === OS_TYPE_LINUX) {
|
|
|
|
|
|
|
+ if (static::$_OS === \OS_TYPE_LINUX) {
|
|
|
static::monitorWorkersForLinux();
|
|
static::monitorWorkersForLinux();
|
|
|
} else {
|
|
} else {
|
|
|
static::monitorWorkersForWindows();
|
|
static::monitorWorkersForWindows();
|
|
@@ -1617,7 +1640,7 @@ class Worker
|
|
|
\pcntl_signal_dispatch();
|
|
\pcntl_signal_dispatch();
|
|
|
// Suspends execution of the current process until a child has exited, or until a signal is delivered
|
|
// Suspends execution of the current process until a child has exited, or until a signal is delivered
|
|
|
$status = 0;
|
|
$status = 0;
|
|
|
- $pid = \pcntl_wait($status, WUNTRACED);
|
|
|
|
|
|
|
+ $pid = \pcntl_wait($status, \WUNTRACED);
|
|
|
// Calls signal handlers for pending signals again.
|
|
// Calls signal handlers for pending signals again.
|
|
|
\pcntl_signal_dispatch();
|
|
\pcntl_signal_dispatch();
|
|
|
// If a child has already exited.
|
|
// If a child has already exited.
|
|
@@ -1635,7 +1658,7 @@ class Worker
|
|
|
if (!isset(static::$_globalStatistics['worker_exit_info'][$worker_id][$status])) {
|
|
if (!isset(static::$_globalStatistics['worker_exit_info'][$worker_id][$status])) {
|
|
|
static::$_globalStatistics['worker_exit_info'][$worker_id][$status] = 0;
|
|
static::$_globalStatistics['worker_exit_info'][$worker_id][$status] = 0;
|
|
|
}
|
|
}
|
|
|
- static::$_globalStatistics['worker_exit_info'][$worker_id][$status]++;
|
|
|
|
|
|
|
+ ++static::$_globalStatistics['worker_exit_info'][$worker_id][$status];
|
|
|
|
|
|
|
|
// Clear process data.
|
|
// Clear process data.
|
|
|
unset(static::$_pidMap[$worker_id][$pid]);
|
|
unset(static::$_pidMap[$worker_id][$pid]);
|
|
@@ -1728,9 +1751,9 @@ class Worker
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (static::$_gracefulStop) {
|
|
if (static::$_gracefulStop) {
|
|
|
- $sig = SIGQUIT;
|
|
|
|
|
|
|
+ $sig = \SIGQUIT;
|
|
|
} else {
|
|
} else {
|
|
|
- $sig = SIGUSR1;
|
|
|
|
|
|
|
+ $sig = \SIGUSR1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Send reload signal to all child processes.
|
|
// Send reload signal to all child processes.
|
|
@@ -1765,7 +1788,7 @@ class Worker
|
|
|
\posix_kill($one_worker_pid, $sig);
|
|
\posix_kill($one_worker_pid, $sig);
|
|
|
// If the process does not exit after static::KILL_WORKER_TIMER_TIME seconds try to kill it.
|
|
// If the process does not exit after static::KILL_WORKER_TIMER_TIME seconds try to kill it.
|
|
|
if(!static::$_gracefulStop){
|
|
if(!static::$_gracefulStop){
|
|
|
- Timer::add(static::KILL_WORKER_TIMER_TIME, '\posix_kill', array($one_worker_pid, SIGKILL), false);
|
|
|
|
|
|
|
+ Timer::add(static::KILL_WORKER_TIMER_TIME, '\posix_kill', array($one_worker_pid, \SIGKILL), false);
|
|
|
}
|
|
}
|
|
|
} // For child processes.
|
|
} // For child processes.
|
|
|
else {
|
|
else {
|
|
@@ -1804,14 +1827,14 @@ class Worker
|
|
|
$worker_pid_array = static::getAllWorkerPids();
|
|
$worker_pid_array = static::getAllWorkerPids();
|
|
|
// Send stop signal to all child processes.
|
|
// Send stop signal to all child processes.
|
|
|
if (static::$_gracefulStop) {
|
|
if (static::$_gracefulStop) {
|
|
|
- $sig = SIGTERM;
|
|
|
|
|
|
|
+ $sig = \SIGTERM;
|
|
|
} else {
|
|
} else {
|
|
|
- $sig = SIGINT;
|
|
|
|
|
|
|
+ $sig = \SIGINT;
|
|
|
}
|
|
}
|
|
|
foreach ($worker_pid_array as $worker_pid) {
|
|
foreach ($worker_pid_array as $worker_pid) {
|
|
|
\posix_kill($worker_pid, $sig);
|
|
\posix_kill($worker_pid, $sig);
|
|
|
if(!static::$_gracefulStop){
|
|
if(!static::$_gracefulStop){
|
|
|
- Timer::add(static::KILL_WORKER_TIMER_TIME, '\posix_kill', array($worker_pid, SIGKILL), false);
|
|
|
|
|
|
|
+ Timer::add(static::KILL_WORKER_TIMER_TIME, '\posix_kill', array($worker_pid, \SIGKILL), false);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
Timer::add(1, "\\Workerman\\Worker::checkIfChildRunning");
|
|
Timer::add(1, "\\Workerman\\Worker::checkIfChildRunning");
|
|
@@ -1890,49 +1913,49 @@ class Worker
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- \file_put_contents(static::$_statisticsFile, \json_encode($all_worker_info)."\n", FILE_APPEND);
|
|
|
|
|
- $loadavg = \function_exists('sys_getloadavg') ? \array_map('round', sys_getloadavg(), array(2)) : array('-', '-', '-');
|
|
|
|
|
|
|
+ \file_put_contents(static::$_statisticsFile, \serialize($all_worker_info)."\n", \FILE_APPEND);
|
|
|
|
|
+ $loadavg = \function_exists('sys_getloadavg') ? \array_map('round', \sys_getloadavg(), array(2)) : array('-', '-', '-');
|
|
|
\file_put_contents(static::$_statisticsFile,
|
|
\file_put_contents(static::$_statisticsFile,
|
|
|
- "----------------------------------------------GLOBAL STATUS----------------------------------------------------\n", FILE_APPEND);
|
|
|
|
|
|
|
+ "----------------------------------------------GLOBAL STATUS----------------------------------------------------\n", \FILE_APPEND);
|
|
|
\file_put_contents(static::$_statisticsFile,
|
|
\file_put_contents(static::$_statisticsFile,
|
|
|
- 'Workerman version:' . static::VERSION . " PHP version:" . PHP_VERSION . "\n", FILE_APPEND);
|
|
|
|
|
|
|
+ 'Workerman version:' . static::VERSION . " PHP version:" . \PHP_VERSION . "\n", \FILE_APPEND);
|
|
|
\file_put_contents(static::$_statisticsFile, 'start time:' . \date('Y-m-d H:i:s',
|
|
\file_put_contents(static::$_statisticsFile, 'start time:' . \date('Y-m-d H:i:s',
|
|
|
static::$_globalStatistics['start_timestamp']) . ' run ' . \floor((\time() - static::$_globalStatistics['start_timestamp']) / (24 * 60 * 60)) . ' days ' . \floor(((\time() - static::$_globalStatistics['start_timestamp']) % (24 * 60 * 60)) / (60 * 60)) . " hours \n",
|
|
static::$_globalStatistics['start_timestamp']) . ' run ' . \floor((\time() - static::$_globalStatistics['start_timestamp']) / (24 * 60 * 60)) . ' days ' . \floor(((\time() - static::$_globalStatistics['start_timestamp']) % (24 * 60 * 60)) / (60 * 60)) . " hours \n",
|
|
|
FILE_APPEND);
|
|
FILE_APPEND);
|
|
|
$load_str = 'load average: ' . \implode(", ", $loadavg);
|
|
$load_str = 'load average: ' . \implode(", ", $loadavg);
|
|
|
\file_put_contents(static::$_statisticsFile,
|
|
\file_put_contents(static::$_statisticsFile,
|
|
|
- \str_pad($load_str, 33) . 'event-loop:' . static::getEventLoopName() . "\n", FILE_APPEND);
|
|
|
|
|
|
|
+ \str_pad($load_str, 33) . 'event-loop:' . static::getEventLoopName() . "\n", \FILE_APPEND);
|
|
|
\file_put_contents(static::$_statisticsFile,
|
|
\file_put_contents(static::$_statisticsFile,
|
|
|
\count(static::$_pidMap) . ' workers ' . \count(static::getAllWorkerPids()) . " processes\n",
|
|
\count(static::$_pidMap) . ' workers ' . \count(static::getAllWorkerPids()) . " processes\n",
|
|
|
- FILE_APPEND);
|
|
|
|
|
|
|
+ \FILE_APPEND);
|
|
|
\file_put_contents(static::$_statisticsFile,
|
|
\file_put_contents(static::$_statisticsFile,
|
|
|
- \str_pad('worker_name', static::$_maxWorkerNameLength) . " exit_status exit_count\n", FILE_APPEND);
|
|
|
|
|
|
|
+ \str_pad('worker_name', static::$_maxWorkerNameLength) . " exit_status exit_count\n", \FILE_APPEND);
|
|
|
foreach (static::$_pidMap as $worker_id => $worker_pid_array) {
|
|
foreach (static::$_pidMap as $worker_id => $worker_pid_array) {
|
|
|
$worker = static::$_workers[$worker_id];
|
|
$worker = static::$_workers[$worker_id];
|
|
|
if (isset(static::$_globalStatistics['worker_exit_info'][$worker_id])) {
|
|
if (isset(static::$_globalStatistics['worker_exit_info'][$worker_id])) {
|
|
|
foreach (static::$_globalStatistics['worker_exit_info'][$worker_id] as $worker_exit_status => $worker_exit_count) {
|
|
foreach (static::$_globalStatistics['worker_exit_info'][$worker_id] as $worker_exit_status => $worker_exit_count) {
|
|
|
\file_put_contents(static::$_statisticsFile,
|
|
\file_put_contents(static::$_statisticsFile,
|
|
|
\str_pad($worker->name, static::$_maxWorkerNameLength) . " " . \str_pad($worker_exit_status,
|
|
\str_pad($worker->name, static::$_maxWorkerNameLength) . " " . \str_pad($worker_exit_status,
|
|
|
- 16) . " $worker_exit_count\n", FILE_APPEND);
|
|
|
|
|
|
|
+ 16) . " $worker_exit_count\n", \FILE_APPEND);
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
\file_put_contents(static::$_statisticsFile,
|
|
\file_put_contents(static::$_statisticsFile,
|
|
|
\str_pad($worker->name, static::$_maxWorkerNameLength) . " " . \str_pad(0, 16) . " 0\n",
|
|
\str_pad($worker->name, static::$_maxWorkerNameLength) . " " . \str_pad(0, 16) . " 0\n",
|
|
|
- FILE_APPEND);
|
|
|
|
|
|
|
+ \FILE_APPEND);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
\file_put_contents(static::$_statisticsFile,
|
|
\file_put_contents(static::$_statisticsFile,
|
|
|
"----------------------------------------------PROCESS STATUS---------------------------------------------------\n",
|
|
"----------------------------------------------PROCESS STATUS---------------------------------------------------\n",
|
|
|
- FILE_APPEND);
|
|
|
|
|
|
|
+ \FILE_APPEND);
|
|
|
\file_put_contents(static::$_statisticsFile,
|
|
\file_put_contents(static::$_statisticsFile,
|
|
|
"pid\tmemory " . \str_pad('listening', static::$_maxSocketNameLength) . " " . \str_pad('worker_name',
|
|
"pid\tmemory " . \str_pad('listening', static::$_maxSocketNameLength) . " " . \str_pad('worker_name',
|
|
|
static::$_maxWorkerNameLength) . " connections " . \str_pad('send_fail', 9) . " "
|
|
static::$_maxWorkerNameLength) . " connections " . \str_pad('send_fail', 9) . " "
|
|
|
- . \str_pad('timers', 8) . \str_pad('total_request', 13) ." qps status\n", FILE_APPEND);
|
|
|
|
|
|
|
+ . \str_pad('timers', 8) . \str_pad('total_request', 13) ." qps status\n", \FILE_APPEND);
|
|
|
|
|
|
|
|
\chmod(static::$_statisticsFile, 0722);
|
|
\chmod(static::$_statisticsFile, 0722);
|
|
|
|
|
|
|
|
foreach (static::getAllWorkerPids() as $worker_pid) {
|
|
foreach (static::getAllWorkerPids() as $worker_pid) {
|
|
|
- \posix_kill($worker_pid, SIGUSR2);
|
|
|
|
|
|
|
+ \posix_kill($worker_pid, \SIGUSR2);
|
|
|
}
|
|
}
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -1943,13 +1966,13 @@ class Worker
|
|
|
$worker = current(static::$_workers);
|
|
$worker = current(static::$_workers);
|
|
|
$worker_status_str = \posix_getpid() . "\t" . \str_pad(round(memory_get_usage(true) / (1024 * 1024), 2) . "M", 7)
|
|
$worker_status_str = \posix_getpid() . "\t" . \str_pad(round(memory_get_usage(true) / (1024 * 1024), 2) . "M", 7)
|
|
|
. " " . \str_pad($worker->getSocketName(), static::$_maxSocketNameLength) . " "
|
|
. " " . \str_pad($worker->getSocketName(), static::$_maxSocketNameLength) . " "
|
|
|
- . str_pad(($worker->name === $worker->getSocketName() ? 'none' : $worker->name), static::$_maxWorkerNameLength)
|
|
|
|
|
|
|
+ . \str_pad(($worker->name === $worker->getSocketName() ? 'none' : $worker->name), static::$_maxWorkerNameLength)
|
|
|
. " ";
|
|
. " ";
|
|
|
$worker_status_str .= \str_pad(ConnectionInterface::$statistics['connection_count'], 11)
|
|
$worker_status_str .= \str_pad(ConnectionInterface::$statistics['connection_count'], 11)
|
|
|
. " " . \str_pad(ConnectionInterface::$statistics['send_fail'], 9)
|
|
. " " . \str_pad(ConnectionInterface::$statistics['send_fail'], 9)
|
|
|
. " " . \str_pad(static::$globalEvent->getTimerCount(), 7)
|
|
. " " . \str_pad(static::$globalEvent->getTimerCount(), 7)
|
|
|
. " " . \str_pad(ConnectionInterface::$statistics['total_request'], 13) . "\n";
|
|
. " " . \str_pad(ConnectionInterface::$statistics['total_request'], 13) . "\n";
|
|
|
- \file_put_contents(static::$_statisticsFile, $worker_status_str, FILE_APPEND);
|
|
|
|
|
|
|
+ \file_put_contents(static::$_statisticsFile, $worker_status_str, \FILE_APPEND);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -1961,11 +1984,11 @@ class Worker
|
|
|
{
|
|
{
|
|
|
// For master process.
|
|
// For master process.
|
|
|
if (static::$_masterPid === \posix_getpid()) {
|
|
if (static::$_masterPid === \posix_getpid()) {
|
|
|
- \file_put_contents(static::$_statisticsFile, "--------------------------------------------------------------------- WORKERMAN CONNECTION STATUS --------------------------------------------------------------------------------\n", FILE_APPEND);
|
|
|
|
|
- \file_put_contents(static::$_statisticsFile, "PID Worker CID Trans Protocol ipv4 ipv6 Recv-Q Send-Q Bytes-R Bytes-W Status Local Address Foreign Address\n", FILE_APPEND);
|
|
|
|
|
|
|
+ \file_put_contents(static::$_statisticsFile, "--------------------------------------------------------------------- WORKERMAN CONNECTION STATUS --------------------------------------------------------------------------------\n", \FILE_APPEND);
|
|
|
|
|
+ \file_put_contents(static::$_statisticsFile, "PID Worker CID Trans Protocol ipv4 ipv6 Recv-Q Send-Q Bytes-R Bytes-W Status Local Address Foreign Address\n", \FILE_APPEND);
|
|
|
\chmod(static::$_statisticsFile, 0722);
|
|
\chmod(static::$_statisticsFile, 0722);
|
|
|
foreach (static::getAllWorkerPids() as $worker_pid) {
|
|
foreach (static::getAllWorkerPids() as $worker_pid) {
|
|
|
- \posix_kill($worker_pid, SIGIO);
|
|
|
|
|
|
|
+ \posix_kill($worker_pid, \SIGIO);
|
|
|
}
|
|
}
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -2026,7 +2049,7 @@ class Worker
|
|
|
. \str_pad($state, 14) . ' ' . \str_pad($local_address, 22) . ' ' . \str_pad($remote_address, 22) ."\n";
|
|
. \str_pad($state, 14) . ' ' . \str_pad($local_address, 22) . ' ' . \str_pad($remote_address, 22) ."\n";
|
|
|
}
|
|
}
|
|
|
if ($str) {
|
|
if ($str) {
|
|
|
- \file_put_contents(static::$_statisticsFile, $str, FILE_APPEND);
|
|
|
|
|
|
|
+ \file_put_contents(static::$_statisticsFile, $str, \FILE_APPEND);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -2038,13 +2061,13 @@ class Worker
|
|
|
public static function checkErrors()
|
|
public static function checkErrors()
|
|
|
{
|
|
{
|
|
|
if (static::STATUS_SHUTDOWN !== static::$_status) {
|
|
if (static::STATUS_SHUTDOWN !== static::$_status) {
|
|
|
- $error_msg = static::$_OS === OS_TYPE_LINUX ? 'Worker['. \posix_getpid() .'] process terminated' : 'Worker process terminated';
|
|
|
|
|
|
|
+ $error_msg = static::$_OS === \OS_TYPE_LINUX ? 'Worker['. \posix_getpid() .'] process terminated' : 'Worker process terminated';
|
|
|
$errors = error_get_last();
|
|
$errors = error_get_last();
|
|
|
- if ($errors && ($errors['type'] === E_ERROR ||
|
|
|
|
|
- $errors['type'] === E_PARSE ||
|
|
|
|
|
- $errors['type'] === E_CORE_ERROR ||
|
|
|
|
|
- $errors['type'] === E_COMPILE_ERROR ||
|
|
|
|
|
- $errors['type'] === E_RECOVERABLE_ERROR)
|
|
|
|
|
|
|
+ if ($errors && ($errors['type'] === \E_ERROR ||
|
|
|
|
|
+ $errors['type'] === \E_PARSE ||
|
|
|
|
|
+ $errors['type'] === \E_CORE_ERROR ||
|
|
|
|
|
+ $errors['type'] === \E_COMPILE_ERROR ||
|
|
|
|
|
+ $errors['type'] === \E_RECOVERABLE_ERROR)
|
|
|
) {
|
|
) {
|
|
|
$error_msg .= ' with ERROR: ' . static::getErrorType($errors['type']) . " \"{$errors['message']} in {$errors['file']} on line {$errors['line']}\"";
|
|
$error_msg .= ' with ERROR: ' . static::getErrorType($errors['type']) . " \"{$errors['message']} in {$errors['file']} on line {$errors['line']}\"";
|
|
|
}
|
|
}
|
|
@@ -2080,7 +2103,7 @@ class Worker
|
|
|
static::safeEcho($msg);
|
|
static::safeEcho($msg);
|
|
|
}
|
|
}
|
|
|
\file_put_contents((string)static::$logFile, \date('Y-m-d H:i:s') . ' ' . 'pid:'
|
|
\file_put_contents((string)static::$logFile, \date('Y-m-d H:i:s') . ' ' . 'pid:'
|
|
|
- . (static::$_OS === OS_TYPE_LINUX ? \posix_getpid() : 1) . ' ' . $msg, FILE_APPEND | LOCK_EX);
|
|
|
|
|
|
|
+ . (static::$_OS === \OS_TYPE_LINUX ? \posix_getpid() : 1) . ' ' . $msg, \FILE_APPEND | \LOCK_EX);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -2120,18 +2143,21 @@ class Worker
|
|
|
private static function outputStream($stream = null)
|
|
private static function outputStream($stream = null)
|
|
|
{
|
|
{
|
|
|
if (!$stream) {
|
|
if (!$stream) {
|
|
|
- $stream = static::$_outputStream ? static::$_outputStream : STDOUT;
|
|
|
|
|
|
|
+ $stream = static::$_outputStream ? static::$_outputStream : \STDOUT;
|
|
|
}
|
|
}
|
|
|
if (!$stream || !\is_resource($stream) || 'stream' !== \get_resource_type($stream)) {
|
|
if (!$stream || !\is_resource($stream) || 'stream' !== \get_resource_type($stream)) {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
$stat = \fstat($stream);
|
|
$stat = \fstat($stream);
|
|
|
|
|
+ if (!$stat) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
if (($stat['mode'] & 0170000) === 0100000) {
|
|
if (($stat['mode'] & 0170000) === 0100000) {
|
|
|
// file
|
|
// file
|
|
|
static::$_outputDecorated = false;
|
|
static::$_outputDecorated = false;
|
|
|
} else {
|
|
} else {
|
|
|
static::$_outputDecorated =
|
|
static::$_outputDecorated =
|
|
|
- static::$_OS === OS_TYPE_LINUX &&
|
|
|
|
|
|
|
+ static::$_OS === \OS_TYPE_LINUX &&
|
|
|
\function_exists('posix_isatty') &&
|
|
\function_exists('posix_isatty') &&
|
|
|
\posix_isatty($stream);
|
|
\posix_isatty($stream);
|
|
|
}
|
|
}
|
|
@@ -2152,26 +2178,27 @@ class Worker
|
|
|
static::$_pidMap[$this->workerId] = array();
|
|
static::$_pidMap[$this->workerId] = array();
|
|
|
|
|
|
|
|
// Get autoload root path.
|
|
// Get autoload root path.
|
|
|
- $backtrace = \debug_backtrace();
|
|
|
|
|
|
|
+ $backtrace = \debug_backtrace();
|
|
|
$this->_autoloadRootPath = \dirname($backtrace[0]['file']);
|
|
$this->_autoloadRootPath = \dirname($backtrace[0]['file']);
|
|
|
-
|
|
|
|
|
- if (static::$_OS === OS_TYPE_LINUX && version_compare(PHP_VERSION,'7.0.0', 'ge')) {
|
|
|
|
|
- $php_uname = strtolower(php_uname('s'));
|
|
|
|
|
- // If not Mac OS then turn reusePort on.
|
|
|
|
|
- if ($php_uname !== 'darwin') {
|
|
|
|
|
- $this->reusePort = true;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ Autoloader::setRootPath($this->_autoloadRootPath);
|
|
|
|
|
|
|
|
// Context for socket.
|
|
// Context for socket.
|
|
|
if ($socket_name) {
|
|
if ($socket_name) {
|
|
|
$this->_socketName = $socket_name;
|
|
$this->_socketName = $socket_name;
|
|
|
- $this->_localSocket = $this->parseSocketAddress();
|
|
|
|
|
if (!isset($context_option['socket']['backlog'])) {
|
|
if (!isset($context_option['socket']['backlog'])) {
|
|
|
$context_option['socket']['backlog'] = static::DEFAULT_BACKLOG;
|
|
$context_option['socket']['backlog'] = static::DEFAULT_BACKLOG;
|
|
|
}
|
|
}
|
|
|
$this->_context = \stream_context_create($context_option);
|
|
$this->_context = \stream_context_create($context_option);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // Turn reusePort on.
|
|
|
|
|
+ if (static::$_OS === \OS_TYPE_LINUX // if linux
|
|
|
|
|
+ && \version_compare(\PHP_VERSION,'7.0.0', 'ge') // if php >= 7.0.0
|
|
|
|
|
+ && \strtolower(\php_uname('s')) !== 'darwin' // if not Mac OS
|
|
|
|
|
+ && $this->transport !== 'unix') { // if not unix socket
|
|
|
|
|
+
|
|
|
|
|
+ $this->reusePort = true;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -2191,10 +2218,10 @@ class Worker
|
|
|
|
|
|
|
|
if (!$this->_mainSocket) {
|
|
if (!$this->_mainSocket) {
|
|
|
|
|
|
|
|
- $local_socket = !empty($this->_localSocket) ? $this->_localSocket : $this->parseSocketAddress();
|
|
|
|
|
|
|
+ $local_socket = $this->parseSocketAddress();
|
|
|
|
|
|
|
|
// Flag.
|
|
// Flag.
|
|
|
- $flags = $this->transport === 'udp' ? STREAM_SERVER_BIND : STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
|
|
|
|
|
|
|
+ $flags = $this->transport === 'udp' ? \STREAM_SERVER_BIND : \STREAM_SERVER_BIND | \STREAM_SERVER_LISTEN;
|
|
|
$errno = 0;
|
|
$errno = 0;
|
|
|
$errmsg = '';
|
|
$errmsg = '';
|
|
|
// SO_REUSEPORT.
|
|
// SO_REUSEPORT.
|
|
@@ -2213,10 +2240,10 @@ class Worker
|
|
|
} elseif ($this->transport === 'unix') {
|
|
} elseif ($this->transport === 'unix') {
|
|
|
$socket_file = \substr($local_socket, 7);
|
|
$socket_file = \substr($local_socket, 7);
|
|
|
if ($this->user) {
|
|
if ($this->user) {
|
|
|
- chown($socket_file, $this->user);
|
|
|
|
|
|
|
+ \chown($socket_file, $this->user);
|
|
|
}
|
|
}
|
|
|
if ($this->group) {
|
|
if ($this->group) {
|
|
|
- chgrp($socket_file, $this->group);
|
|
|
|
|
|
|
+ \chgrp($socket_file, $this->group);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -2224,8 +2251,8 @@ class Worker
|
|
|
if (\function_exists('socket_import_stream') && static::$_builtinTransports[$this->transport] === 'tcp') {
|
|
if (\function_exists('socket_import_stream') && static::$_builtinTransports[$this->transport] === 'tcp') {
|
|
|
\set_error_handler(function(){});
|
|
\set_error_handler(function(){});
|
|
|
$socket = \socket_import_stream($this->_mainSocket);
|
|
$socket = \socket_import_stream($this->_mainSocket);
|
|
|
- \socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 1);
|
|
|
|
|
- \socket_set_option($socket, SOL_TCP, TCP_NODELAY, 1);
|
|
|
|
|
|
|
+ \socket_set_option($socket, \SOL_SOCKET, \SO_KEEPALIVE, 1);
|
|
|
|
|
+ \socket_set_option($socket, \SOL_TCP, \TCP_NODELAY, 1);
|
|
|
\restore_error_handler();
|
|
\restore_error_handler();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -2274,7 +2301,7 @@ class Worker
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (!isset(static::$_builtinTransports[$this->transport])) {
|
|
if (!isset(static::$_builtinTransports[$this->transport])) {
|
|
|
- throw new \Exception('Bad worker->transport ' . \var_export($this->transport, true));
|
|
|
|
|
|
|
+ throw new Exception('Bad worker->transport ' . \var_export($this->transport, true));
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
$this->transport = $scheme;
|
|
$this->transport = $scheme;
|
|
@@ -2321,7 +2348,7 @@ class Worker
|
|
|
*/
|
|
*/
|
|
|
public function getSocketName()
|
|
public function getSocketName()
|
|
|
{
|
|
{
|
|
|
- return $this->_socketName ? lcfirst($this->_socketName) : 'none';
|
|
|
|
|
|
|
+ return $this->_socketName ? \lcfirst($this->_socketName) : 'none';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -2359,7 +2386,7 @@ class Worker
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
\restore_error_handler();
|
|
\restore_error_handler();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// Try to emit onWorkerStart callback.
|
|
// Try to emit onWorkerStart callback.
|
|
|
if ($this->onWorkerStart) {
|
|
if ($this->onWorkerStart) {
|
|
|
try {
|
|
try {
|
|
@@ -2422,7 +2449,7 @@ class Worker
|
|
|
{
|
|
{
|
|
|
// Accept a connection on server socket.
|
|
// Accept a connection on server socket.
|
|
|
\set_error_handler(function(){});
|
|
\set_error_handler(function(){});
|
|
|
- $new_socket = stream_socket_accept($socket, 0, $remote_address);
|
|
|
|
|
|
|
+ $new_socket = \stream_socket_accept($socket, 0, $remote_address);
|
|
|
\restore_error_handler();
|
|
\restore_error_handler();
|
|
|
|
|
|
|
|
// Thundering herd.
|
|
// Thundering herd.
|
|
@@ -2500,7 +2527,7 @@ class Worker
|
|
|
}else{
|
|
}else{
|
|
|
\call_user_func($this->onMessage, $connection, $recv_buffer);
|
|
\call_user_func($this->onMessage, $connection, $recv_buffer);
|
|
|
}
|
|
}
|
|
|
- ConnectionInterface::$statistics['total_request']++;
|
|
|
|
|
|
|
+ ++ConnectionInterface::$statistics['total_request'];
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
static::log($e);
|
|
static::log($e);
|
|
|
exit(250);
|
|
exit(250);
|