1
0

StatServerJob.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 = 10;
  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. if (!StatServer::create([
  51. 'server_id' => $this->server['id'],
  52. 'server_type' => $this->protocol,
  53. 'u' => $this->u,
  54. 'd' => $this->d,
  55. 'record_type' => $this->recordType,
  56. 'record_at' => $recordAt
  57. ])) {
  58. abort(500, '节点统计数据创建失败');
  59. }
  60. return;
  61. }
  62. try {
  63. $data->update([
  64. 'u' => $data['u'] + $this->u,
  65. 'd' => $data['d'] + $this->d
  66. ]);
  67. } catch (\Exception $e) {
  68. abort(500, '节点统计数据更新失败');
  69. }
  70. }
  71. }