Skip to content

Commit 0aa5756

Browse files
author
ywisax
committed
改用redis来做队列的存储
1 parent 560bf46 commit 0aa5756

File tree

10 files changed

+18
-145
lines changed

10 files changed

+18
-145
lines changed

README.md

-4
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ return [
9393
'bootstrapRefresh' => [
9494
'xxx\backend\Bootstrap',
9595
],
96-
'global' => [
97-
'host' => '127.0.0.1',
98-
'port' => 6676,
99-
],
10096
'server' => [
10197
'host' => '127.0.0.1',
10298
'port' => 6677,

composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"require": {
1111
"php": ">=5.6.0",
1212
"workerman/workerman": "^3.3.4",
13-
"workerman/globaldata": "^1.0",
1413
"yiisoft/yii2": "^2.0",
1514
"yiisoft/yii2-bootstrap": "~2.0.0",
1615
"yiisoft/yii2-swiftmailer": "~2.0.0",

demo/controllers/ApiController.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public function actionGetUsers()
5050
public function actionTask()
5151
{
5252
//Task::pushTask("var_dump", [time()]);
53-
Task::pushTask("time");
53+
//Task::pushTask("time");
54+
Task::pushTask('sleep', [10]);
5455
return time();
5556
}
5657
}

demo/index.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
require_once __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
88

99
Yii::$app->params['workermanHttp']['demo'] = [
10-
'host' => '127.0.0.1',
11-
'port' => '6688',
1210
'root' => __DIR__,
1311
'debug' => false,
1412
// bootstrap文件, 只会引入一次
@@ -21,10 +19,6 @@
2119
],
2220
// 有一些模块比较特殊, 无法实现Refreshable接口, 此时唯有在这里指定他的类名
2321
'bootstrapRefresh' => [],
24-
'global' => [
25-
'host' => '127.0.0.1',
26-
'port' => 6676,
27-
],
2822
'server' => [
2923
'host' => '127.0.0.1',
3024
'port' => 6677,
@@ -35,7 +29,7 @@
3529
'task' => [
3630
'host' => '127.0.0.1',
3731
'port' => 6678,
38-
'count' => 20,
32+
'count' => 1,
3933
'name' => 'demo-task',
4034
],
4135
];

src/Application.php

-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace tourze\workerman\yii2;
44

5-
use tourze\workerman\yii2\globalData\Client;
65
use tourze\workerman\yii2\web\ErrorHandler;
76
use tourze\workerman\yii2\web\Request;
87
use tourze\workerman\yii2\web\Response;
@@ -56,11 +55,6 @@ public static function getGlobalConfig()
5655
*/
5756
public static $workerApp = null;
5857

59-
/**
60-
* @var Client
61-
*/
62-
public static $globalData = null;
63-
6458
/**
6559
* @var Worker 当前运行中的服务器实例
6660
*/

src/async/Task.php

+15-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace tourze\workerman\yii2\async;
44

5-
use tourze\workerman\yii2\Application;
5+
use Yii;
6+
use yii\redis\Connection;
67

78
/**
89
* 使用
@@ -22,6 +23,14 @@ class Task
2223
*/
2324
public static $taskCountKey = 'task_count';
2425

26+
/**
27+
* @return Connection
28+
*/
29+
public static function getRedis()
30+
{
31+
return Yii::$app->get('redis');
32+
}
33+
2534
/**
2635
* 打包数据
2736
*
@@ -56,13 +65,9 @@ public static function unpackData($data)
5665
*/
5766
public static function pushTask($function, $params = [])
5867
{
59-
if ( ! Application::$globalData)
60-
{
61-
return 0;
62-
}
6368
$data = self::packData($function, $params);
64-
Application::$globalData->push(self::$taskQueueKey, $data);
65-
$taskId = Application::$globalData->increment(self::$taskCountKey);
69+
self::getRedis()->rpush(self::$taskQueueKey, $data);
70+
$taskId = self::getRedis()->incr(self::$taskCountKey);
6671
return $taskId;
6772
}
6873

@@ -73,20 +78,17 @@ public static function pushTask($function, $params = [])
7378
*/
7479
public static function popTask()
7580
{
76-
if ( ! Application::$globalData)
77-
{
78-
return '';
79-
}
80-
return Application::$globalData->pop(self::$taskQueueKey);
81+
return self::getRedis()->lpop(self::$taskQueueKey);
8182
}
8283

8384
/**
8485
* 执行任务
8586
*
8687
* @param string $data
88+
* @param int $taskId
8789
* @return mixed
8890
*/
89-
public static function runTask($data)
91+
public static function runTask($data, $taskId = null)
9092
{
9193
$data = self::unpackData($data);
9294
$function = array_shift($data);

src/globalData/Client.php

-59
This file was deleted.

src/globalData/Server.php

-7
This file was deleted.

src/server/GlobalServer.php

-17
This file was deleted.

src/server/Server.php

-30
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use tourze\workerman\yii2\Application;
66
use tourze\workerman\yii2\async\Task;
77
use tourze\workerman\yii2\Container;
8-
use tourze\workerman\yii2\globalData\Client;
98
use tourze\workerman\yii2\log\Logger;
109
use Workerman\Worker;
1110
use Yii;
@@ -156,31 +155,6 @@ public static function loadBootstrapFile($bootstrapFile)
156155
}
157156
}
158157

159-
/**
160-
* 运行全局数据共享服务器
161-
*
162-
* @param array $config
163-
* @param bool $isDebug
164-
*/
165-
public static function runAppGlobalData($config, $isDebug)
166-
{
167-
$globalConfig = (array) ArrayHelper::getValue($config, 'global');
168-
if ($globalConfig)
169-
{
170-
$host = ArrayHelper::getValue($globalConfig, 'host', '127.0.0.1');
171-
$port = ArrayHelper::getValue($globalConfig, 'port', 6676);
172-
unset($globalConfig['host'], $globalConfig['port']);
173-
$task = new GlobalServer([
174-
'app' => Application::$workerApp,
175-
'host' => $host,
176-
'port' => $port,
177-
'debug' => $isDebug,
178-
]);
179-
$task->run($globalConfig);
180-
Application::$globalData = new Client("{$host}:{$port}");
181-
}
182-
}
183-
184158
/**
185159
* 运行HTTP服务器
186160
*
@@ -255,10 +229,6 @@ final public static function runApp($app)
255229
$isDebug = ArrayHelper::getValue($config, 'debug', false);
256230

257231
$root = ArrayHelper::getValue($config, 'root');
258-
$host = ArrayHelper::getValue($config, 'host');
259-
260-
// 全局数据
261-
self::runAppGlobalData($config, $isDebug);
262232

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

0 commit comments

Comments
 (0)