ProcessDataJob.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Jobs;
  3. use App\Service\ClearDataService;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use Symfony\Component\Console\Output\ConsoleOutput;
  10. use Symfony\Component\Console\Output\OutputInterface;
  11. class ProcessDataJob implements ShouldQueue
  12. {
  13. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  14. protected $data;
  15. /**
  16. * Create a new job instance.
  17. *
  18. * @return void
  19. */
  20. public function __construct($data)
  21. {
  22. $this->data = $data;
  23. }
  24. /**
  25. * Execute the job.
  26. *
  27. * @return void
  28. */
  29. public function handle()
  30. {
  31. //标记
  32. file_put_contents('record.txt',json_encode($this->data) . PHP_EOL,8);
  33. //保存数据到自己服务器
  34. ClearDataService::saveData($this->data);
  35. //处理数据
  36. $return = ClearDataService::clearData($this->data);
  37. //传递数据给下一个队列
  38. dispatch(new SendDataJob($return))->onQueue('cloud_device');
  39. //输出信息
  40. $this->echoMessage(new ConsoleOutput());
  41. }
  42. protected function echoMessage(OutputInterface $output)
  43. {
  44. $output->writeln(json_encode($this->data));
  45. }
  46. }