PaymentJob.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. Log::error('📅 下一个更新订单状态任务已安排');
  40. } catch (\Exception $e) {
  41. Log::error('❌ 更新订单状态任务执行异常: ' . $e->getMessage());
  42. // 异常后15秒重试
  43. PaymentJob::dispatch()->delay(now()->addSeconds(15));
  44. }
  45. }
  46. // 可选:失败处理
  47. public function failed(\Throwable $exception)
  48. {
  49. Log::error('更新订单状态任务失败: ' . $exception->getMessage());
  50. // 可选:重新分发或通知
  51. dispatch(new self())->delay(now()->addSeconds(15));
  52. }
  53. }