PaymentJob.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use Illuminate\Support\Facades\Log;
  10. use App\Services\PaymentOrderService;
  11. use Google\Service\Adsense\Payment;
  12. class PaymentJob implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. /**
  16. * 任务尝试次数
  17. */
  18. public $tries = 3;
  19. /**
  20. * 任务超时时间(秒)
  21. */
  22. public $timeout = 30;
  23. /**
  24. * Create a new job instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. //
  31. }
  32. /**
  33. * Execute the job.
  34. *
  35. * @return void
  36. */
  37. public function handle()
  38. {
  39. try {
  40. Log::error('🚀 开始执行更新订单状态任务: ' . now());
  41. PaymentOrderService::syncPayOrder();
  42. // 重要:使用类名而不是 self(),避免递归
  43. PaymentJob::dispatch()->delay(now()->addSeconds(15));
  44. Log::error('📅 下一个更新订单状态任务已安排');
  45. } catch (\Exception $e) {
  46. Log::error('❌ 更新订单状态任务执行异常: ' . $e->getMessage());
  47. // 异常后15秒重试
  48. PaymentJob::dispatch()->delay(now()->addSeconds(15));
  49. }
  50. }
  51. // 可选:失败处理
  52. public function failed(\Throwable $exception)
  53. {
  54. Log::error('更新订单状态任务失败: ' . $exception->getMessage());
  55. // 可选:重新分发或通知
  56. dispatch(new self())->delay(now()->addSeconds(15));
  57. }
  58. }