Yii2 使用 Beanstalk 队列
Yii2 使用 Beanstalk 队列
小田天 发表于2年前
Yii2 使用 Beanstalk 队列
  • 发表于 2年前
  • 阅读 28
  • 收藏 0
  • 点赞 0
  • 评论 0

移动开发云端新模式探索实践 >>>   

安装

安装服务端

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

安装 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

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 78
码字总数 49841
×
小田天
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: