Skip to content

Commit

Permalink
Optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor committed Nov 23, 2024
1 parent 517ef66 commit 9157560
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions src/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ protected static function initWorkers(): void

// Event-loop name.
$eventLoopName = $worker->eventLoop ?: static::$eventLoopClass;
$worker->context->eventLoopName = str_starts_with($eventLoopName, 'Workerman\\Events\\') ? strtolower(substr($eventLoopName, 17)) : $eventLoopName;
$worker->context->eventLoopName = strtolower(substr($eventLoopName, strrpos($eventLoopName, '\\') + 1));

// Status name.
$worker->context->statusState = '<g> [OK] </g>';
Expand Down Expand Up @@ -1004,7 +1004,7 @@ protected static function getVersionLine(): string
public static function getUiColumns(): array
{
return [
'event' => 'eventLoopName',
'event-loop' => 'eventLoopName',
'proto' => 'transport',
'user' => 'user',
'worker' => 'name',
Expand Down Expand Up @@ -1048,7 +1048,7 @@ protected static function parseCommand(): void

// Check argv;
$startFile = basename(static::$startFile);
$usage = "Usage: php yourfile <command> [mode]\nCommands: \nstart\t\tStart worker in USER 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";
$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";
$availableCommands = [
'start',
'stop',
Expand Down Expand Up @@ -1081,7 +1081,7 @@ protected static function parseCommand(): void
if ($mode === '-d' || static::$daemonize) {
$modeStr = 'in DAEMON mode';
} else {
$modeStr = 'in USER mode';
$modeStr = 'in DEBUG mode';
}
}
static::log("Workerman[$startFile] $command $modeStr");
Expand Down Expand Up @@ -1344,12 +1344,12 @@ protected static function signalHandler(int $signal): void
case SIGHUP:
case SIGTSTP:
static::$gracefulStop = false;
static::stopAll(0, "received signal $signal");
static::stopAll(0, 'received signal ' . static::getSignalName($signal));
break;
// Graceful stop.
case SIGQUIT:
static::$gracefulStop = true;
static::stopAll(0, "received signal $signal");
static::stopAll(0, 'received signal ' . static::getSignalName($signal));
break;
// Reload.
case SIGUSR2:
Expand All @@ -1372,6 +1372,28 @@ protected static function signalHandler(int $signal): void
}
}

/**
* Get signal name.
*
* @param int $signal
* @return string
*/
protected static function getSignalName(int $signal): string
{
return match ($signal) {
SIGINT => 'SIGINT',
SIGTERM => 'SIGTERM',
SIGHUP => 'SIGHUP',
SIGTSTP => 'SIGTSTP',
SIGQUIT => 'SIGQUIT',
SIGUSR1 => 'SIGUSR1',
SIGUSR2 => 'SIGUSR2',
SIGIOT => 'SIGIOT',
SIGIO => 'SIGIO',
default => $signal,
};
}

/**
* Run as daemon mode.
*/
Expand Down Expand Up @@ -2084,20 +2106,20 @@ protected static function writeStatisticsToStatusFile(): void
count(static::$pidMap) . ' workers ' . count(static::getAllWorkerPids()) . " processes\n",
FILE_APPEND);
file_put_contents(static::$statisticsFile,
str_pad('name', static::getUiColumnLength('maxWorkerNameLength')) . " event exit_status exit_count\n", FILE_APPEND);
str_pad('name', static::getUiColumnLength('maxWorkerNameLength')) . " event-loop 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::$statisticsFile,
str_pad($worker->name, static::getUiColumnLength('maxWorkerNameLength')) . " " .
str_pad($worker->context->eventLoopName, 12) . " " .
str_pad($worker->context->eventLoopName, 14) . " " .
str_pad((string)$workerExitStatus, 16) . str_pad((string)$workerExitCount, 16) . "\n", FILE_APPEND);
}
} else {
file_put_contents(static::$statisticsFile,
str_pad($worker->name, static::getUiColumnLength('maxWorkerNameLength')) . " " .
str_pad($worker->context->eventLoopName, 12) . " " .
str_pad($worker->context->eventLoopName, 14) . " " .
str_pad('0', 16) . str_pad('0', 16) . "\n", FILE_APPEND);
}
}
Expand Down

0 comments on commit 9157560

Please sign in to comment.