FiveSecondTaskJob.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Jobs;
  3. use App\Services\BetService;
  4. use App\Services\LogService;
  5. use App\Services\PcIssueService;
  6. use Exception;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. use App\Services\IssueService;
  13. use Illuminate\Support\Facades\Log;
  14. class FiveSecondTaskJob implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. /**
  18. * 任务尝试次数
  19. */
  20. public $tries = 3;
  21. /**
  22. * 任务超时时间(秒),需小于队列的 retry_after。
  23. */
  24. public $timeout = 85;
  25. /**
  26. * Create a new job instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. //
  33. }
  34. /**
  35. * Execute the job.
  36. *
  37. * @return void
  38. */
  39. public function handle()
  40. {
  41. try {
  42. //(极速28)自定义开奖
  43. $this->runTimedStep('极速28开奖处理', fn () => PcIssueService::index());
  44. // 提前60秒秒提醒封盘倒数
  45. $this->runTimedStep('加拿大28封盘倒计时', fn () => IssueService::syncCountdownIssue());
  46. // 停止下注 //封盘
  47. $this->runTimedStep('加拿大28封盘', fn () => IssueService::syncCloseIssue());
  48. // 获取最新的期号
  49. $this->runTimedStep('加拿大28开奖同步', fn () => IssueService::getLatestIssue());
  50. //随机虚拟投注
  51. $this->runTimedStep('虚拟投注', fn () => BetService::randomVirtualBetting(3));
  52. // 重要:使用类名而不是 self(),避免递归
  53. FiveSecondTaskJob::dispatch()->delay(now()->addSeconds(15));
  54. // Log::error('📅 下一个15秒任务已安排');
  55. } catch (Exception $e) {
  56. try {
  57. throw new Exception('❌ 15秒任务执行异常: ' . $e->getMessage());
  58. }catch (Exception $e) {
  59. LogService::error($e);
  60. }
  61. // 异常后15秒重试
  62. FiveSecondTaskJob::dispatch()->delay(now()->addSeconds(15));
  63. }
  64. }
  65. private function runTimedStep(string $step, callable $callback): void
  66. {
  67. $startedAt = microtime(true);
  68. $completed = false;
  69. $context = [
  70. 'step' => $step,
  71. 'job_id' => $this->job?->getJobId(),
  72. 'attempt' => $this->attempts(),
  73. 'process_id' => getmypid(),
  74. ];
  75. Log::channel('issue')->info('PC28任务步骤开始', $context);
  76. try {
  77. $callback();
  78. $completed = true;
  79. } finally {
  80. Log::channel('issue')->info('PC28任务步骤结束', $context + [
  81. 'elapsed_ms' => round((microtime(true) - $startedAt) * 1000, 1),
  82. 'status' => $completed ? 'completed' : 'failed',
  83. ]);
  84. }
  85. }
  86. // 可选:失败处理
  87. public function failed(\Throwable $exception)
  88. {
  89. Log::error('任务失败: ' . $exception->getMessage());
  90. // 可选:重新分发或通知
  91. dispatch(new self())->delay(now()->addSeconds(15));
  92. }
  93. }