TrafficFetchJob.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\User;
  4. use App\Services\MailService;
  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\DB;
  11. class TrafficFetchJob implements ShouldQueue
  12. {
  13. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  14. protected $u;
  15. protected $d;
  16. protected $userId;
  17. protected $server;
  18. protected $protocol;
  19. public $tries = 3;
  20. public $timeout = 3;
  21. /**
  22. * Create a new job instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct($u, $d, $userId, $server, $protocol)
  27. {
  28. $this->onQueue('traffic_fetch');
  29. $this->u = $u;
  30. $this->d = $d;
  31. $this->userId = $userId;
  32. $this->server = $server;
  33. $this->protocol = $protocol;
  34. }
  35. /**
  36. * Execute the job.
  37. *
  38. * @return void
  39. */
  40. public function handle()
  41. {
  42. $user = User::find($this->userId);
  43. if (!$user) return;
  44. try {
  45. $user->update([
  46. 't' => time(),
  47. 'u' => DB::raw("u+{$this->u}"),
  48. 'd' => DB::raw("d+{$this->d}")
  49. ]);
  50. } catch (\Exception $e) {
  51. throw new \Exception('流量更新失败');
  52. }
  53. $mailService = new MailService();
  54. $mailService->remindTraffic($user->first());
  55. }
  56. }