FiveSecondTaskJob.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Jobs;
  3. use App\Services\PcIssueService;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldBeUnique;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use App\Services\IssueService;
  11. use Illuminate\Support\Facades\Log;
  12. class FiveSecondTaskJob implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. /**
  16. * 任务尝试次数
  17. */
  18. public $tries = 3;
  19. /**
  20. * 任务超时时间(秒)
  21. */
  22. public $timeout = 30;
  23. /**
  24. * Create a new job instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. //
  31. }
  32. /**
  33. * Execute the job.
  34. *
  35. * @return void
  36. */
  37. public function handle()
  38. {
  39. try {
  40. Log::error('🚀 开始执行15秒任务: ' . now());
  41. PcIssueService::index();
  42. IssueService::syncCountdownIssue(); // 提前20秒提醒
  43. IssueService::syncCloseIssue(); // 同步停止
  44. // Log::error('✅ 获取到最新期号: ' . ($latestIssue ?? '无'));
  45. // 你的业务逻辑
  46. IssueService::getLatestIssue(); // 获取最新的期号
  47. // 重要:使用类名而不是 self(),避免递归
  48. FiveSecondTaskJob::dispatch()->delay(now()->addSeconds(15));
  49. Log::error('📅 下一个15秒任务已安排');
  50. } catch (\Exception $e) {
  51. Log::error('❌ 15秒任务执行异常: ' . $e->getMessage());
  52. // 异常后15秒重试
  53. FiveSecondTaskJob::dispatch()->delay(now()->addSeconds(15));
  54. }
  55. }
  56. // 可选:失败处理
  57. public function failed(\Throwable $exception)
  58. {
  59. Log::error('任务失败: ' . $exception->getMessage());
  60. // 可选:重新分发或通知
  61. dispatch(new self())->delay(now()->addSeconds(15));
  62. }
  63. }