Skip to content

Commit

Permalink
修改数据格式
Browse files Browse the repository at this point in the history
  • Loading branch information
ywisax committed Dec 6, 2016
1 parent a752de9 commit 560bf46
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 49 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ swoole 用 C 写的, 看不懂... 那现在有用 PHP 实现的 workerman 了,

## 进行中的工作

* swoole 任务投递的优化
* 异步任务的逻辑优化
* 增加单元测试
* 兼容swoole

Expand Down Expand Up @@ -73,8 +73,6 @@ swoole 用 C 写的, 看不懂... 那现在有用 PHP 实现的 workerman 了,
return [
'workermanHttp' => [
'frontend' => [
'host' => '127.0.0.1',
'port' => '6677',
'root' => realpath(__DIR__ . '/../../frontend/web'),
// 在这里定义一些常用的可以常驻与内存的组件
'persistClasses' => [
Expand All @@ -95,9 +93,22 @@ return [
'bootstrapRefresh' => [
'xxx\backend\Bootstrap',
],
// 配置参考 http://doc3.workerman.net/worker-development/property.html
'global' => [
'host' => '127.0.0.1',
'port' => 6676,
],
'server' => [
'host' => '127.0.0.1',
'port' => 6677,
// 配置参考 http://doc3.workerman.net/worker-development/property.html
'count' => 4,
'name' => 'demo-http'
],
'task' => [
'host' => '127.0.0.1',
'port' => 6678,
'count' => 4,
'name' => 'demo-task',
],
'logFile' => __DIR__ . '/../runtime/workerman.log',
],
Expand Down
1 change: 1 addition & 0 deletions demo/controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ public function actionTask()
{
//Task::pushTask("var_dump", [time()]);
Task::pushTask("time");
return time();
}
}
11 changes: 10 additions & 1 deletion demo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,21 @@
],
// 有一些模块比较特殊, 无法实现Refreshable接口, 此时唯有在这里指定他的类名
'bootstrapRefresh' => [],
'global' => [
'host' => '127.0.0.1',
'port' => 6676,
],
'server' => [
'host' => '127.0.0.1',
'port' => 6677,
// 配置参考 http://doc3.workerman.net/worker-development/property.html
'count' => 4,
'name' => 'demo-http'
],
'task' => [
'count' => 4,
'host' => '127.0.0.1',
'port' => 6678,
'count' => 20,
'name' => 'demo-task',
],
];
Expand Down
90 changes: 46 additions & 44 deletions src/server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,43 +157,45 @@ public static function loadBootstrapFile($bootstrapFile)
}

/**
* 运行全局数据共享服务器
*
* @param array $config
* @param string $host
* @param bool $isDebug
*/
public static function runAppGlobalData($config, $host, $isDebug)
public static function runAppGlobalData($config, $isDebug)
{
$globalConfig = ArrayHelper::getValue($config, 'global');
if ( ! $globalConfig)
$globalConfig = (array) ArrayHelper::getValue($config, 'global');
if ($globalConfig)
{
$globalConfig = [
'port' => 2207,
];
$host = ArrayHelper::getValue($globalConfig, 'host', '127.0.0.1');
$port = ArrayHelper::getValue($globalConfig, 'port', 6676);
unset($globalConfig['host'], $globalConfig['port']);
$task = new GlobalServer([
'app' => Application::$workerApp,
'host' => $host,
'port' => $port,
'debug' => $isDebug,
]);
$task->run($globalConfig);
Application::$globalData = new Client("{$host}:{$port}");
}
$globalDataHost = ArrayHelper::getValue($globalConfig, 'host', $host);
$globalDataPort = ArrayHelper::getValue($globalConfig, 'port', 2207);
$task = new GlobalServer([
'app' => Application::$workerApp,
'host' => $globalDataHost,
'port' => $globalDataPort,
'debug' => $isDebug,
]);
$task->run($globalConfig);
Application::$globalData = new Client("{$globalDataHost}:{$globalDataPort}");
}

/**
* @param $config
* @param $host
* @param $port
* @param $root
* @param $isDebug
* 运行HTTP服务器
*
* @param array $config
* @param static $root
* @param bool $isDebug
*/
public static function runAppHttpServer($config, $host, $port, $root, $isDebug)
public static function runAppHttpServer($config, $root, $isDebug)
{
$serverConfig = ArrayHelper::getValue($config, 'server');
$serverConfig = (array) ArrayHelper::getValue($config, 'server');
if ($serverConfig)
{
$host = ArrayHelper::getValue($serverConfig, 'host', '127.0.0.1');
$port = ArrayHelper::getValue($serverConfig, 'port', 6677);
unset($serverConfig['host'], $serverConfig['port']);
/** @var HttpServer $server */
$server = new HttpServer([
'app' => Application::$workerApp,
Expand All @@ -207,26 +209,27 @@ public static function runAppHttpServer($config, $host, $port, $root, $isDebug)
}

/**
* @param $config
* @param $host
* @param $isDebug
* 运行任务处理服务器
*
* @param array $config
* @param bool $isDebug
*/
public static function runAppTaskServer($config, $host, $isDebug)
public static function runAppTaskServer($config, $isDebug)
{
$taskConfig = ArrayHelper::getValue($config, 'task');
if ( ! $taskConfig)
$taskConfig = (array) ArrayHelper::getValue($config, 'task');
if ($taskConfig)
{
$taskConfig = [
'port' => 2208,
];
$host = ArrayHelper::getValue($taskConfig, 'host', '127.0.0.1');
$port = ArrayHelper::getValue($taskConfig, 'port', 6678);
unset($taskConfig['host'], $taskConfig['port']);
$task = new TaskServer([
'app' => Application::$workerApp,
'host' => $host,
'port' => $port,
'debug' => $isDebug,
]);
$task->run($taskConfig);
}
$task = new TaskServer([
'app' => Application::$workerApp,
'host' => ArrayHelper::getValue($taskConfig, 'host', $host), // 默认跟http服务同一个主机名
'port' => ArrayHelper::getValue($taskConfig, 'port', 2208), // 默认任务使用的2208端口
'debug' => $isDebug,
]);
$task->run($taskConfig);
}

/**
Expand All @@ -253,16 +256,15 @@ final public static function runApp($app)

$root = ArrayHelper::getValue($config, 'root');
$host = ArrayHelper::getValue($config, 'host');
$port = ArrayHelper::getValue($config, 'port');

// 全局数据
self::runAppGlobalData($config, $host, $isDebug);
self::runAppGlobalData($config, $isDebug);

// 执行 HTTP SERVER
self::runAppHttpServer($config, $host, $port, $root, $isDebug);
self::runAppHttpServer($config, $root, $isDebug);

// 执行 TASK SERVER
self::runAppTaskServer($config, $host, $isDebug);
self::runAppTaskServer($config, $isDebug);

Worker::runAll();
}
Expand Down
5 changes: 5 additions & 0 deletions src/server/TaskServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class TaskServer extends Server
*/
public function run($config)
{
if (isset($config['interval']))
{
$this->timeInterval = $config['interval'];
unset($config['interval']);
}
$this->server = new Worker("Text://{$this->host}:{$this->port}");
foreach ($config as $k => $v)
{
Expand Down

0 comments on commit 560bf46

Please sign in to comment.