PaymentJob.php 1.7 KB

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