mcfedr/periodic-queue-driver-bundle

此包已被废弃,不再维护。作者建议使用 mcfedr/queue-manager-bundle 包。

用于使用 doctrine 作为驱动运行 symfony 背景任务的包

1.5.0 2018-10-11 06:17 UTC

This package is auto-updated.

Last update: 2022-02-01 13:05:26 UTC


README

这是一个为 Queue Manager Bundle 实现的驱动,它使用定期运行任务。

此驱动本身不运行任务,它需要另一个驱动来实际处理任务。

Latest Stable Version License Build Status

使用方法

此驱动没有运行守护进程,因为它只是连接到其他驱动。可以通过使用带有 period 选项的 put 将任务放入此驱动来使用它。

安装

Composer

composer require mcfedr/periodic-queue-driver-bundle

AppKernel

将包包含到您的 AppKernel 中

public function registerBundles()
{
    $bundles = [
        ...
        new Mcfedr\QueueManagerBundle\McfedrQueueManagerBundle(),
        new Mcfedr\PeriodicQueueDriverBundle\McfedrPeriodicQueueDriverBundle(),

配置

安装此包后,您可以设置类似以下的队列管理器配置

mcfedr_queue_manager:
    managers:
        periodic:
            driver: periodic
            options:
                default_manager: delay
                default_manager_options: []

这将创建一个名为 "mcfedr_queue_manager.periodic"QueueManager 服务

  • default_manager - 默认工作处理器,必须支持延迟任务,例如 Doctrine Delay
  • default_manager_options - 传递给工作处理器的默认选项 put

传递给 QueueManager::put 的选项

  • period - 任务运行之间的平均秒数
  • manager - 使用不同的工作处理器处理此任务
  • manager_options - 传递给处理器 put 方法的选项

说明

命令

有两个命令可以用来调查任务分布的情况。一种简单的方法是将输出通过 feedgnuplot

rand-add 使用一种简单的方法,即在下一个运行时间添加一个随机数来获取时间

./tests/console test:distribution:rand-add -v | feedgnuplot --histogram 0 --binwidth 60 --hardcopy "rand-add.png" --exit

Graph of rand-add

periodic 使用包实现

./tests/console test:distribution:periodic -v | feedgnuplot --histogram 0 --binwidth 60 --hardcopy "periodic.png" --exit

Graph of periodic

任务令牌

有一个 job_tokens 参数可以帮助确保任务唯一性。

使用示例

当放入新的周期性任务时,您会得到 PeriodicJob 对象。

    $periodicJob = $this->manager->put('some_service', [
        'some_argument' => 'some_value',
    ], [
        'period' => 'some_seconds',
    ], 'periodic');

您可以对其进行 $periodicJob->getToken() 操作并存储令牌以用于任务执行

在执行过程中

 public function execute(array $arguments)
    {
        // Get the stored token
        ...

        if ($storedToken != $arguments['job_tokens']['token']) {
            throw new InvalidTokenException();
        }
        
        ...
        
        $storedToken = ($arguments['job_tokens']['next_token']);

        // Save stored token for next execition.
    }

每次任务执行都会生成新的令牌,因此如果出现某些任务重复,只有一个将被执行,其他任务将失败