FiveSecondTaskJob.php 2.3 KB

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