ImportQueueService.php 945 B

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