Skip to content

Commit a816494

Browse files
committed
feat: 实现delay-queue所有API接口
1 parent 314bc67 commit a816494

20 files changed

+461
-60
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33

44
# composer
55
composer.lock
6-
vendor
6+
vendor
7+
8+
# node
9+
node_modules

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
language: php
22

33
php:
4+
- 5.4
45
- 5.5
56
- 5.6
67
- 7.0

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# delayqueue-php
22
[延迟队列](https://github.com/ouqiang/delay-queue)PHP客户端
3+
4+
[![Build Status](https://travis-ci.org/ouqiang/delayqueue-php.png)](https://travis-ci.org/ouqiang/delayqueue-php)

bin/delayqueue-php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,34 @@
11
#!/usr/bin/env php
22
<?php
3+
4+
use DelayQueue\Process\Worker;
5+
6+
require __DIR__ . '/../vendor/autoload.php';
7+
8+
if (!defined('STDIN') || php_sapi_name() !== 'cli') {
9+
echo 'only run in cli';
10+
exit(1);
11+
}
12+
13+
if (!extension_loaded('pcntl') || !extension_loaded('posix')) {
14+
echo 'extension pcntl, posix not loaded';
15+
exit(2);
16+
}
17+
18+
// 启动的worker数量
19+
$workerNum = 5;
20+
// 延迟队列服务器地址
21+
$server = 'http://127.0.0.1:9277';
22+
23+
for ($i = 0; $i < $workerNum; $i++) {
24+
$pid = pcntl_fork();
25+
if ($pid < 0) {
26+
throw new Exception('fork failed');
27+
}
28+
if ($pid === 0) {
29+
// 子进程
30+
$worker = new Worker();
31+
$worker->run();
32+
break;
33+
}
34+
}

build_delayqueue.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
#!/usr/bin/env bash
2-
set -x -u
2+
3+
wget -c https://github.com/ouqiang/delay-queue/releases/download/v0.3/delay-queue-linux-amd64.tar.gz
4+
tar xzf delay-queue-linux-amd64.tar.gz
5+
cd delay-queue
6+
7+
./delay-queue &> /dev/null &

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"require": {
1616
"php": ">= 5.4",
1717
"ext-pcntl": "*",
18+
"ext-posix": "*",
1819
"monolog/monolog": "^1.0",
1920
"guzzlehttp/guzzle": "^5.0",
2021
"psr/container": "^1.0"
@@ -27,13 +28,13 @@
2728
],
2829
"autoload": {
2930
"psr-4": {
30-
"DelayQueue\\": "src/",
31-
"Demo\\": "demo/"
31+
"DelayQueue\\": "src/"
3232
}
3333
},
3434
"autoload-dev": {
3535
"psr-4": {
36-
"DelayQueue\\Tests\\": "tests/"
36+
"DelayQueue\\Tests\\": "tests/",
37+
"Demo\\Handler\\": "demo/Handler"
3738
}
3839
}
3940
}

demo/Handler/OrderHandler.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Demo\Handler;
4+
5+
use DelayQueue\Handler\AbstractHandler;
6+
7+
class OrderHandler extends AbstractHandler
8+
{
9+
protected function perform()
10+
{
11+
12+
}
13+
}

demo/demo.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use DelayQueue\Job;
4+
use DelayQueue\Util\Time;
5+
use DelayQueue\DelayQueue;
6+
7+
require __DIR__ . '/../vendor/autoload.php';
8+
9+
$job = new Job();
10+
$job->setTopic('order');
11+
$job->setId('15702398321');
12+
$job->setDelay(1 * Time::MINUTE);
13+
$job->setTtr(20 * Time::SECOND);
14+
$job->setBody([
15+
'uid' => 10829378,
16+
'created' => 1498657365,
17+
]);
18+
19+
20+
$delayQueue = new DelayQueue('http://127.0.0.1:9277');
21+
$className = 'Demo\\Handler\\OrderHandler';
22+
try {
23+
// 添加一个Job到延迟队列
24+
$delayQueue->push($className, $job);
25+
26+
// 从延迟队列中删除Job
27+
$delayQueue->delete('15702398321');
28+
29+
// 从队列中取出已过期的Job
30+
$data = $delayQueue->pop(['order']);
31+
// $data['className'] 处理Job的类名
32+
// $data['id'] Job唯一标识
33+
// $data['body'] Job自定义Body
34+
35+
36+
// Job处理完成, 确认删除
37+
$delayQueue->finish('15702398321');
38+
} catch (Exception $exception) {
39+
echo $exception->getMessage();
40+
}
41+

demo/push_job.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "delayqueue-php",
3+
"version": "1.0.0",
4+
"description": "[延迟队列](https://github.com/ouqiang/delay-queue)PHP客户端",
5+
"main": "index.js",
6+
"directories": {
7+
"test": "tests"
8+
},
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/ouqiang/delayqueue-php.git"
15+
},
16+
"keywords": [],
17+
"author": "",
18+
"license": "ISC",
19+
"bugs": {
20+
"url": "https://github.com/ouqiang/delayqueue-php/issues"
21+
},
22+
"homepage": "https://github.com/ouqiang/delayqueue-php#readme",
23+
"devDependencies": {
24+
"cz-conventional-changelog": "^2.0.0"
25+
},
26+
"config": {
27+
"commitizen": {
28+
"path": "./node_modules/cz-conventional-changelog"
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)