控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DemoJob.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 测试队列,仅供参考
  13. // +----------------------------------------------------------------------
  14. namespace addons\queue\jobs;
  15. use think\facade\Log;
  16. use think\queue\Job;
  17. class DemoJob
  18. {
  19. /**
  20. * fire方法是消息队列默认调用的方法
  21. * @param Job $job 当前的任务对象
  22. * @param array|mixed $data 发布任务时自定义的数据
  23. */
  24. public function fire(Job $job, $data)
  25. {
  26. $isJobDone = $this->doHelloJob($data);
  27. if ($isJobDone) {
  28. // 如果任务执行成功, 记得删除任务
  29. $job->delete();
  30. print("<info>Hello Job has been done and deleted" . "</info>\n");
  31. } else {
  32. //超过3次删除任务 如果不删除 并且命令行附带--tries参数,则会执行failed方法
  33. if ($job->attempts() > 3) {
  34. $job->delete();
  35. print("<warn>Hello Job has been retried more than 3 times!" . "</warn>\n");
  36. } else {
  37. //5秒后从新放入队列
  38. $job->release(5);
  39. }
  40. }
  41. }
  42. //多任务A 在发布任务时,需要用 push('任务的类名@方法名')
  43. public function taskA(Job $job, $data)
  44. {
  45. $isJobDone = $this->_doTaskA($data);
  46. if ($isJobDone) {
  47. $job->delete();
  48. print("Info: TaskA of Job MultiTask has been done and deleted" . "\n");
  49. } else {
  50. if ($job->attempts() > 3) {
  51. $job->delete();
  52. }
  53. }
  54. }
  55. //多任务B 在发布任务时,需要用 push('任务的类名@方法名')
  56. public function taskB(Job $job, $data)
  57. {
  58. $isJobDone = $this->_doTaskA($data);
  59. if ($isJobDone) {
  60. $job->delete();
  61. print("Info: TaskB of Job MultiTask has been done and deleted" . "\n");
  62. } else {
  63. if ($job->attempts() > 2) {
  64. $job->release();
  65. }
  66. }
  67. }
  68. /**
  69. * 根据消息中的数据进行实际的业务处理...
  70. */
  71. private function doHelloJob($data)
  72. {
  73. print("<info>Hello Job Started. job Data is: " . var_export($data, true) . "</info> \n");
  74. print("<info>Hello Job is Fired at " . date('Y-m-d H:i:s') . "</info> \n");
  75. print("<info>Hello Job is Done!" . "</info> \n");
  76. return false;
  77. }
  78. private function _doTaskA($data)
  79. {
  80. print("Info: doing TaskA of Job MultiTask " . "\n");
  81. return true;
  82. }
  83. private function _doTaskB($data)
  84. {
  85. print("Info: doing TaskB of Job MultiTask " . "\n");
  86. return true;
  87. }
  88. //此函数可以省略 通常用于定义通知管理员队列失败
  89. //任务最终失败回调 命令行的 --tries 参数的值大于0生效
  90. public function failed($jobData)
  91. {
  92. Log::error("Warning: Job failed after max retries. job data is :" . var_export($jobData, true));
  93. }
  94. }