Yii2 使用 Beanstalk 队列

安装

安装服务端

根据官方的教程安装,一条命令搞定。

安装 Yii2 客户端扩展

在你 Yii2 项目中执行

 
  1. $ composer require udokmeci/yii2-beanstalk "dev-master"

安装管理 Beanstalk 队列的 Web 系统

 

此项目是一个单独的系统,可以查看和管理队列任务

 

 
  1. $ composer create-project ptrofimov/beanstalk_console -s dev path/to/install

使用

开启服务端

 
  1. $ beanstalkd -l 127.0.0.1 -p 11300

Yii2 端使用

添加配置文件:

 
  1. 'components' => [
  2. // ...
  3. 'beanstalk' => [
  4. 'class' => 'udokmeci\yii2beanstalk\Beanstalk',
  5. 'host' => 127.0.0.1, // default host
  6. 'port' => 11300,
  7. 'connectTimeout' => 1,
  8. 'sleep' => false, // or int for usleep after every job
  9. ],
  10. // ...
  11. ],

执行队列在 console\controllers\WorkerController 里面:

 
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yidashi
  5. * Date: 16/8/16
  6. * Time: 下午9:09
  7. */
  8.  
  9. namespace console\controllers;
  10.  
  11. use udokmeci\yii2beanstalk\BeanstalkController;
  12. use yii\helpers\Console;
  13. use Yii;
  14.  
  15. class WorkerController extends BeanstalkController
  16. {
  17. // Those are the default values you can override
  18.  
  19. const DELAY_PRIORITY = "1000"; //Default priority
  20. const DELAY_TIME = 5; //Default delay time
  21.  
  22. // Used for Decaying. When DELAY_MAX reached job is deleted or delayed with
  23. const DELAY_MAX = 3;
  24.  
  25. public function listenTubes(){
  26. return ["tube"];
  27. }
  28.  
  29. /**
  30. *
  31. * @param Pheanstalk\Job $job
  32. * @return string self::BURY
  33. * self::RELEASE
  34. * self::DELAY
  35. * self::DELETE
  36. * self::NO_ACTION
  37. * self::DECAY
  38. *
  39. */
  40. public function actionTube($job){
  41. $sentData = $job->getData();
  42. try {
  43. // something useful here
  44.  
  45. /*if($everthingIsAllRight == true){
  46. fwrite(STDOUT, Console::ansiFormat("- Everything is allright"."\n", [Console::FG_GREEN]));
  47. //Delete the job from beanstalkd
  48. return self::DELETE;
  49. }
  50.  
  51. if($everthingWillBeAllRight == true){
  52. fwrite(STDOUT, Console::ansiFormat("- Everything will be allright"."\n", [Console::FG_GREEN]));
  53. //Delay the for later try
  54. //You may prefer decay to avoid endless loop
  55. return self::DELAY;
  56. }
  57.  
  58. if($IWantSomethingCustom==true){
  59. Yii::$app->beanstalk->release($job);
  60. return self::NO_ACTION;
  61. }
  62.  
  63. fwrite(STDOUT, Console::ansiFormat("- Not everything is allright!!!"."\n", [Console::FG_GREEN]));
  64. //Decay the job to try DELAY_MAX times.
  65. return self::DECAY;*/
  66.  
  67. // if you return anything else job is burried.
  68. } catch (\Exception $e) {
  69. //If there is anything to do.
  70. fwrite(STDERR, Console::ansiFormat($e."\n", [Console::FG_RED]));
  71. // you can also bury jobs to examine later
  72. return self::BURY;
  73. }
  74. }
  75. }

最后记得要执行你写的 Worker:

$ php yii worker

参考文献

原文链接: http://www.getyii.com/topic/531

转载于:https://my.oschina.net/u/2552765/blog/790390

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值