安装
- 首先这货需要在linux下跑,非得用windows就别看了,也不是不能装,费劲且性能渣
- 得有composer,嫌慢的也拉倒吧,别看了,本文不介绍没有composer怎么办
- 安装composer也不是本文要点,在ubuntu(其实为了不折腾QQ我装的是deepin)中就是一行命令
- php版本要高一点,5.4以下也别看了
- 该环境php是apt装的,所以一切无阻碍,如果是编译安装可能不会这么顺畅
安装composer
sudo apt-get install composer
安装redis
redis是个数据库,nosql。不是废话,因为有人的确不知道
sudo apt-get install redis-server
还得有git
sudo apt-get install git
真正的安装
cd /your/www/path
git clone git://github.com/chrisboulton/php-resque.git
cd php-resque
composer install
不出意外的话能安装成功,抽风就多试几次,包不大,可忍受
demo
网上有些demo,都是抄的同一个博文,尔等能找到这里很可能是那一篇看了觉得不大对头。 composer安装的包里面有demo,里面的代码也TM坑的很。所以还是看我这篇。。。。
首先咱们写个简单的job
job,工作,就是碎催,接到消息之后干活的,好,定义明确了
<?php
// job.php 放在demo目录里面,原有的那个删了算球
class TestJob
{
public function perform()
{
echo $this->args['name'];
}
}
再写个脚本往队列里写消息
<?php
// demo/quere.php 注意,这个是个命令行版,需要在shell中运行,也可以自己写个浏览器里能跑的
// 功能,往队列里写一些装逼犯的名字
if(empty($argv[1])) {
die('Specify the name of a job to add. e.g, php queue.php PHP_Job');
}
require __DIR__ . '/init.php';
date_default_timezone_set('GMT');
Resque::setBackend('127.0.0.1:6379');
$names = [
'李灵黛','冷文卿','阴露萍','柳兰歌','秦水支','李念儿','文彩依','柳婵诗','顾莫言','任水寒','金磨针','丁玲珑','凌霜华','水笙','景茵梦','容柒雁','林墨瞳','华诗','千湄','剑舞','兰陵',' 洛离'
];
foreach($names as $name){
$jobId = Resque::enqueue('default', 'TestJob', ['name' => $name]);
echo "Queued job " . $jobId . "\n\n";
}```
## 写个守护进程脚本
```php
<?php
// demo/resque.php照抄原demo,会发现找不到文件,所以可以照抄我这个
// 这个脚本也是在shell里跑的,真正应用应该放进开机启动里面
date_default_timezone_set('GMT');
require 'bad_job.php';
require 'job.php';
require 'php_error_job.php';
require '/install-path/php-resque/bin/resque';
试一下
先启动守护进程
QUEUE=* php demo/resque.php
会有以下输出
#!/usr/bin/env php
[notice] Starting worker your-computer-name:10757:*
另开一个终端,键入
php demo/queue.php TestJob
会有类似下面输出,那些hash串就是任务id了
Queued job 4b510e225af5897bd5022fee30d202bf
Queued job d14a2ed9339f739b2dec0e0c64069dd7
Queued job d58a536dab2fde9aa6097b37577bd02a
Queued job 53ed1a64d7d8e9a4fed15a0942587e64
Queued job 178ea9087a159c9c7b74b8d9d87e40c2
Queued job cc5dd2087de6865e613fcfdebd52abbc
Queued job e647351c4a83b04b3d701a5dafa52118
Queued job 3f69bd449888e5adb5984a0d64aeb3ac
Queued job 31e114326c8db42443029e14bd677cdb
Queued job 28404f8202a22ade5d204345596d10e0
Queued job fefcf7c402b09c92de2eac5aa642ca80
Queued job e6ecf1529d97e568c5f80bb330c423f2
Queued job c92e64ca49948e7547a5dacce2f373fb
Queued job 97a504c56d74f7714781318b6d244ae6
Queued job 2f8e51937016fc981e0426fcef5d1643
Queued job db3be312811732803fc5d7b814cc69d4
Queued job 23124e4693146812471c09401137a6fd
Queued job af070ce348f73c6c6fe25782351a1937
Queued job 8825fc73b7881e13067e59d10287b598
Queued job 21e31139c9f70c7097927a80a442a577
Queued job 283a8d6119657dd018a5cc8298021bba
这时你看刚才运行守护进程的那个终端,会有类似以下输出,如愿输出了一系列装逼犯
[notice] Starting work on (Job{default} | ID: 3dcf4dc9008d255ab59917a221aaf984 | TestJob | [{"name":"\u674e\u7075\u9edb"}])
李灵黛[notice] (Job{default} | ID: 3dcf4dc9008d255ab59917a221aaf984 | TestJob | [{"name":"\u674e\u7075\u9edb"}]) has finished
[notice] Starting work on (Job{default} | ID: fa2d6bd7a97a77ab754c35822cb8dcd0 | TestJob | [{"name":"\u51b7\u6587\u537f"}])
冷文卿[notice] (Job{default} | ID: fa2d6bd7a97a77ab754c35822cb8dcd0 | TestJob | [{"name":"\u51b7\u6587\u537f"}]) has finished
[notice] Starting work on (Job{default} | ID: 0962a49c4ffd3e987864b2c7e2fdf857 | TestJob | [{"name":"\u9634\u9732\u840d"}])
阴露萍[notice] (Job{default} | ID: 0962a49c4ffd3e987864b2c7e2fdf857 | TestJob | [{"name":"\u9634\u9732\u840d"}]) has finished
[notice] Starting work on (Job{default} | ID: 51ecee3f45d0ba8a906c438ad8ad1887 | TestJob | [{"name":"\u67f3\u5170\u6b4c"}])
柳兰歌[notice] (Job{default} | ID: 51ecee3f45d0ba8a906c438ad8ad1887 | TestJob | [{"name":"\u67f3\u5170\u6b4c"}]) has finished
[notice] Starting work on (Job{default} | ID: 6dbdf11ee30ab874a94d239d93637671 | TestJob | [{"name":"\u79e6\u6c34\u652f"}])
秦水支[notice] (Job{default} | ID: 6dbdf11ee30ab874a94d239d93637671 | TestJob | [{"name":"\u79e6\u6c34\u652f"}]) has finished
[notice] Starting work on (Job{default} | ID: e566a95f0666dcca8fa30c1ed984434f | TestJob | [{"name":"\u674e\u5ff5\u513f"}])
李念儿[notice] (Job{default} | ID: e566a95f0666dcca8fa30c1ed984434f | TestJob | [{"name":"\u674e\u5ff5\u513f"}]) has finished
[notice] Starting work on (Job{default} | ID: 826cfe9d90f06f06f19c790bae54955d | TestJob | [{"name":"\u6587\u5f69\u4f9d"}])
文彩依[notice] (Job{default} | ID: 826cfe9d90f06f06f19c790bae54955d | TestJob | [{"name":"\u6587\u5f69\u4f9d"}]) has finished
[notice] Starting work on (Job{default} | ID: 106843bea773add35576cb796f5a2036 | TestJob | [{"name":"\u67f3\u5a75\u8bd7"}])
柳婵诗[notice] (Job{default} | ID: 106843bea773add35576cb796f5a2036 | TestJob | [{"name":"\u67f3\u5a75\u8bd7"}]) has finished
。。。
。。。
。。。
打完收工
这里是个简单例子,实际上干啥都行,发短信,发邮件,执行不那么着急的数据库操作等等,只管job.php里面写个新class即可,往队列里些消息的时候注意标明一下参数就行
Resque::enqueue('default', 'ClassName', ['参数一' => $param1, '参数二' => $param2,]);
简单应用完全够了