IdentifyFire.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * IdentifyFire.php UTF-8
  4. * 实名认证队列消耗
  5. *
  6. * @date : 2021/4/28 11:54
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK-IDENTITY 1.0
  11. */
  12. namespace huoIdentify\lib\queue\job;
  13. use huoIdentify\lib\queue\constant\JobConst;
  14. use huoIdentify\model\IdentifyInQueueModel;
  15. use huolib\queue\job\Fire;
  16. use think\queue\Job;
  17. class IdentifyFire extends Fire {
  18. /**
  19. * fire方法是消息队列默认调用的方法
  20. *
  21. * @param Job $job 当前的任务对象
  22. * @param array|mixed $data 发布任务时自定义的数据
  23. */
  24. public function fire(Job $job, $data) {
  25. $_is_job_done = $this->doJob($data);
  26. if ($_is_job_done) {
  27. /* 如果任务执行成功, 记得删除任务 */
  28. $job->delete();
  29. } else {
  30. $_limit_attempts = get_val($data, 'attempts', JobConst::JOB_ATTEMPTS_MAX_CNT);
  31. $_attempts = $job->attempts();
  32. if ($_attempts >= $_limit_attempts) {
  33. /* 任务达到最大次数则删除任务不再执行 */
  34. $job->delete();
  35. $this->deleteData($data);
  36. } else {
  37. /* 重新发布任务 */
  38. switch ($_attempts) {
  39. case 1:
  40. $_delay = JobConst::JOB_ATTEMPTS_SECOND_TIME;
  41. break;
  42. case 2:
  43. $_delay = JobConst::JOB_ATTEMPTS_THIRD_TIME;
  44. break;
  45. case 3:
  46. $_delay = JobConst::JOB_ATTEMPTS_FOURTH_TIME;
  47. break;
  48. case 4:
  49. $_delay = JobConst::JOB_ATTEMPTS_FIFTH_TIME;
  50. break;
  51. default:
  52. $_delay = JobConst::JOB_ATTEMPTS_ONCE_TIME;
  53. }
  54. $job->release($_delay); /* $_delay为延迟时间,表示该任务延迟$_delay秒后再执行 */
  55. }
  56. }
  57. }
  58. /**
  59. * 根据消息中的数据进行实际的业务处理
  60. *
  61. * @param array $data 发布任务时自定义的数据
  62. *
  63. * @return bool 任务执行的结果
  64. */
  65. public function doJob($data) {
  66. $_iiq_id = isset($data['iiq_id']) ? $data['iiq_id'] : '';
  67. if (empty($_iiq_id)) {
  68. /* $_iiq_id 为空认为执行成功,否则会重复入队列 */
  69. return true;
  70. }
  71. $_identify_class = isset($data['identify_class']) ? $data['identify_class'] : '';
  72. if (class_exists($_identify_class)) {
  73. return (new $_identify_class())->checkFromQueue($_iiq_id);
  74. }
  75. return true;
  76. }
  77. /**
  78. * 删除数据库数据
  79. *
  80. * @param $data
  81. */
  82. public function deleteData($data) {
  83. $_iiq_id = isset($data['iiq_id']) ? $data['iiq_id'] : '';
  84. if (!empty($_iiq_id)) {
  85. (new IdentifyInQueueModel())->deleteData($_iiq_id);
  86. }
  87. }
  88. }