PaymentJob.php 1.6 KB

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