FiveSecondTaskJob.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 App\Services\IssueService;
  10. use Illuminate\Support\Facades\Log;
  11. class FiveSecondTaskJob 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::info('🚀 开始执行5秒任务: ' . now());
  40. // 你的业务逻辑
  41. IssueService::getLatestIssue(); // 获取最新的期号
  42. IssueService::syncCloseIssue(); // 同步停止
  43. // Log::info('✅ 获取到最新期号: ' . ($latestIssue ?? '无'));
  44. // 重要:使用类名而不是 self(),避免递归
  45. FiveSecondTaskJob::dispatch()->delay(now()->addSeconds(5));
  46. Log::info('📅 下一个5秒任务已安排');
  47. } catch (\Exception $e) {
  48. Log::error('❌ 5秒任务执行异常: ' . $e->getMessage());
  49. // 异常后10秒重试
  50. FiveSecondTaskJob::dispatch()->delay(now()->addSeconds(10));
  51. }
  52. }
  53. // 可选:失败处理
  54. public function failed(\Throwable $exception)
  55. {
  56. Log::error('任务失败: ' . $exception->getMessage());
  57. // 可选:重新分发或通知
  58. dispatch(new self())->delay(now()->addSeconds(5));
  59. }
  60. }