From 93dfdfca0a2db878411a74bf3df6b6329f71b1a5 Mon Sep 17 00:00:00 2001 From: Anton Date: Wed, 8 Nov 2023 00:19:59 +0800 Subject: [PATCH] public $statusFile returned and default places to store files changed as before --- src/Worker.php | 82 +++++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/src/Worker.php b/src/Worker.php index 20b3271e..f284b05a 100644 --- a/src/Worker.php +++ b/src/Worker.php @@ -281,14 +281,7 @@ class Worker * * @var string */ - public static string $processStatusFile; - - /** - * The file used to store connections status. - * - * @var string - */ - public static string $connectionStatusFile; + public static string $statusFile; /** * Log file. @@ -468,6 +461,20 @@ class Worker */ protected static int $maxStateNameLength = 1; + /** + * The file to store status info of current worker process. + * + * @var string + */ + protected static string $statisticsFile; + + /** + * The file to store status info of connections. + * + * @var string + */ + protected static string $connectionsFile; + /** * Start file. * @@ -645,13 +652,12 @@ protected static function init(): void $startFilePrefix = hash('xxh64', static::$startFile); // Pid file. - static::$pidFile ??= sprintf('%s/workerman.%s.pid', sys_get_temp_dir(), $startFilePrefix); - - // Process status file. - static::$processStatusFile ??= sprintf('%s/workerman.%s.process.status', sys_get_temp_dir(), $startFilePrefix); + static::$pidFile ??= sprintf('%s/workerman.%s.pid', dirname(__DIR__), $startFilePrefix); - // Connections status file. - static::$connectionStatusFile ??= sprintf('%s/workerman.%s.connection.status', sys_get_temp_dir(), $startFilePrefix); + // Status file. + static::$statusFile ??= sprintf('%s/workerman.%s.status', dirname(__DIR__), $startFilePrefix); + static::$statisticsFile ??= static::$statusFile; + static::$connectionsFile ??= static::$statusFile . '.connection'; // Log file. static::$logFile ??= sprintf('%s/workerman.log', dirname(__DIR__, 2)); @@ -1021,7 +1027,7 @@ protected static function parseCommand(): void break; case 'status': // Delete status file on shutdown - register_shutdown_function(unlink(...), static::$processStatusFile); + register_shutdown_function(unlink(...), static::$statisticsFile); while (1) { // Master process will send SIGIOT signal to all child processes. posix_kill($masterPid, SIGIOT); @@ -1040,7 +1046,7 @@ protected static function parseCommand(): void } case 'connections': // Delete status file on shutdown - register_shutdown_function(unlink(...), static::$connectionStatusFile); + register_shutdown_function(unlink(...), static::$connectionsFile); // Master process will send SIGIO signal to all child processes. posix_kill($masterPid, SIGIO); // Waiting a moment. @@ -1121,10 +1127,10 @@ public static function getArgv(): array protected static function formatProcessStatusData(): string { static $totalRequestCache = []; - if (!is_readable(static::$processStatusFile)) { + if (!is_readable(static::$statisticsFile)) { return ''; } - $info = file(static::$processStatusFile, FILE_IGNORE_NEW_LINES); + $info = file(static::$statisticsFile, FILE_IGNORE_NEW_LINES); if (!$info) { return ''; } @@ -1202,7 +1208,7 @@ protected static function formatProcessStatusData(): string protected static function formatConnectionStatusData(): string { - return file_get_contents(static::$connectionStatusFile); + return file_get_contents(static::$connectionsFile); } /** @@ -1993,43 +1999,43 @@ protected static function writeStatisticsToStatusFile(): void $allWorkerInfo[$pid] = ['name' => $worker->name, 'listen' => $worker->getSocketName()]; } } - file_put_contents(static::$processStatusFile, ''); - chmod(static::$processStatusFile, 0722); - file_put_contents(static::$processStatusFile, serialize($allWorkerInfo) . "\n", FILE_APPEND); + file_put_contents(static::$statisticsFile, ''); + chmod(static::$statisticsFile, 0722); + file_put_contents(static::$statisticsFile, serialize($allWorkerInfo) . "\n", FILE_APPEND); $loadavg = function_exists('sys_getloadavg') ? array_map(round(...), sys_getloadavg(), [2, 2, 2]) : ['-', '-', '-']; - file_put_contents(static::$processStatusFile, + file_put_contents(static::$statisticsFile, "----------------------------------------------GLOBAL STATUS----------------------------------------------------\n", FILE_APPEND); - file_put_contents(static::$processStatusFile, + file_put_contents(static::$statisticsFile, 'Workerman version:' . static::VERSION . " PHP version:" . PHP_VERSION . "\n", FILE_APPEND); - file_put_contents(static::$processStatusFile, '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", FILE_APPEND); $loadStr = 'load average: ' . implode(", ", $loadavg); - file_put_contents(static::$processStatusFile, + file_put_contents(static::$statisticsFile, str_pad($loadStr, 33) . 'event-loop:' . static::getEventLoopName() . "\n", FILE_APPEND); - file_put_contents(static::$processStatusFile, + file_put_contents(static::$statisticsFile, count(static::$pidMap) . ' workers ' . count(static::getAllWorkerPids()) . " processes\n", FILE_APPEND); - file_put_contents(static::$processStatusFile, + file_put_contents(static::$statisticsFile, str_pad('worker_name', static::$maxWorkerNameLength) . " exit_status exit_count\n", FILE_APPEND); foreach (static::$pidMap as $workerId => $workerPidArray) { $worker = static::$workers[$workerId]; if (isset(static::$globalStatistics['worker_exit_info'][$workerId])) { foreach (static::$globalStatistics['worker_exit_info'][$workerId] as $workerExitStatus => $workerExitCount) { - file_put_contents(static::$processStatusFile, + file_put_contents(static::$statisticsFile, str_pad($worker->name, static::$maxWorkerNameLength) . " " . str_pad((string)$workerExitStatus, 16) . " $workerExitCount\n", FILE_APPEND); } } else { - file_put_contents(static::$processStatusFile, + file_put_contents(static::$statisticsFile, str_pad($worker->name, static::$maxWorkerNameLength) . " " . str_pad('0', 16) . " 0\n", FILE_APPEND); } } - file_put_contents(static::$processStatusFile, + file_put_contents(static::$statisticsFile, "----------------------------------------------PROCESS STATUS---------------------------------------------------\n", FILE_APPEND); - file_put_contents(static::$processStatusFile, + file_put_contents(static::$statisticsFile, "pid\tmemory " . str_pad('listening', static::$maxSocketNameLength) . " " . str_pad('worker_name', static::$maxWorkerNameLength) . " connections " . str_pad('send_fail', 9) . " " . str_pad('timers', 8) . str_pad('total_request', 13) . " qps status\n", FILE_APPEND); @@ -2054,7 +2060,7 @@ protected static function writeStatisticsToStatusFile(): void . " " . str_pad((string)ConnectionInterface::$statistics['send_fail'], 9) . " " . str_pad((string)static::$globalEvent->getTimerCount(), 7) . " " . str_pad((string)ConnectionInterface::$statistics['total_request'], 13) . "\n"; - file_put_contents(static::$processStatusFile, $workerStatusStr, FILE_APPEND); + file_put_contents(static::$statisticsFile, $workerStatusStr, FILE_APPEND); } /** @@ -2066,10 +2072,10 @@ protected static function writeConnectionsStatisticsToStatusFile(): void { // For master process. if (static::$masterPid === posix_getpid()) { - file_put_contents(static::$connectionStatusFile, ''); - chmod(static::$connectionStatusFile, 0722); - file_put_contents(static::$connectionStatusFile, "--------------------------------------------------------------------- WORKERMAN CONNECTION STATUS --------------------------------------------------------------------------------\n", FILE_APPEND); - file_put_contents(static::$connectionStatusFile, "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::$connectionsFile, ''); + chmod(static::$connectionsFile, 0722); + file_put_contents(static::$connectionsFile, "--------------------------------------------------------------------- WORKERMAN CONNECTION STATUS --------------------------------------------------------------------------------\n", FILE_APPEND); + file_put_contents(static::$connectionsFile, "PID Worker CID Trans Protocol ipv4 ipv6 Recv-Q Send-Q Bytes-R Bytes-W Status Local Address Foreign Address\n", FILE_APPEND); foreach (static::getAllWorkerPids() as $workerPid) { posix_kill($workerPid, SIGIO); } @@ -2130,7 +2136,7 @@ protected static function writeConnectionsStatisticsToStatusFile(): void . str_pad($state, 14) . ' ' . str_pad($localAddress, 22) . ' ' . str_pad($remoteAddress, 22) . "\n"; } if ($str) { - file_put_contents(static::$connectionStatusFile, $str, FILE_APPEND); + file_put_contents(static::$connectionsFile, $str, FILE_APPEND); } }