PushJob.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\index\controller;
  12. use think\Exception;
  13. use think\exception\ErrorException;
  14. use think\Queue;
  15. class PushJob
  16. {
  17. /**
  18. * 一个使用了队列的 action
  19. */
  20. public static function actionWithDoPinkJob(array $data){
  21. try{
  22. // 1.当前任务将由哪个类来负责处理。
  23. // 当轮到该任务时,系统将生成一个该类的实例,并调用其 fire 方法
  24. $jobHandlerClassName = 'app\index\job\PullDoPink';
  25. // 2.当前任务归属的队列名称,如果为新队列,会自动创建
  26. $jobQueueName = "doPinkJobQueue";
  27. // 3.当前任务所需的业务数据 . 不能为 resource 类型,其他类型最终将转化为json形式的字符串
  28. // ( jobData 为对象时,存储其public属性的键值对 )
  29. $jobData = [ 'pinkInfo' => $data, 'time' => date('Y-m-d H:i:s')] ;
  30. if (!isset($data['pink_time']) || !$data['pink_time']) return true;
  31. $timewait = $data['pink_time'] + 300;
  32. //$jobData = [ 'pinkInfo' => 'hahah', 'time' => date('Y-m-d H:i:s'), 'b' => 21] ;
  33. //$timewait = 20;
  34. // 4.将该任务推送到消息队列,等待对应的消费者去执行
  35. $isPushed = Queue::later($timewait, $jobHandlerClassName , $jobData , $jobQueueName );
  36. //$isPushed = Queue::push($jobHandlerClassName , $jobData , $jobQueueName );
  37. // database 驱动时,返回值为 1|false ; redis 驱动时,返回值为 随机字符串|false
  38. if( $isPushed !== false ){
  39. return 1;
  40. }else{
  41. return 1;
  42. }
  43. }catch (ErrorException $e){
  44. echo $e->getMessage();
  45. }
  46. }
  47. }