|
|
@@ -12,16 +12,16 @@
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
*/
|
|
|
namespace Workerman;
|
|
|
-require_once __DIR__ . '/Lib/Constants.php';
|
|
|
|
|
|
use Workerman\Events\EventInterface;
|
|
|
use Workerman\Connection\ConnectionInterface;
|
|
|
use Workerman\Connection\TcpConnection;
|
|
|
use Workerman\Connection\UdpConnection;
|
|
|
-use Workerman\Lib\Timer;
|
|
|
+use Workerman\Timer;
|
|
|
use Workerman\Events\Select;
|
|
|
use \Exception;
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* Worker class
|
|
|
* A container for listening ports
|
|
|
@@ -33,7 +33,7 @@ class Worker
|
|
|
*
|
|
|
* @var string
|
|
|
*/
|
|
|
- const VERSION = '4.0.32';
|
|
|
+ const VERSION = '5.0.0';
|
|
|
|
|
|
/**
|
|
|
* Status starting.
|
|
|
@@ -64,14 +64,6 @@ class Worker
|
|
|
const STATUS_RELOADING = 8;
|
|
|
|
|
|
/**
|
|
|
- * After sending the restart command to the child process KILL_WORKER_TIMER_TIME seconds,
|
|
|
- * if the process is still living then forced to kill.
|
|
|
- *
|
|
|
- * @var int
|
|
|
- */
|
|
|
- const KILL_WORKER_TIMER_TIME = 2;
|
|
|
-
|
|
|
- /**
|
|
|
* Default backlog. Backlog is the maximum length of the queue of pending connections.
|
|
|
*
|
|
|
* @var int
|
|
|
@@ -215,7 +207,7 @@ class Worker
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
- public $connections = array();
|
|
|
+ public $connections = [];
|
|
|
|
|
|
/**
|
|
|
* Application layer protocol.
|
|
|
@@ -225,13 +217,6 @@ class Worker
|
|
|
public $protocol = null;
|
|
|
|
|
|
/**
|
|
|
- * Root path for autoload.
|
|
|
- *
|
|
|
- * @var string
|
|
|
- */
|
|
|
- protected $_autoloadRootPath = '';
|
|
|
-
|
|
|
- /**
|
|
|
* Pause accept new connections or not.
|
|
|
*
|
|
|
* @var bool
|
|
|
@@ -271,7 +256,7 @@ class Worker
|
|
|
* @var string
|
|
|
*/
|
|
|
public static $statusFile = '';
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Log file.
|
|
|
*
|
|
|
@@ -282,7 +267,7 @@ class Worker
|
|
|
/**
|
|
|
* Global event loop.
|
|
|
*
|
|
|
- * @var EventInterface
|
|
|
+ * @var Select
|
|
|
*/
|
|
|
public static $globalEvent = null;
|
|
|
|
|
|
@@ -315,6 +300,20 @@ class Worker
|
|
|
public static $processTitle = 'WorkerMan';
|
|
|
|
|
|
/**
|
|
|
+ * After sending the stop command to the child process stopTimeout seconds,
|
|
|
+ * if the process is still living then forced to kill.
|
|
|
+ *
|
|
|
+ * @var int
|
|
|
+ */
|
|
|
+ public static $stopTimeout = 2;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Command
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ public static $command = '';
|
|
|
+
|
|
|
+ /**
|
|
|
* The PID of master process.
|
|
|
*
|
|
|
* @var int
|
|
|
@@ -354,7 +353,7 @@ class Worker
|
|
|
*
|
|
|
* @var Worker[]
|
|
|
*/
|
|
|
- protected static $_workers = array();
|
|
|
+ protected static $_workers = [];
|
|
|
|
|
|
/**
|
|
|
* All worker processes pid.
|
|
|
@@ -362,7 +361,7 @@ class Worker
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
- protected static $_pidMap = array();
|
|
|
+ protected static $_pidMap = [];
|
|
|
|
|
|
/**
|
|
|
* All worker processes waiting for restart.
|
|
|
@@ -370,7 +369,7 @@ class Worker
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
- protected static $_pidsToRestart = array();
|
|
|
+ protected static $_pidsToRestart = [];
|
|
|
|
|
|
/**
|
|
|
* Mapping from PID to worker process ID.
|
|
|
@@ -378,7 +377,7 @@ class Worker
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
- protected static $_idMap = array();
|
|
|
+ protected static $_idMap = [];
|
|
|
|
|
|
/**
|
|
|
* Current status.
|
|
|
@@ -444,57 +443,50 @@ class Worker
|
|
|
protected static $_startFile = '';
|
|
|
|
|
|
/**
|
|
|
- * OS.
|
|
|
- *
|
|
|
- * @var string
|
|
|
- */
|
|
|
- protected static $_OS = \OS_TYPE_LINUX;
|
|
|
-
|
|
|
- /**
|
|
|
* Processes for windows.
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
- protected static $_processForWindows = array();
|
|
|
+ protected static $_processForWindows = [];
|
|
|
|
|
|
/**
|
|
|
* Status info of current worker process.
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
- protected static $_globalStatistics = array(
|
|
|
+ protected static $_globalStatistics = [
|
|
|
'start_timestamp' => 0,
|
|
|
- 'worker_exit_info' => array()
|
|
|
- );
|
|
|
+ 'worker_exit_info' => []
|
|
|
+ ];
|
|
|
|
|
|
/**
|
|
|
* Available event loops.
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
- protected static $_availableEventLoops = array(
|
|
|
+ protected static $_availableEventLoops = [
|
|
|
'event' => '\Workerman\Events\Event',
|
|
|
'libevent' => '\Workerman\Events\Libevent'
|
|
|
- );
|
|
|
+ ];
|
|
|
|
|
|
/**
|
|
|
* PHP built-in protocols.
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
- protected static $_builtinTransports = array(
|
|
|
+ protected static $_builtinTransports = [
|
|
|
'tcp' => 'tcp',
|
|
|
'udp' => 'udp',
|
|
|
'unix' => 'unix',
|
|
|
'ssl' => 'tcp'
|
|
|
- );
|
|
|
+ ];
|
|
|
|
|
|
/**
|
|
|
* PHP built-in error types.
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
- protected static $_errorType = array(
|
|
|
+ protected static $_errorType = [
|
|
|
\E_ERROR => 'E_ERROR', // 1
|
|
|
\E_WARNING => 'E_WARNING', // 2
|
|
|
\E_PARSE => 'E_PARSE', // 4
|
|
|
@@ -510,7 +502,7 @@ class Worker
|
|
|
\E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR', // 4096
|
|
|
\E_DEPRECATED => 'E_DEPRECATED', // 8192
|
|
|
\E_USER_DEPRECATED => 'E_USER_DEPRECATED' // 16384
|
|
|
- );
|
|
|
+ ];
|
|
|
|
|
|
/**
|
|
|
* Graceful stop or not.
|
|
|
@@ -562,9 +554,6 @@ class Worker
|
|
|
if (\PHP_SAPI !== 'cli') {
|
|
|
exit("Only run in command line mode \n");
|
|
|
}
|
|
|
- if (\DIRECTORY_SEPARATOR === '\\') {
|
|
|
- self::$_OS = \OS_TYPE_WINDOWS;
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -592,7 +581,7 @@ class Worker
|
|
|
|
|
|
// Log file.
|
|
|
if (empty(static::$logFile)) {
|
|
|
- static::$logFile = __DIR__ . '/../workerman.log';
|
|
|
+ static::$logFile = __DIR__ . '/../../workerman.log';
|
|
|
}
|
|
|
$log_file = (string)static::$logFile;
|
|
|
if (!\is_file($log_file)) {
|
|
|
@@ -648,12 +637,10 @@ class Worker
|
|
|
*/
|
|
|
protected static function initWorkers()
|
|
|
{
|
|
|
- if (static::$_OS !== \OS_TYPE_LINUX) {
|
|
|
+ if (\DIRECTORY_SEPARATOR !== '/') {
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- static::$_statisticsFile = static::$statusFile ? static::$statusFile : __DIR__ . '/../workerman-' .posix_getpid().'.status';
|
|
|
-
|
|
|
+ static::$_statisticsFile = static::$statusFile ?: __DIR__ . '/../workerman-' .posix_getpid().'.status';
|
|
|
foreach (static::$_workers as $worker) {
|
|
|
// Worker name.
|
|
|
if (empty($worker->name)) {
|
|
|
@@ -678,7 +665,7 @@ class Worker
|
|
|
// Get column mapping for UI
|
|
|
foreach(static::getUiColumns() as $column_name => $prop){
|
|
|
!isset($worker->{$prop}) && $worker->{$prop} = 'NNNN';
|
|
|
- $prop_length = \strlen((string) $worker->{$prop});
|
|
|
+ $prop_length = \strlen($worker->{$prop});
|
|
|
$key = '_max' . \ucfirst(\strtolower($column_name)) . 'NameLength';
|
|
|
static::$$key = \max(static::$$key, $prop_length);
|
|
|
}
|
|
|
@@ -716,7 +703,7 @@ class Worker
|
|
|
/**
|
|
|
* Get global event-loop instance.
|
|
|
*
|
|
|
- * @return EventInterface
|
|
|
+ * @return Select
|
|
|
*/
|
|
|
public static function getEventLoop()
|
|
|
{
|
|
|
@@ -738,7 +725,7 @@ class Worker
|
|
|
protected static function initId()
|
|
|
{
|
|
|
foreach (static::$_workers as $worker_id => $worker) {
|
|
|
- $new_id_map = array();
|
|
|
+ $new_id_map = [];
|
|
|
$worker->count = $worker->count < 1 ? 1 : $worker->count;
|
|
|
for($key = 0; $key < $worker->count; $key++) {
|
|
|
$new_id_map[$key] = isset(static::$_idMap[$worker_id][$key]) ? static::$_idMap[$worker_id][$key] : 0;
|
|
|
@@ -765,20 +752,20 @@ class Worker
|
|
|
*/
|
|
|
protected static function displayUI()
|
|
|
{
|
|
|
- global $argv;
|
|
|
- if (\in_array('-q', $argv)) {
|
|
|
+ $tmp_argv = static::getArgv();
|
|
|
+ if (\in_array('-q', $tmp_argv)) {
|
|
|
return;
|
|
|
}
|
|
|
- if (static::$_OS !== \OS_TYPE_LINUX) {
|
|
|
+ if (\DIRECTORY_SEPARATOR !== '/') {
|
|
|
static::safeEcho("----------------------- WORKERMAN -----------------------------\r\n");
|
|
|
static::safeEcho('Workerman version:'. static::VERSION. ' PHP version:'. \PHP_VERSION. "\r\n");
|
|
|
static::safeEcho("------------------------ WORKERS -------------------------------\r\n");
|
|
|
- static::safeEcho("worker listen processes status\r\n");
|
|
|
+ static::safeEcho("worker listen processes status\r\n");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
//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:', 16, ' ', \STR_PAD_LEFT) . \PHP_VERSION . \str_pad('Event-loop:', 16, ' ', \STR_PAD_LEFT) . static::getEventLoopName() . \PHP_EOL;
|
|
|
!\defined('LINE_VERSIOIN_LENGTH') && \define('LINE_VERSIOIN_LENGTH', \strlen($line_version));
|
|
|
$total_length = static::getSingleLineTotalLength();
|
|
|
$line_one = '<n>' . \str_pad('<w> WORKERMAN </w>', $total_length + \strlen('<w></w>'), '-', \STR_PAD_BOTH) . '</n>'. \PHP_EOL;
|
|
|
@@ -800,9 +787,9 @@ class Worker
|
|
|
$content = '';
|
|
|
foreach(static::getUiColumns() as $column_name => $prop){
|
|
|
$key = '_max' . \ucfirst(\strtolower($column_name)) . 'NameLength';
|
|
|
- \preg_match_all("/(<n>|<\/n>|<w>|<\/w>|<g>|<\/g>)/is", (string) $worker->{$prop}, $matches);
|
|
|
+ \preg_match_all("/(<n>|<\/n>|<w>|<\/w>|<g>|<\/g>)/is", $worker->{$prop}, $matches);
|
|
|
$place_holder_length = !empty($matches) ? \strlen(\implode('', $matches[0])) : 0;
|
|
|
- $content .= \str_pad((string) $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);
|
|
|
}
|
|
|
@@ -812,15 +799,9 @@ class Worker
|
|
|
!empty($content) && static::safeEcho($line_last);
|
|
|
|
|
|
if (static::$daemonize) {
|
|
|
- $tmpArgv = $argv;
|
|
|
- foreach ($tmpArgv as $index => $value) {
|
|
|
- if ($value == '-d') {
|
|
|
- unset($tmpArgv[$index]);
|
|
|
- } elseif ($value == 'start' || $value == 'restart') {
|
|
|
- $tmpArgv[$index] = 'stop';
|
|
|
- }
|
|
|
- }
|
|
|
- static::safeEcho("Input \"php ".implode(' ', $tmpArgv)."\" to stop. Start success.\n\n");
|
|
|
+ global $argv;
|
|
|
+ $start_file = $argv[0];
|
|
|
+ static::safeEcho('Input "php '. $start_file . ' stop" to stop. Start success.' . "\n\n");
|
|
|
} else {
|
|
|
static::safeEcho("Press Ctrl+C to stop. Start success.\n");
|
|
|
}
|
|
|
@@ -829,21 +810,21 @@ class Worker
|
|
|
/**
|
|
|
* Get UI columns to be shown in terminal
|
|
|
*
|
|
|
- * 1. $column_map: array('ui_column_name' => 'clas_property_name')
|
|
|
+ * 1. $column_map: ['ui_column_name' => 'clas_property_name']
|
|
|
* 2. Consider move into configuration in future
|
|
|
*
|
|
|
* @return array
|
|
|
*/
|
|
|
public static function getUiColumns()
|
|
|
{
|
|
|
- return array(
|
|
|
+ return [
|
|
|
'proto' => 'transport',
|
|
|
'user' => 'user',
|
|
|
'worker' => 'name',
|
|
|
'socket' => 'socket',
|
|
|
'processes' => 'count',
|
|
|
'status' => 'status',
|
|
|
- );
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -874,27 +855,27 @@ class Worker
|
|
|
*/
|
|
|
protected static function parseCommand()
|
|
|
{
|
|
|
- if (static::$_OS !== \OS_TYPE_LINUX) {
|
|
|
+ if (\DIRECTORY_SEPARATOR !== '/') {
|
|
|
return;
|
|
|
}
|
|
|
global $argv;
|
|
|
// Check argv;
|
|
|
$start_file = $argv[0];
|
|
|
$usage = "Usage: php yourfile <command> [mode]\nCommands: \nstart\t\tStart worker in DEBUG mode.\n\t\tUse mode -d to start in DAEMON mode.\nstop\t\tStop worker.\n\t\tUse mode -g to stop gracefully.\nrestart\t\tRestart workers.\n\t\tUse mode -d to start in DAEMON mode.\n\t\tUse mode -g to stop gracefully.\nreload\t\tReload codes.\n\t\tUse mode -g to reload gracefully.\nstatus\t\tGet worker status.\n\t\tUse mode -d to show live status.\nconnections\tGet worker connections.\n";
|
|
|
- $available_commands = array(
|
|
|
+ $available_commands = [
|
|
|
'start',
|
|
|
'stop',
|
|
|
'restart',
|
|
|
'reload',
|
|
|
'status',
|
|
|
'connections',
|
|
|
- );
|
|
|
- $available_mode = array(
|
|
|
+ ];
|
|
|
+ $available_mode = [
|
|
|
'-d',
|
|
|
'-g'
|
|
|
- );
|
|
|
+ ];
|
|
|
$command = $mode = '';
|
|
|
- foreach ($argv as $value) {
|
|
|
+ foreach (static::getArgv() as $value) {
|
|
|
if (\in_array($value, $available_commands)) {
|
|
|
$command = $value;
|
|
|
} elseif (\in_array($value, $available_mode)) {
|
|
|
@@ -930,7 +911,7 @@ class Worker
|
|
|
exit;
|
|
|
}
|
|
|
|
|
|
- $statistics_file = static::$statusFile ? static::$statusFile : __DIR__ . "/../workerman-$master_pid.status";
|
|
|
+ $statistics_file = static::$statusFile ?: __DIR__ . "/../workerman-$master_pid.status";
|
|
|
|
|
|
// execute command.
|
|
|
switch ($command) {
|
|
|
@@ -1029,6 +1010,12 @@ class Worker
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static function getArgv()
|
|
|
+ {
|
|
|
+ global $argv;
|
|
|
+ return isset($argv[1]) ? $argv : (static::$command ? \explode(' ', static::$command) : $argv);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Format status data.
|
|
|
*
|
|
|
@@ -1037,7 +1024,7 @@ class Worker
|
|
|
*/
|
|
|
protected static function formatStatusData($statistics_file)
|
|
|
{
|
|
|
- static $total_request_cache = array();
|
|
|
+ static $total_request_cache = [];
|
|
|
if (!\is_readable($statistics_file)) {
|
|
|
return '';
|
|
|
}
|
|
|
@@ -1046,11 +1033,11 @@ class Worker
|
|
|
return '';
|
|
|
}
|
|
|
$status_str = '';
|
|
|
- $current_total_request = array();
|
|
|
+ $current_total_request = [];
|
|
|
$worker_info = \unserialize($info[0]);
|
|
|
\ksort($worker_info, SORT_NUMERIC);
|
|
|
unset($info[0]);
|
|
|
- $data_waiting_sort = array();
|
|
|
+ $data_waiting_sort = [];
|
|
|
$read_process_status = false;
|
|
|
$total_requests = 0;
|
|
|
$total_qps = 0;
|
|
|
@@ -1120,28 +1107,13 @@ class Worker
|
|
|
*/
|
|
|
protected static function installSignal()
|
|
|
{
|
|
|
- if (static::$_OS !== \OS_TYPE_LINUX) {
|
|
|
+ if (\DIRECTORY_SEPARATOR !== '/') {
|
|
|
return;
|
|
|
}
|
|
|
- $signalHandler = '\Workerman\Worker::signalHandler';
|
|
|
- // stop
|
|
|
- \pcntl_signal(\SIGINT, $signalHandler, false);
|
|
|
- // stop
|
|
|
- \pcntl_signal(\SIGTERM, $signalHandler, false);
|
|
|
- // stop
|
|
|
- \pcntl_signal(\SIGHUP, $signalHandler, false);
|
|
|
- // stop
|
|
|
- \pcntl_signal(\SIGTSTP, $signalHandler, false);
|
|
|
- // graceful stop
|
|
|
- \pcntl_signal(\SIGQUIT, $signalHandler, false);
|
|
|
- // reload
|
|
|
- \pcntl_signal(\SIGUSR1, $signalHandler, false);
|
|
|
- // graceful reload
|
|
|
- \pcntl_signal(\SIGUSR2, $signalHandler, false);
|
|
|
- // status
|
|
|
- \pcntl_signal(\SIGIOT, $signalHandler, false);
|
|
|
- // connection status
|
|
|
- \pcntl_signal(\SIGIO, $signalHandler, false);
|
|
|
+ $signals = [\SIGINT, \SIGTERM, \SIGHUP, \SIGTSTP, \SIGQUIT, \SIGUSR1, \SIGUSR2, \SIGIOT, \SIGIO];
|
|
|
+ foreach ($signals as $signal) {
|
|
|
+ \pcntl_signal($signal, [Worker::class, 'signalHandler'], false);
|
|
|
+ }
|
|
|
// ignore
|
|
|
\pcntl_signal(\SIGPIPE, \SIG_IGN, false);
|
|
|
}
|
|
|
@@ -1153,44 +1125,14 @@ class Worker
|
|
|
*/
|
|
|
protected static function reinstallSignal()
|
|
|
{
|
|
|
- if (static::$_OS !== \OS_TYPE_LINUX) {
|
|
|
+ if (\DIRECTORY_SEPARATOR !== '/') {
|
|
|
return;
|
|
|
}
|
|
|
- $signalHandler = '\Workerman\Worker::signalHandler';
|
|
|
- // uninstall stop signal handler
|
|
|
- \pcntl_signal(\SIGINT, \SIG_IGN, false);
|
|
|
- // uninstall stop signal handler
|
|
|
- \pcntl_signal(\SIGTERM, \SIG_IGN, false);
|
|
|
- // uninstall stop signal handler
|
|
|
- \pcntl_signal(\SIGHUP, \SIG_IGN, false);
|
|
|
- // uninstall stop signal handler
|
|
|
- \pcntl_signal(\SIGTSTP, \SIG_IGN, false);
|
|
|
- // uninstall graceful stop signal handler
|
|
|
- \pcntl_signal(\SIGQUIT, \SIG_IGN, false);
|
|
|
- // uninstall reload signal handler
|
|
|
- \pcntl_signal(\SIGUSR1, \SIG_IGN, false);
|
|
|
- // uninstall graceful reload signal handler
|
|
|
- \pcntl_signal(\SIGUSR2, \SIG_IGN, false);
|
|
|
- // uninstall status signal handler
|
|
|
- \pcntl_signal(\SIGIOT, \SIG_IGN, false);
|
|
|
- // uninstall connections status signal handler
|
|
|
- \pcntl_signal(\SIGIO, \SIG_IGN, false);
|
|
|
- // reinstall stop signal handler
|
|
|
- static::$globalEvent->add(\SIGINT, EventInterface::EV_SIGNAL, $signalHandler);
|
|
|
- // reinstall graceful stop signal handler
|
|
|
- static::$globalEvent->add(\SIGQUIT, EventInterface::EV_SIGNAL, $signalHandler);
|
|
|
- // reinstall graceful stop signal handler
|
|
|
- static::$globalEvent->add(\SIGHUP, EventInterface::EV_SIGNAL, $signalHandler);
|
|
|
- // reinstall graceful stop signal handler
|
|
|
- static::$globalEvent->add(\SIGTSTP, EventInterface::EV_SIGNAL, $signalHandler);
|
|
|
- // reinstall reload signal handler
|
|
|
- static::$globalEvent->add(\SIGUSR1, EventInterface::EV_SIGNAL, $signalHandler);
|
|
|
- // reinstall graceful reload signal handler
|
|
|
- static::$globalEvent->add(\SIGUSR2, EventInterface::EV_SIGNAL, $signalHandler);
|
|
|
- // reinstall status signal handler
|
|
|
- static::$globalEvent->add(\SIGIOT, EventInterface::EV_SIGNAL, $signalHandler);
|
|
|
- // reinstall connection status signal handler
|
|
|
- static::$globalEvent->add(\SIGIO, EventInterface::EV_SIGNAL, $signalHandler);
|
|
|
+ $signals = [\SIGINT, \SIGTERM, \SIGHUP, \SIGTSTP, \SIGQUIT, \SIGUSR1, \SIGUSR2, \SIGIOT, \SIGIO];
|
|
|
+ foreach ($signals as $signal) {
|
|
|
+ \pcntl_signal($signal, \SIG_IGN, false);
|
|
|
+ static::$globalEvent->onSignal($signal, '\Workerman\Worker::signalHandler');
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -1239,7 +1181,7 @@ class Worker
|
|
|
*/
|
|
|
protected static function daemonize()
|
|
|
{
|
|
|
- if (!static::$daemonize || static::$_OS !== \OS_TYPE_LINUX) {
|
|
|
+ if (!static::$daemonize || \DIRECTORY_SEPARATOR !== '/') {
|
|
|
return;
|
|
|
}
|
|
|
\umask(0);
|
|
|
@@ -1268,7 +1210,7 @@ class Worker
|
|
|
*/
|
|
|
public static function resetStd()
|
|
|
{
|
|
|
- if (!static::$daemonize || static::$_OS !== \OS_TYPE_LINUX) {
|
|
|
+ if (!static::$daemonize || \DIRECTORY_SEPARATOR !== '/') {
|
|
|
return;
|
|
|
}
|
|
|
global $STDOUT, $STDERR;
|
|
|
@@ -1303,7 +1245,7 @@ class Worker
|
|
|
*/
|
|
|
protected static function saveMasterPid()
|
|
|
{
|
|
|
- if (static::$_OS !== \OS_TYPE_LINUX) {
|
|
|
+ if (\DIRECTORY_SEPARATOR !== '/') {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -1351,7 +1293,7 @@ class Worker
|
|
|
*/
|
|
|
protected static function getAllWorkerPids()
|
|
|
{
|
|
|
- $pid_array = array();
|
|
|
+ $pid_array = [];
|
|
|
foreach (static::$_pidMap as $worker_pid_array) {
|
|
|
foreach ($worker_pid_array as $worker_pid) {
|
|
|
$pid_array[$worker_pid] = $worker_pid;
|
|
|
@@ -1367,7 +1309,7 @@ class Worker
|
|
|
*/
|
|
|
protected static function forkWorkers()
|
|
|
{
|
|
|
- if (static::$_OS === \OS_TYPE_LINUX) {
|
|
|
+ if (\DIRECTORY_SEPARATOR === '/') {
|
|
|
static::forkWorkersForLinux();
|
|
|
} else {
|
|
|
static::forkWorkersForWindows();
|
|
|
@@ -1407,8 +1349,7 @@ class Worker
|
|
|
protected static function forkWorkersForWindows()
|
|
|
{
|
|
|
$files = static::getStartFilesForWindows();
|
|
|
- global $argv;
|
|
|
- if(\in_array('-q', $argv) || \count($files) === 1)
|
|
|
+ if(\in_array('-q', static::getArgv()) || \count($files) === 1)
|
|
|
{
|
|
|
if(\count(static::$_workers) > 1)
|
|
|
{
|
|
|
@@ -1425,7 +1366,7 @@ class Worker
|
|
|
$worker = current(static::$_workers);
|
|
|
|
|
|
// Display UI.
|
|
|
- static::safeEcho(\str_pad($worker->name, 30) . \str_pad($worker->getSocketName(), 36) . \str_pad($worker->count, 10) . "[ok]\n");
|
|
|
+ static::safeEcho(\str_pad($worker->name, 21) . \str_pad($worker->getSocketName(), 36) . \str_pad($worker->count, 10) . "[ok]\n");
|
|
|
$worker->listen();
|
|
|
$worker->run();
|
|
|
exit("@@@child exit@@@\r\n");
|
|
|
@@ -1447,9 +1388,8 @@ class Worker
|
|
|
* @return array
|
|
|
*/
|
|
|
public static function getStartFilesForWindows() {
|
|
|
- global $argv;
|
|
|
- $files = array();
|
|
|
- foreach($argv as $file)
|
|
|
+ $files = [];
|
|
|
+ foreach(static::getArgv() as $file)
|
|
|
{
|
|
|
if(\is_file($file))
|
|
|
{
|
|
|
@@ -1540,7 +1480,7 @@ class Worker
|
|
|
if (static::$_status === static::STATUS_STARTING) {
|
|
|
static::resetStd();
|
|
|
}
|
|
|
- static::$_pidMap = array();
|
|
|
+ static::$_pidMap = [];
|
|
|
// Remove other listener.
|
|
|
foreach(static::$_workers as $key => $one_worker) {
|
|
|
if ($one_worker->workerId !== $worker->workerId) {
|
|
|
@@ -1620,13 +1560,7 @@ class Worker
|
|
|
protected static function setProcessTitle($title)
|
|
|
{
|
|
|
\set_error_handler(function(){});
|
|
|
- // >=php 5.5
|
|
|
- if (\function_exists('cli_set_process_title')) {
|
|
|
- \cli_set_process_title($title);
|
|
|
- } // Need proctitle when php<=5.5 .
|
|
|
- elseif (\extension_loaded('proctitle') && \function_exists('setproctitle')) {
|
|
|
- \setproctitle($title);
|
|
|
- }
|
|
|
+ \cli_set_process_title($title);
|
|
|
\restore_error_handler();
|
|
|
}
|
|
|
|
|
|
@@ -1637,7 +1571,7 @@ class Worker
|
|
|
*/
|
|
|
protected static function monitorWorkers()
|
|
|
{
|
|
|
- if (static::$_OS === \OS_TYPE_LINUX) {
|
|
|
+ if (\DIRECTORY_SEPARATOR === '/') {
|
|
|
static::monitorWorkersForLinux();
|
|
|
} else {
|
|
|
static::monitorWorkersForWindows();
|
|
|
@@ -1714,7 +1648,7 @@ class Worker
|
|
|
{
|
|
|
Timer::add(1, "\\Workerman\\Worker::checkWorkerStatusForWindows");
|
|
|
|
|
|
- static::$globalEvent->loop();
|
|
|
+ static::$globalEvent->run();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -1757,9 +1691,7 @@ class Worker
|
|
|
if (static::$onMasterReload) {
|
|
|
try {
|
|
|
\call_user_func(static::$onMasterReload);
|
|
|
- } catch (\Exception $e) {
|
|
|
- static::stopAll(250, $e);
|
|
|
- } catch (\Error $e) {
|
|
|
+ } catch (\Throwable $e) {
|
|
|
static::stopAll(250, $e);
|
|
|
}
|
|
|
static::initId();
|
|
|
@@ -1773,7 +1705,7 @@ class Worker
|
|
|
}
|
|
|
|
|
|
// Send reload signal to all child processes.
|
|
|
- $reloadable_pid_array = array();
|
|
|
+ $reloadable_pid_array = [];
|
|
|
foreach (static::$_pidMap as $worker_id => $worker_pid_array) {
|
|
|
$worker = static::$_workers[$worker_id];
|
|
|
if ($worker->reloadable) {
|
|
|
@@ -1802,9 +1734,9 @@ class Worker
|
|
|
$one_worker_pid = \current(static::$_pidsToRestart);
|
|
|
// Send reload signal to a worker process.
|
|
|
\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 stopTimeout seconds try to kill it.
|
|
|
if(!static::$_gracefulStop){
|
|
|
- Timer::add(static::KILL_WORKER_TIMER_TIME, '\posix_kill', array($one_worker_pid, \SIGKILL), false);
|
|
|
+ Timer::add(static::$stopTimeout, '\posix_kill', [$one_worker_pid, \SIGKILL], false);
|
|
|
}
|
|
|
} // For child processes.
|
|
|
else {
|
|
|
@@ -1814,9 +1746,7 @@ class Worker
|
|
|
if ($worker->onWorkerReload) {
|
|
|
try {
|
|
|
\call_user_func($worker->onWorkerReload, $worker);
|
|
|
- } catch (\Exception $e) {
|
|
|
- static::stopAll(250, $e);
|
|
|
- } catch (\Error $e) {
|
|
|
+ } catch (\Throwable $e) {
|
|
|
static::stopAll(250, $e);
|
|
|
}
|
|
|
}
|
|
|
@@ -1853,7 +1783,7 @@ class Worker
|
|
|
foreach ($worker_pid_array as $worker_pid) {
|
|
|
\posix_kill($worker_pid, $sig);
|
|
|
if(!static::$_gracefulStop){
|
|
|
- Timer::add(static::KILL_WORKER_TIMER_TIME, '\posix_kill', array($worker_pid, \SIGKILL), false);
|
|
|
+ Timer::add(static::$stopTimeout, '\posix_kill', [$worker_pid, \SIGKILL], false);
|
|
|
}
|
|
|
}
|
|
|
Timer::add(1, "\\Workerman\\Worker::checkIfChildRunning");
|
|
|
@@ -1871,9 +1801,9 @@ class Worker
|
|
|
}
|
|
|
}
|
|
|
if (!static::$_gracefulStop || ConnectionInterface::$statistics['connection_count'] <= 0) {
|
|
|
- static::$_workers = array();
|
|
|
+ static::$_workers = [];
|
|
|
if (static::$globalEvent) {
|
|
|
- static::$globalEvent->destroy();
|
|
|
+ static::$globalEvent->stop();
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
@@ -1928,17 +1858,17 @@ class Worker
|
|
|
{
|
|
|
// For master process.
|
|
|
if (static::$_masterPid === \posix_getpid()) {
|
|
|
- $all_worker_info = array();
|
|
|
+ $all_worker_info = [];
|
|
|
foreach(static::$_pidMap as $worker_id => $pid_array) {
|
|
|
/** @var /Workerman/Worker $worker */
|
|
|
$worker = static::$_workers[$worker_id];
|
|
|
foreach($pid_array as $pid) {
|
|
|
- $all_worker_info[$pid] = array('name' => $worker->name, 'listen' => $worker->getSocketName());
|
|
|
+ $all_worker_info[$pid] = ['name' => $worker->name, 'listen' => $worker->getSocketName()];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
\file_put_contents(static::$_statisticsFile, \serialize($all_worker_info)."\n", \FILE_APPEND);
|
|
|
- $loadavg = \function_exists('sys_getloadavg') ? \array_map('round', \sys_getloadavg(), array(2,2,2)) : array('-', '-', '-');
|
|
|
+ $loadavg = \function_exists('sys_getloadavg') ? \array_map('round', \sys_getloadavg(), [2,2,2]) : ['-', '-', '-'];
|
|
|
\file_put_contents(static::$_statisticsFile,
|
|
|
"----------------------------------------------GLOBAL STATUS----------------------------------------------------\n", \FILE_APPEND);
|
|
|
\file_put_contents(static::$_statisticsFile,
|
|
|
@@ -2085,7 +2015,7 @@ class Worker
|
|
|
public static function checkErrors()
|
|
|
{
|
|
|
if (static::STATUS_SHUTDOWN !== static::$_status) {
|
|
|
- $error_msg = static::$_OS === \OS_TYPE_LINUX ? 'Worker['. \posix_getpid() .'] process terminated' : 'Worker process terminated';
|
|
|
+ $error_msg = \DIRECTORY_SEPARATOR === '/' ? 'Worker['. \posix_getpid() .'] process terminated' : 'Worker process terminated';
|
|
|
$errors = error_get_last();
|
|
|
if ($errors && ($errors['type'] === \E_ERROR ||
|
|
|
$errors['type'] === \E_PARSE ||
|
|
|
@@ -2127,7 +2057,7 @@ class Worker
|
|
|
static::safeEcho($msg);
|
|
|
}
|
|
|
\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);
|
|
|
+ . (\DIRECTORY_SEPARATOR === '/' ? \posix_getpid() : 1) . ' ' . $msg, \FILE_APPEND | \LOCK_EX);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -2150,8 +2080,8 @@ class Worker
|
|
|
$green = "\033[32;40m";
|
|
|
$end = "\033[0m";
|
|
|
}
|
|
|
- $msg = \str_replace(array('<n>', '<w>', '<g>'), array($line, $white, $green), $msg);
|
|
|
- $msg = \str_replace(array('</n>', '</w>', '</g>'), $end, $msg);
|
|
|
+ $msg = \str_replace(['<n>', '<w>', '<g>'], [$line, $white, $green], $msg);
|
|
|
+ $msg = \str_replace(['</n>', '</w>', '</g>'], $end, $msg);
|
|
|
} elseif (!static::$_outputDecorated) {
|
|
|
return false;
|
|
|
}
|
|
|
@@ -2181,7 +2111,7 @@ class Worker
|
|
|
static::$_outputDecorated = false;
|
|
|
} else {
|
|
|
static::$_outputDecorated =
|
|
|
- static::$_OS === \OS_TYPE_LINUX &&
|
|
|
+ \DIRECTORY_SEPARATOR === '/' &&
|
|
|
\function_exists('posix_isatty') &&
|
|
|
\posix_isatty($stream);
|
|
|
}
|
|
|
@@ -2194,17 +2124,12 @@ class Worker
|
|
|
* @param string $socket_name
|
|
|
* @param array $context_option
|
|
|
*/
|
|
|
- public function __construct($socket_name = '', array $context_option = array())
|
|
|
+ public function __construct($socket_name = '', array $context_option = [])
|
|
|
{
|
|
|
// Save all worker instances.
|
|
|
$this->workerId = \spl_object_hash($this);
|
|
|
static::$_workers[$this->workerId] = $this;
|
|
|
- static::$_pidMap[$this->workerId] = array();
|
|
|
-
|
|
|
- // Get autoload root path.
|
|
|
- $backtrace = \debug_backtrace();
|
|
|
- $this->_autoloadRootPath = \dirname($backtrace[0]['file']);
|
|
|
- Autoloader::setRootPath($this->_autoloadRootPath);
|
|
|
+ static::$_pidMap[$this->workerId] = [];
|
|
|
|
|
|
// Context for socket.
|
|
|
if ($socket_name) {
|
|
|
@@ -2216,7 +2141,7 @@ class Worker
|
|
|
}
|
|
|
|
|
|
// Turn reusePort on.
|
|
|
- /*if (static::$_OS === \OS_TYPE_LINUX // if linux
|
|
|
+ /*if (\DIRECTORY_SEPARATOR === '/' // if linux
|
|
|
&& \version_compare(\PHP_VERSION,'7.0.0', 'ge') // if php >= 7.0.0
|
|
|
&& \version_compare(php_uname('r'), '3.9', 'ge') // if kernel >=3.9
|
|
|
&& \strtolower(\php_uname('s')) !== 'darwin' // if not Mac OS
|
|
|
@@ -2238,9 +2163,6 @@ class Worker
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // Autoload.
|
|
|
- Autoloader::setRootPath($this->_autoloadRootPath);
|
|
|
-
|
|
|
if (!$this->_mainSocket) {
|
|
|
|
|
|
$local_socket = $this->parseSocketAddress();
|
|
|
@@ -2329,7 +2251,9 @@ class Worker
|
|
|
throw new Exception('Bad worker->transport ' . \var_export($this->transport, true));
|
|
|
}
|
|
|
} else {
|
|
|
- $this->transport = $scheme;
|
|
|
+ if ($this->transport === 'tcp') {
|
|
|
+ $this->transport = $scheme;
|
|
|
+ }
|
|
|
}
|
|
|
//local socket
|
|
|
return static::$_builtinTransports[$this->transport] . ":" . $address;
|
|
|
@@ -2343,7 +2267,7 @@ class Worker
|
|
|
public function pauseAccept()
|
|
|
{
|
|
|
if (static::$globalEvent && false === $this->_pauseAccept && $this->_mainSocket) {
|
|
|
- static::$globalEvent->del($this->_mainSocket, EventInterface::EV_READ);
|
|
|
+ static::$globalEvent->offReadable($this->_mainSocket);
|
|
|
$this->_pauseAccept = true;
|
|
|
}
|
|
|
}
|
|
|
@@ -2358,9 +2282,9 @@ class Worker
|
|
|
// Register a listener to be notified when server socket is ready to read.
|
|
|
if (static::$globalEvent && true === $this->_pauseAccept && $this->_mainSocket) {
|
|
|
if ($this->transport !== 'udp') {
|
|
|
- static::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptConnection'));
|
|
|
+ static::$globalEvent->onReadable($this->_mainSocket, [$this, 'acceptTcpConnection']);
|
|
|
} else {
|
|
|
- static::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptUdpConnection'));
|
|
|
+ static::$globalEvent->onReadable($this->_mainSocket, [$this, 'acceptUdpConnection']);
|
|
|
}
|
|
|
$this->_pauseAccept = false;
|
|
|
}
|
|
|
@@ -2387,10 +2311,7 @@ class Worker
|
|
|
static::$_status = static::STATUS_RUNNING;
|
|
|
|
|
|
// Register shutdown function for checking errors.
|
|
|
- \register_shutdown_function(array("\\Workerman\\Worker", 'checkErrors'));
|
|
|
-
|
|
|
- // Set autoload root path.
|
|
|
- Autoloader::setRootPath($this->_autoloadRootPath);
|
|
|
+ \register_shutdown_function(["\\Workerman\\Worker", 'checkErrors']);
|
|
|
|
|
|
// Create a global event loop.
|
|
|
if (!static::$globalEvent) {
|
|
|
@@ -2415,12 +2336,8 @@ class Worker
|
|
|
// Try to emit onWorkerStart callback.
|
|
|
if ($this->onWorkerStart) {
|
|
|
try {
|
|
|
- \call_user_func($this->onWorkerStart, $this);
|
|
|
- } catch (\Exception $e) {
|
|
|
- // Avoid rapid infinite loop exit.
|
|
|
- sleep(1);
|
|
|
- static::stopAll(250, $e);
|
|
|
- } catch (\Error $e) {
|
|
|
+ ($this->onWorkerStart)($this);
|
|
|
+ } catch (\Throwable $e) {
|
|
|
// Avoid rapid infinite loop exit.
|
|
|
sleep(1);
|
|
|
static::stopAll(250, $e);
|
|
|
@@ -2428,7 +2345,7 @@ class Worker
|
|
|
}
|
|
|
|
|
|
// Main loop.
|
|
|
- static::$globalEvent->loop();
|
|
|
+ static::$globalEvent->run();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -2441,11 +2358,9 @@ class Worker
|
|
|
// Try to emit onWorkerStop callback.
|
|
|
if ($this->onWorkerStop) {
|
|
|
try {
|
|
|
- \call_user_func($this->onWorkerStop, $this);
|
|
|
- } catch (\Exception $e) {
|
|
|
- static::stopAll(250, $e);
|
|
|
- } catch (\Error $e) {
|
|
|
- static::stopAll(250, $e);
|
|
|
+ ($this->onWorkerStop)($this);
|
|
|
+ } catch (\Throwable $e) {
|
|
|
+ Worker::log($e);
|
|
|
}
|
|
|
}
|
|
|
// Remove listener for server socket.
|
|
|
@@ -2466,7 +2381,7 @@ class Worker
|
|
|
* @param resource $socket
|
|
|
* @return void
|
|
|
*/
|
|
|
- public function acceptConnection($socket)
|
|
|
+ public function acceptTcpConnection($socket)
|
|
|
{
|
|
|
// Accept a connection on server socket.
|
|
|
\set_error_handler(function(){});
|
|
|
@@ -2493,10 +2408,8 @@ class Worker
|
|
|
// Try to emit onConnect callback.
|
|
|
if ($this->onConnect) {
|
|
|
try {
|
|
|
- \call_user_func($this->onConnect, $connection);
|
|
|
- } catch (\Exception $e) {
|
|
|
- static::stopAll(250, $e);
|
|
|
- } catch (\Error $e) {
|
|
|
+ ($this->onConnect)($connection);
|
|
|
+ } catch (\Throwable $e) {
|
|
|
static::stopAll(250, $e);
|
|
|
}
|
|
|
}
|
|
|
@@ -2519,7 +2432,8 @@ class Worker
|
|
|
// UdpConnection.
|
|
|
$connection = new UdpConnection($socket, $remote_address);
|
|
|
$connection->protocol = $this->protocol;
|
|
|
- if ($this->onMessage) {
|
|
|
+ $message_cb = $this->onMessage;
|
|
|
+ if ($message_cb) {
|
|
|
try {
|
|
|
if ($this->protocol !== null) {
|
|
|
/** @var \Workerman\Protocols\ProtocolInterface $parser */
|
|
|
@@ -2532,24 +2446,24 @@ class Worker
|
|
|
$package = \substr($recv_buffer, 0, $len);
|
|
|
$recv_buffer = \substr($recv_buffer, $len);
|
|
|
$data = $parser::decode($package, $connection);
|
|
|
- if ($data === false)
|
|
|
+ if ($data === false) {
|
|
|
continue;
|
|
|
- \call_user_func($this->onMessage, $connection, $data);
|
|
|
+ }
|
|
|
+ $message_cb($connection, $data);
|
|
|
}
|
|
|
} else {
|
|
|
$data = $parser::decode($recv_buffer, $connection);
|
|
|
// Discard bad packets.
|
|
|
- if ($data === false)
|
|
|
+ if ($data === false) {
|
|
|
return true;
|
|
|
- \call_user_func($this->onMessage, $connection, $data);
|
|
|
+ }
|
|
|
+ $message_cb($connection, $data);
|
|
|
}
|
|
|
} else {
|
|
|
- \call_user_func($this->onMessage, $connection, $recv_buffer);
|
|
|
+ $message_cb($connection, $recv_buffer);
|
|
|
}
|
|
|
++ConnectionInterface::$statistics['total_request'];
|
|
|
- } catch (\Exception $e) {
|
|
|
- static::stopAll(250, $e);
|
|
|
- } catch (\Error $e) {
|
|
|
+ } catch (\Throwable $e) {
|
|
|
static::stopAll(250, $e);
|
|
|
}
|
|
|
}
|