FiveSecondTaskJob.php 2.1 KB

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