StatServerJob.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\StatServer;
  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. class StatServerJob implements ShouldQueue
  10. {
  11. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  12. protected $statistic;
  13. public $tries = 3;
  14. public $timeout = 5;
  15. /**
  16. * Create a new job instance.
  17. *
  18. * @return void
  19. */
  20. public function __construct(array $statistic)
  21. {
  22. $this->onQueue('stat_server');
  23. $this->statistic = $statistic;
  24. }
  25. /**
  26. * Execute the job.
  27. *
  28. * @return void
  29. */
  30. public function handle()
  31. {
  32. $statistic = $this->statistic;
  33. $data = StatServer::where('record_at', $statistic['record_at'])
  34. ->where('server_id', $statistic['server_id'])
  35. ->first();
  36. if ($data) {
  37. try {
  38. $data->update($statistic);
  39. } catch (\Exception $e) {
  40. abort(500, '节点统计数据更新失败');
  41. }
  42. } else {
  43. if (!StatServer::create($statistic)) {
  44. abort(500, '节点统计数据创建失败');
  45. }
  46. }
  47. }
  48. }