FiveSecondTaskJob.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Services\IssueService;
  10. use Illuminate\Support\Facades\Log;
  11. class FiveSecondTaskJob implements ShouldQueue
  12. {
  13. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  14. /**
  15. * Create a new job instance.
  16. *
  17. * @return void
  18. */
  19. public function __construct()
  20. {
  21. //
  22. }
  23. /**
  24. * Execute the job.
  25. *
  26. * @return void
  27. */
  28. public function handle()
  29. {
  30. // 你的业务逻辑
  31. IssueService::getLatestIssue(); // 获取最新的期号
  32. // 重要:5秒后再次分发自己
  33. dispatch(new self())->delay(now()->addSeconds(5));
  34. }
  35. // 可选:失败处理
  36. public function failed(\Throwable $exception)
  37. {
  38. Log::error('任务失败: ' . $exception->getMessage());
  39. // 可选:重新分发或通知
  40. dispatch(new self())->delay(now()->addSeconds(5));
  41. }
  42. }