GatherQueueService.php 977 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service;
  4. use App\Job\GatherExampleJob;
  5. use Hyperf\AsyncQueue\Driver\DriverFactory;
  6. use Hyperf\AsyncQueue\Driver\DriverInterface;
  7. /**
  8. * @Job(name="default")
  9. */
  10. class GatherQueueService
  11. {
  12. protected DriverInterface $driver;
  13. public function __construct(DriverFactory $driverFactory)
  14. {
  15. $this->driver = $driverFactory->get('default');
  16. }
  17. /**
  18. * 生产消息.
  19. * @param $params 数据
  20. * @param int $delay 延时时间 单位秒
  21. */
  22. public function push($params, int $delay = 0): bool
  23. {
  24. // 这里的 `ExampleJob` 会被序列化存到 Redis 中,所以内部变量最好只传入普通数据
  25. // 同理,如果内部使用了注解 @Value 会把对应对象一起序列化,导致消息体变大。
  26. // 所以这里也不推荐使用 `make` 方法来创建 `Job` 对象。
  27. return $this->driver->push(new GatherExampleJob($params), $delay);
  28. }
  29. }