BoxAddJob.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Jobs;
  3. use App\Model\ErrorTable;
  4. use App\Service\Box\BoxService;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use Illuminate\Support\Facades\Log;
  11. use Symfony\Component\Console\Output\ConsoleOutput;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. class BoxAddJob implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. //队列名称
  17. const job = 'hc_box';// 包装 =》 用友产成品入库
  18. protected $data;
  19. protected $user;
  20. public $tries = 0;
  21. public function __construct($data, $user = [])
  22. {
  23. $this->data = $data ?? [];
  24. $this->user = $user ?? [];
  25. }
  26. /**
  27. *
  28. * file_put_contents('charge.txt',"标记位置退出".PHP_EOL,8);
  29. * Execute the job.
  30. *
  31. * @return void
  32. */
  33. public function handle()
  34. {
  35. $service = new BoxService();
  36. $currentAttempt = $this->attempts();
  37. try {
  38. Log::info("任务开始执行", [
  39. 'job_id' => $this->job->getJobId(),
  40. 'attempt' => $currentAttempt,
  41. 'data' => $this->data
  42. ]);
  43. list($status,$msg) = $service->boxInSettle($this->data, $this->user);
  44. if(! $status) {
  45. Log::info("业务逻辑执行失败", [
  46. 'job_id' => $this->job->getJobId(),
  47. 'attempt' => $currentAttempt,
  48. 'error' => $msg
  49. ]);
  50. $service->dellimitingSendRequestBackgNeed($service->lock_key); //删除锁
  51. $this->recordErrorTable($msg);
  52. }else {
  53. Log::info("任务执行成功", [
  54. 'job_id' => $this->job->getJobId(),
  55. 'attempt' => $currentAttempt
  56. ]);
  57. }
  58. } catch (\Throwable $e) {
  59. Log::error("任务执行异常", [
  60. 'job_id' => $this->job->getJobId(),
  61. 'attempt' => $currentAttempt,
  62. 'exception' => $e->getMessage(),
  63. ]);
  64. $service->dellimitingSendRequestBackgNeed($service->lock_key);//删除锁
  65. $this->delete();
  66. $this->recordErrorTable($e->getFile() . $e->getMessage() . $e->getLine());
  67. }
  68. //输出信息
  69. $this->echoMessage(new ConsoleOutput());
  70. }
  71. protected function echoMessage(OutputInterface $output)
  72. {
  73. //输出消息
  74. $output->writeln(json_encode($this->data));
  75. }
  76. private function recordErrorTable($msg){
  77. // 连接到指定数据库连接
  78. ErrorTable::insert([
  79. 'msg' => $msg,
  80. 'data' => json_encode($this->data),
  81. 'user_id' => $this->user['id'] ?? 0,
  82. 'user_operation_time' => time(),
  83. 'type' => 1,
  84. 'order_no' => $data['order_no'] ?? ""
  85. ]);
  86. }
  87. }