StatServerJob.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 $u;
  13. protected $d;
  14. protected $server;
  15. protected $protocol;
  16. protected $recordType;
  17. public $tries = 3;
  18. public $timeout = 60;
  19. /**
  20. * Create a new job instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct($u, $d, $server, $protocol, $recordType = 'd')
  25. {
  26. $this->onQueue('stat');
  27. $this->u = $u;
  28. $this->d = $d;
  29. $this->server = $server;
  30. $this->protocol = $protocol;
  31. $this->recordType = $recordType;
  32. }
  33. /**
  34. * Execute the job.
  35. *
  36. * @return void
  37. */
  38. public function handle()
  39. {
  40. $recordAt = strtotime(date('Y-m-d'));
  41. if ($this->recordType === 'm') {
  42. //
  43. }
  44. $data = StatServer::lockForUpdate()
  45. ->where('record_at', $recordAt)
  46. ->where('server_id', $this->server['id'])
  47. ->where('server_type', $this->protocol)
  48. ->first();
  49. if ($data) {
  50. try {
  51. $data->update([
  52. 'u' => $data['u'] + $this->u,
  53. 'd' => $data['d'] + $this->d
  54. ]);
  55. } catch (\Exception $e) {
  56. abort(500, '节点统计数据更新失败');
  57. }
  58. } else {
  59. if (!StatServer::create([
  60. 'server_id' => $this->server['id'],
  61. 'server_type' => $this->protocol,
  62. 'u' => $this->u,
  63. 'd' => $this->d,
  64. 'record_type' => $this->recordType,
  65. 'record_at' => $recordAt
  66. ])) {
  67. abort(500, '节点统计数据创建失败');
  68. }
  69. }
  70. }
  71. }