FiveSecondTaskJob.php 2.0 KB

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