Skip to content

Commit

Permalink
优化主进程对象的分配以及调试输出功能
Browse files Browse the repository at this point in the history
  • Loading branch information
breath-co2 committed Mar 28, 2017
1 parent 541ee09 commit 05464c8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
7 changes: 1 addition & 6 deletions src/Clusters/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,6 @@ protected function callbackFinish($taskId, $data, $workerName)
unset($this->taskCallbackList[$taskId]);
$callback(Server::$instance->server, $taskId, $data);
}
elseif ($workerName === Server::$instance->mainHostKey)
{
# 执行回调
Server::$instance->worker->onFinish(Server::$instance->server, $taskId, $data);
}
elseif (isset(Server::$instance->workers[$workerName]))
{
# 执行回调
Expand All @@ -515,7 +510,7 @@ protected function callbackFinish($taskId, $data, $workerName)
}
else
{
Server::$instance->warn("Task callback unknown worker type: $workerName");
Server::$instance->worker->onFinish(Server::$instance->server, $taskId, $data);
}
}
}
47 changes: 32 additions & 15 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ class Server
public $workerTask;

/**
* 当前主进程对象
* 主进程对象名称
*
* @var string
*/
public $defaultWorkerName = 'Main';

/**
* Main进程对象
*
* @var \WorkerMain|WorkerWebSocket|WorkerTCP|WorkerUDP|WorkerRedis
*/
Expand Down Expand Up @@ -115,32 +122,32 @@ class Server
];

/**
* 主服务器的 Host key
* 启动时间
*
* @var null
* @var int
*/
public $mainHostKey = null;
public $startTime;

/**
* 主服务器配置
* 启动时间,含毫秒
*
* @var array
* @var float
*/
public $mainHost = [];
public $startTimeFloat;

/**
* 启动时间
* 主服务器的 Host key
*
* @var int
* @var null
*/
public $startTime;
protected $mainHostKey = null;

/**
* 启动时间,含毫秒
* 主服务器配置
*
* @var float
* @var array
*/
public $startTimeFloat;
protected $mainHost = [];

/**
* 所有 Http 和 ws 服务列表
Expand Down Expand Up @@ -582,7 +589,7 @@ protected function initHosts()

if ($this->config['remote_shell']['open'])
{
$shell = $this->workers['_remoteShell'] = new RemoteShell($this->config['remote_shell']['public_keys']?: null);
$shell = $this->workers['_remoteShell'] = new RemoteShell($this->config['remote_shell']['public_key']?: null);
$rs = $shell->listen($this->server, $host = $this->config['remote_shell']['host'] ?: '127.0.0.1', $port = $this->config['remote_shell']['port']?: 9599);
if ($rs)
{
Expand All @@ -591,6 +598,7 @@ protected function initHosts()
else
{
$this->warn("RAdd remote shell tcp://$host:$port fail");
exit;
}
}
}
Expand Down Expand Up @@ -631,6 +639,8 @@ public function onWorkerStart($server, $workerId)
$this->workers['_Task'] = $this->workerTask;

$this->workerTask->onStart();

self::debug("TaskWorker#{$taskId} Started, pid: {$this->server->worker_pid}");
}
else
{
Expand Down Expand Up @@ -673,13 +683,15 @@ public function onWorkerStart($server, $workerId)
}
}
}
$this->worker = $this->workers[$this->mainHostKey];
$this->worker = $this->workers[$this->defaultWorkerName];

foreach ($this->workers as $worker)
{
# 调用初始化方法
$worker->onStart();
}

self::debug("Worker#{$workerId} Started, pid: {$this->server->worker_pid}");
}
}

Expand All @@ -692,6 +704,7 @@ public function onWorkerStop($server, $workerId)
if($server->taskworker)
{
$this->workerTask->onStop();
self::debug("TaskWorker#". ($workerId - $server->setting['worker_num']) ." Stopped, pid: {$this->server->worker_pid}");
}
else
{
Expand All @@ -702,6 +715,7 @@ public function onWorkerStop($server, $workerId)
*/
$worker->onStop();
}
self::debug("Worker#{$workerId} Stopped, pid: {$this->server->worker_pid}");
}
}

Expand Down Expand Up @@ -1154,6 +1168,9 @@ protected function checkConfig()
exit;
}

# 主对象名称
$this->defaultWorkerName = key($this->config['hosts']);

$mainHost = null;
foreach ($this->config['hosts'] as $key => & $hostConfig)
{
Expand Down
4 changes: 1 addition & 3 deletions src/Traits/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function sendMessage($data, $workerId, $serverId = -1, $serverGroup = nul

return true;
}
else if ($this->name !== static::$Server->mainHostKey || !is_string($data))
else if ($this !== static::$Server->worker || !is_string($data))
{
$obj = new \stdClass();
$obj->_sys = true;
Expand Down Expand Up @@ -167,7 +167,6 @@ public function sendMessageToAllWorker($data, $workerType = 0)
*/
public function onStop()
{
self::debug("Worker{$this->name}#{$this->id} Stop, pid: {$this->server->worker_pid}");
}

/**
Expand All @@ -176,7 +175,6 @@ public function onStop()
*/
public function onStart()
{
self::debug("Worker{$this->name}#{$this->id} Start, pid: {$this->server->worker_pid}");
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/WorkerHttpRangeUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function __construct(\Swoole\Server $server, $name)
});
}

if ($this->name != static::$Server->mainHostKey && ($this->id == 0 || SWOOLE_BASE == static::$Server->serverMode))
if ($this !== static::$Server->worker && ($this->id == 0 || SWOOLE_BASE == static::$Server->serverMode))
{
# SWOOLE_BASE 模式下只能获取当前进程的连接,所以需要每个进程都去遍历,其它模式会获取全部连接,所以只需要 $this->id = 0 的去遍历
# 移除不活跃的链接
Expand Down

0 comments on commit 05464c8

Please sign in to comment.