PHP code example of cnjackhack2012 / delayqueue-php
1. Go to this page and download the library: Download cnjackhack2012/delayqueue-php library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cnjackhack2012 / delayqueue-php example snippets
use DelayQueue\Job;
use DelayQueue\Util\Time;
use DelayQueue\DelayQueue;
tDelay(1 * Time::MINUTE);
$job->setTtr(20 * Time::SECOND);
$job->setBody([
'uid' => 10829378,
'created' => 1498657365,
]);
$delayQueue = new DelayQueue('http://127.0.0.1:9277');
// 处理Job的类名
$className = 'Demo\\Handler\\OrderHandler';
try {
// 添加一个Job到队列
$delayQueue->push($className, $job);
// 从队列中删除Job
$delayQueue->delete('15702398321');
} catch (Exception $exception) {
echo $exception->getMessage();
}
#!/usr/bin/env php
$workerNum = 10;
$command = '/path/to/vendor/bin/delayqueue-php';
$args = ['-c', '/path/to/config.ini'];
for ($i = 0; $i < $workerNum; $i++) {
$pid = pcntl_fork();
if ($pid < 0) {
// fork失败
} else if ($pid === 0) {
// 子进程
pcntl_exec($command, $args);
break;
}
}
namespace Demo\Handler;
use DelayQueue\Handler\AbstractHandler;
// 必须继承DelayQueue\Handler\AbstractHandler, 并实现方法perform
class OrderHandler extends AbstractHandler
{
protected function perform()
{
// 未抛出异常视为成功, Job将会被删除
// Job唯一标识
// $this->id;
// Job自定义内容
// $this->body;
}
}
shell
php vendor/bin/delayqueue-php -c /path/to/config.ini