基于php的任务队列
queue
介绍
任务队列,用于发布、消费程序任务。
这是基于think-queue
照葫芦画瓢的作品,很多基本的原理都来自think-queue
。
因为运行过程跟定时任务比较类似,所以我更喜欢称其为任务队列。
软件架构
php>=7.0
symfony/process
安装教程
仓库地址:https://github.com/d2gin/queue
composer require icy8/queue
使用说明
监听/消费任务
因为
think-queue
的消费进程是基于think
命令的,我们并不希望把thinkphp
的其他依赖牵扯进来。所以定制了一个可以自定义消费进程的方法
makeProcess
,他可以让你调用任何php进程来消费任务,方法参数只接受string
和Process
对象。<?php use icy8\Queue\Listener; use Symfony\Component\Process\PhpProcess; include __DIR__ . "/../../../../autoload.php"; $listener = new Listener(); // 进程调度间隔 单位秒 默认是1秒 允许小数 $listener->execInterval = '0.1'; // 定制自己的消费进程 /*$listener->makeProcess('<?php // 为redis配置密码 $exec = new \icy8\Queue\Executor("redis", ["port"=>"6399", "password"=>"123456"]); $exec->runNext();');*/ // 开始监听队列 $listener->run();
发布任务
<?php include __DIR__ . "/../../../../autoload.php"; $connection = new \icy8\Queue\connector\Redis(); $connection->init(); // 任务立即执行 $connection->push(\icy8\Queue\test\JobTest::class, ['a', 'b', 'c']); // 运行任务类的自定义方法 $connection->push([\icy8\Queue\test\JobTest::class, 'custom'], '自定义方法'); // 延迟任务 $connection->pushDelay(\icy8\Queue\test\JobTest::class, ['a', 'b', 'c'], 3);
其他说明
核心的两部分是消费和发布,而消费的核心是
\icy8\Queue\Executor
,所以你完全可以不使用icy8\Queue\Listener
。为什么使用
symfony/process
:主要是涉及到热更新的问题,没想到、没找到其他更好的办法。redis的配置写得比较乱,需要优化。
基于php的任务队列
http://blog.icy8.cn/posts/60975/