Skip to content

Commit

Permalink
改用redis来做队列的存储
Browse files Browse the repository at this point in the history
  • Loading branch information
ywisax committed Dec 6, 2016
1 parent 560bf46 commit 0aa5756
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 145 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ return [
'bootstrapRefresh' => [
'xxx\backend\Bootstrap',
],
'global' => [
'host' => '127.0.0.1',
'port' => 6676,
],
'server' => [
'host' => '127.0.0.1',
'port' => 6677,
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"require": {
"php": ">=5.6.0",
"workerman/workerman": "^3.3.4",
"workerman/globaldata": "^1.0",
"yiisoft/yii2": "^2.0",
"yiisoft/yii2-bootstrap": "~2.0.0",
"yiisoft/yii2-swiftmailer": "~2.0.0",
Expand Down
3 changes: 2 additions & 1 deletion demo/controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public function actionGetUsers()
public function actionTask()
{
//Task::pushTask("var_dump", [time()]);
Task::pushTask("time");
//Task::pushTask("time");
Task::pushTask('sleep', [10]);
return time();
}
}
8 changes: 1 addition & 7 deletions demo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
require_once __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';

Yii::$app->params['workermanHttp']['demo'] = [
'host' => '127.0.0.1',
'port' => '6688',
'root' => __DIR__,
'debug' => false,
// bootstrap文件, 只会引入一次
Expand All @@ -21,10 +19,6 @@
],
// 有一些模块比较特殊, 无法实现Refreshable接口, 此时唯有在这里指定他的类名
'bootstrapRefresh' => [],
'global' => [
'host' => '127.0.0.1',
'port' => 6676,
],
'server' => [
'host' => '127.0.0.1',
'port' => 6677,
Expand All @@ -35,7 +29,7 @@
'task' => [
'host' => '127.0.0.1',
'port' => 6678,
'count' => 20,
'count' => 1,
'name' => 'demo-task',
],
];
Expand Down
6 changes: 0 additions & 6 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace tourze\workerman\yii2;

use tourze\workerman\yii2\globalData\Client;
use tourze\workerman\yii2\web\ErrorHandler;
use tourze\workerman\yii2\web\Request;
use tourze\workerman\yii2\web\Response;
Expand Down Expand Up @@ -56,11 +55,6 @@ public static function getGlobalConfig()
*/
public static $workerApp = null;

/**
* @var Client
*/
public static $globalData = null;

/**
* @var Worker 当前运行中的服务器实例
*/
Expand Down
28 changes: 15 additions & 13 deletions src/async/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace tourze\workerman\yii2\async;

use tourze\workerman\yii2\Application;
use Yii;
use yii\redis\Connection;

/**
* 使用
Expand All @@ -22,6 +23,14 @@ class Task
*/
public static $taskCountKey = 'task_count';

/**
* @return Connection
*/
public static function getRedis()
{
return Yii::$app->get('redis');
}

/**
* 打包数据
*
Expand Down Expand Up @@ -56,13 +65,9 @@ public static function unpackData($data)
*/
public static function pushTask($function, $params = [])
{
if ( ! Application::$globalData)
{
return 0;
}
$data = self::packData($function, $params);
Application::$globalData->push(self::$taskQueueKey, $data);
$taskId = Application::$globalData->increment(self::$taskCountKey);
self::getRedis()->rpush(self::$taskQueueKey, $data);
$taskId = self::getRedis()->incr(self::$taskCountKey);
return $taskId;
}

Expand All @@ -73,20 +78,17 @@ public static function pushTask($function, $params = [])
*/
public static function popTask()
{
if ( ! Application::$globalData)
{
return '';
}
return Application::$globalData->pop(self::$taskQueueKey);
return self::getRedis()->lpop(self::$taskQueueKey);
}

/**
* 执行任务
*
* @param string $data
* @param int $taskId
* @return mixed
*/
public static function runTask($data)
public static function runTask($data, $taskId = null)
{
$data = self::unpackData($data);
$function = array_shift($data);
Expand Down
59 changes: 0 additions & 59 deletions src/globalData/Client.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/globalData/Server.php

This file was deleted.

17 changes: 0 additions & 17 deletions src/server/GlobalServer.php

This file was deleted.

30 changes: 0 additions & 30 deletions src/server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use tourze\workerman\yii2\Application;
use tourze\workerman\yii2\async\Task;
use tourze\workerman\yii2\Container;
use tourze\workerman\yii2\globalData\Client;
use tourze\workerman\yii2\log\Logger;
use Workerman\Worker;
use Yii;
Expand Down Expand Up @@ -156,31 +155,6 @@ public static function loadBootstrapFile($bootstrapFile)
}
}

/**
* 运行全局数据共享服务器
*
* @param array $config
* @param bool $isDebug
*/
public static function runAppGlobalData($config, $isDebug)
{
$globalConfig = (array) ArrayHelper::getValue($config, 'global');
if ($globalConfig)
{
$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}");
}
}

/**
* 运行HTTP服务器
*
Expand Down Expand Up @@ -255,10 +229,6 @@ final public static function runApp($app)
$isDebug = ArrayHelper::getValue($config, 'debug', false);

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

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

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

0 comments on commit 0aa5756

Please sign in to comment.