Skip to content

Commit 8bce7e2

Browse files
committed
实现队列组件
0 parents  commit 8bce7e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2676
-0
lines changed

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
* text=auto
2+
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
5+
/res export-ignore

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor
2+
/composer.lock
3+
/example/bin/server.pid

.travis.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
sudo: required
2+
language: php
3+
4+
matrix:
5+
include:
6+
- php: '7.2'
7+
env: SWOOLE_VERSION="v4.5.2"
8+
- php: '7.3'
9+
env: SWOOLE_VERSION="v4.5.2"
10+
- php: '7.4'
11+
env: SWOOLE_VERSION="v4.5.2"
12+
- php: '7.2'
13+
env: SWOOLE_VERSION="v4.4.19"
14+
- php: '7.3'
15+
env: SWOOLE_VERSION="v4.4.19"
16+
- php: '7.4'
17+
env: SWOOLE_VERSION="v4.4.19"
18+
- php: '7.2'
19+
env: SWOOLE_VERSION="master"
20+
- php: '7.3'
21+
env: SWOOLE_VERSION="master"
22+
- php: '7.4'
23+
env: SWOOLE_VERSION="master"
24+
- php: 'nightly'
25+
env: SWOOLE_VERSION="master" DISABLE_XDEBUG=false INSTALL_REDIS_EXT=true
26+
allow_failures:
27+
- env: SWOOLE_VERSION="master"
28+
- php: nightly
29+
30+
services:
31+
- redis-server
32+
33+
addons:
34+
apt:
35+
packages:
36+
- unzip
37+
38+
install:
39+
- if [[ $INSTALL_REDIS_EXT == true ]]; then
40+
wget https://github.com/phpredis/phpredis/archive/develop.zip;
41+
unzip develop.zip;
42+
cd phpredis-develop;
43+
phpize;
44+
./configure;
45+
make && make install;
46+
cd ../;
47+
fi
48+
- echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
49+
- wget https://github.com/swoole/swoole-src/archive/$SWOOLE_VERSION.tar.gz -O swoole.tar.gz && mkdir -p swoole && tar -xf swoole.tar.gz -C swoole --strip-components=1 && rm swoole.tar.gz && cd swoole && phpize && ./configure --enable-openssl --enable-http2 && make -j && make install && cd -
50+
- echo "extension = swoole.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
51+
52+
before_script:
53+
- if [[ $DISABLE_XDEBUG != false ]]; then
54+
phpenv config-rm xdebug.ini;
55+
fi
56+
- curl -sSfL -o ~/.phpenv/versions/$(phpenv version-name)/bin/phpunit https://phar.phpunit.de/phpunit-7.phar
57+
- export TEST_PHPUNIT_PATH=~/.phpenv/versions/$(phpenv version-name)/bin/phpunit
58+
- php ./.travis/composer-no-phpunit.php
59+
- composer update
60+
- php --ri swoole
61+
62+
script:
63+
- ./tests/run
64+
65+
after_failure:
66+
- php ./.travis/output-server-log.php
67+

.travis/composer-no-phpunit.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
$composerJson = json_decode(file_get_contents(dirname(__DIR__) . '/composer.json'));
3+
unset($composerJson->{"require-dev"}->{"phpunit/phpunit"});
4+
file_put_contents(dirname(__DIR__) . '/composer.json', json_encode($composerJson));

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 imi 开发组
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
# imi-queue
2+
3+
[![Latest Version](https://img.shields.io/packagist/v/imiphp/imi-queue.svg)](https://packagist.org/packages/imiphp/imi-queue)
4+
[![Php Version](https://img.shields.io/badge/php-%3E=7.2-brightgreen.svg)](https://secure.php.net/)
5+
[![Swoole Version](https://img.shields.io/badge/swoole-%3E=4.4.0-brightgreen.svg)](https://github.com/swoole/swoole-src)
6+
[![IMI License](https://img.shields.io/github/license/imiphp/imi-queue.svg)](https://github.com/imiphp/imi-queue/blob/master/LICENSE)
7+
8+
## 介绍
9+
10+
imi 框架的队列组件,使用 Redis 作为持久化
11+
12+
支持的特性:
13+
14+
- 消息持久化
15+
- 分布式
16+
- 延时消息
17+
- ACK 机制
18+
- 消费超时机制
19+
- 失败/超时消息重新消费
20+
21+
## Composer
22+
23+
本项目可以使用composer安装,遵循psr-4自动加载规则,在你的 `composer.json` 中加入下面的内容:
24+
25+
```json
26+
{
27+
"require": {
28+
"imiphp/imi-queue": "^1.0.0"
29+
}
30+
}
31+
```
32+
33+
然后执行 `composer update` 安装。
34+
35+
## 使用说明
36+
37+
> 可以参考 `example``tests` 目录示例。
38+
39+
项目配置文件:
40+
41+
```php
42+
[
43+
'components' => [
44+
'Queue' => 'Imi\Queue',
45+
],
46+
'beans' => [
47+
'AutoRunProcessManager' => [
48+
'processes' => [
49+
// 加入队列消费进程,非必须,你也可以自己写进程消费
50+
'QueueConsumer',
51+
],
52+
],
53+
'imiQueue' => [
54+
// 默认队列
55+
'default' => 'test1',
56+
// 队列列表
57+
'list' => [
58+
// 队列名称
59+
'test1' => [
60+
// 使用的队列驱动
61+
'driver' => \Imi\Queue\Driver\RedisQueueDriver::class,
62+
// 消费协程数量
63+
'co' => 1,
64+
// 消费进程数量;可能会受进程分组影响,以同一组中配置的最多进程数量为准
65+
'process' => 1,
66+
// 消费循环尝试 pop 的时间间隔,单位:秒
67+
'timespan' => 0.1,
68+
// 进程分组名称
69+
'processGroup' => 'a',
70+
// 自动消费
71+
'autoConsumer' => true,
72+
// 消费者类
73+
'consumer' => 'AConsumer',
74+
// 驱动类所需要的参数数组
75+
'config' => [
76+
'poolName' => 'redis',
77+
'prefix' => 'imi:queue:test:',
78+
]
79+
],
80+
],
81+
],
82+
]
83+
]
84+
```
85+
86+
### 获取队列对象
87+
88+
```php
89+
use \Imi\Queue\Facade\Queue;
90+
$queue = Queue::getQueue('队列名称');
91+
```
92+
93+
### 推送消息到队列
94+
95+
返回消息ID
96+
97+
```php
98+
$message = new \Imi\Queue\Model\Message;
99+
$message->setMessage('字符串的消息内容');
100+
$message->setWorkingTimeout(0); // 设置工作超时时间,单位:秒,为0不限制
101+
$queue->push($message);
102+
// 延时消息,单位:秒
103+
$queue->push($message, 1.5);
104+
```
105+
106+
### 从队列弹出一个消息
107+
108+
```php
109+
$message = $queue->pop();
110+
if(null !== $message)
111+
{
112+
// 将消息标记为成功
113+
$queue->success($message);
114+
115+
// 将消息标记为失败
116+
$queue->fail($message);
117+
}
118+
```
119+
120+
### 删除消息
121+
122+
```php
123+
$message = $queue->pop();
124+
if(null !== $message)
125+
{
126+
$queue->delete($message);
127+
}
128+
```
129+
130+
### 清空队列
131+
132+
```php
133+
use \Imi\Queue\Enum\QueueType;
134+
135+
$queue->clear(); // 清空全部
136+
137+
// 清空指定类型
138+
$queue->clear([
139+
QueueType::READY, // 准备就绪
140+
QueueType::WORKING, // 工作中
141+
QueueType::FAIL, // 失败
142+
QueueType::TIMEOUT, // 超时
143+
QueueType::DELAY, // 准备就绪延时
144+
]);
145+
```
146+
147+
### 获取队列状态
148+
149+
```php
150+
// 返回 \Imi\Queue\Model\QueueStatus 类型
151+
$status = $queue->status();
152+
$status->getReady(); // 准备就绪数量
153+
$status->getWorking(); // 工作中数量
154+
$status->getFail(); // 失败数量
155+
$status->getTimeout(); // 超时数量
156+
$status->getDelay(); // 延时数量
157+
```
158+
159+
### 将失败消息恢复到队列
160+
161+
让失败消息可以被重新消费
162+
163+
```php
164+
$queue->restoreFailMessages();
165+
```
166+
167+
### 将超时消息恢复到队列
168+
169+
让超时消息可以被重新消费
170+
171+
```php
172+
$queue->restoreTimeoutMessages();
173+
```
174+
175+
## 命令行工具
176+
177+
### 获取队列状态
178+
179+
命令:`bin/imi queue/status -queue 队列名称`
180+
181+
返回 JSON:
182+
183+
```js
184+
{
185+
"ready": 0,
186+
"working": 0,
187+
"fail": 0,
188+
"timeout": 0,
189+
"delay": 0
190+
}
191+
```
192+
193+
### 将失败消息恢复到队列
194+
195+
命令:`bin/imi queue/restoreFail -queue 队列名称`
196+
197+
返回恢复的消息数量:
198+
199+
```js
200+
0
201+
```
202+
203+
### 将超时消息恢复到队列
204+
205+
命令:`bin/imi queue/restoreTimeout -queue 队列名称`
206+
207+
返回恢复的消息数量:
208+
209+
```js
210+
0
211+
```
212+
213+
## 免费技术支持
214+
215+
QQ群:17916227 [![点击加群](https://pub.idqqimg.com/wpa/images/group.png "点击加群")](https://jq.qq.com/?_wv=1027&k=5wXf4Zq),如有问题会有人解答和修复。
216+
217+
## 运行环境
218+
219+
- [PHP](https://php.net/) >= 7.1
220+
- [Composer](https://getcomposer.org/)
221+
- [Swoole](https://www.swoole.com/) >= 4.4.0
222+
223+
## 版权信息
224+
225+
`imi-queue` 遵循 MIT 开源协议发布,并提供免费使用。
226+
227+
## 捐赠
228+
229+
<img src="https://raw.githubusercontent.com/imiphp/imi-queue/master/res/pay.png"/>
230+
231+
开源不求盈利,多少都是心意,生活不易,随缘随缘……

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "imiphp/imi-queue",
3+
"type": "library",
4+
"license": "MIT",
5+
"description": "imi 框架的队列组件,使用 Redis 作为持久化",
6+
"require": {
7+
8+
},
9+
"require-dev": {
10+
"yurunsoft/imi": "dev-dev",
11+
"phpunit/phpunit": "^7",
12+
"yurunsoft/ide-helper": "~1.0"
13+
},
14+
"autoload": {
15+
"psr-4" : {
16+
"Imi\\Queue\\" : "src/"
17+
}
18+
},
19+
"autoload-dev": {
20+
"psr-4" : {
21+
"ImiApp\\" : "example/",
22+
"Imi\\Queue\\Test\\" : "tests/"
23+
}
24+
},
25+
"scripts": {
26+
"test": "./tests/run",
27+
"install-test": "php --ri swoole && composer install composer test"
28+
},
29+
"extra": {
30+
"ide-helper": {
31+
"list": [
32+
"swoole",
33+
"redis"
34+
]
35+
}
36+
}
37+
}

example/.env

Whitespace-only changes.

example/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.lock
2+
/.runtime
3+
/vendor

0 commit comments

Comments
 (0)