FiveSecondTaskJob.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. //自定义开奖
  42. PcIssueService::index();
  43. IssueService::syncCountdownIssue(); // 提前20秒提醒
  44. IssueService::syncCloseIssue(); // 同步停止
  45. // Log::error('✅ 获取到最新期号: ' . ($latestIssue ?? '无'));
  46. // 你的业务逻辑
  47. IssueService::getLatestIssue(); // 获取最新的期号
  48. // 重要:使用类名而不是 self(),避免递归
  49. FiveSecondTaskJob::dispatch()->delay(now()->addSeconds(15));
  50. Log::error('📅 下一个15秒任务已安排');
  51. } catch (\Exception $e) {
  52. Log::error('❌ 15秒任务执行异常: ' . $e->getMessage());
  53. // 异常后15秒重试
  54. FiveSecondTaskJob::dispatch()->delay(now()->addSeconds(15));
  55. }
  56. }
  57. // 可选:失败处理
  58. public function failed(\Throwable $exception)
  59. {
  60. Log::error('任务失败: ' . $exception->getMessage());
  61. // 可选:重新分发或通知
  62. dispatch(new self())->delay(now()->addSeconds(15));
  63. }
  64. }