Skip to content

Commit

Permalink
public $statusFile returned and default places to store files changed…
Browse files Browse the repository at this point in the history
… as before
  • Loading branch information
luzrain committed Nov 7, 2023
1 parent e1fec7b commit 93dfdfc
Showing 1 changed file with 44 additions and 38 deletions.
82 changes: 44 additions & 38 deletions src/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand All @@ -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.
Expand Down Expand Up @@ -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 '';
}
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 93dfdfc

Please sign in to comment.