kuborgh/queue-bundle

实现了一个用于异步处理symfony命令的队列

安装次数: 1,705

依赖者: 0

建议者: 0

安全: 0

星标: 3

关注者: 6

分支: 0

开放问题: 1

类型:symfony-bundle

v1.0.1 2016-05-03 10:40 UTC

This package is not auto-updated.

Last update: 2024-09-13 15:30:31 UTC


README

这是一个symfony命令的队列实现。它通过将命令写入数据库并使用由cron job启动的队列运行器来异步执行symfony命令。

具有以下特性

  • 纯PHP实现 - 服务器上无需额外软件
  • 基于cron的运行检测 - 定期检查队列是否仍在运行
  • 停滞检测 - 通过PID检查命令是否仍在运行或已静默失败
  • 作业之间的短等待状态 - 不需要等待一分钟直到cron再次运行。在 < 10s 内开始作业。
  • symfony命令用于监控和队列
  • MySQL数据库存储 - 这使得调试洞察和操作变得容易
  • 可配置并发性 - 在需要时并行运行更多作业
  • 优先级 - 5个优先级级别,以便优先执行重要命令
  • 重复检查 - 当第一次运行仍在等待时,跳过将相同的命令添加到队列两次

安装

步骤 1: 下载Bundle

打开命令行,进入项目目录并执行以下命令以下载此bundle的最新稳定版本

$ composer require kuborgh/queue-bundle

步骤 2: 启用Bundle

然后,通过将其添加到项目中 app/AppKernel.php 文件中注册的bundle列表中,来启用bundle

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new Kuborgh\QueueBundle\KuborghQueueBundle(),
        );

        // ...
    }

    // ...
}

步骤 3: 配置cron

cron作业的目的是确保队列运行器始终运行。这在服务器重启时非常有用,当队列运行器被杀死或由于错误而终止时。cron间隔是队列在最坏情况下可能不运行的最大时间,所以1小时在大多数情况下应该绝对没问题。

0 * * * * cd <your-installation>; php app/console queue:runner > /dev/null 2>&1

如果您对队列运行器的输出感兴趣,可以将它重定向到文件中

0 * * * * (cd <your-installation>; echo `date` START; php app/console queue:runner; echo `date` END) >> logs/queue-runner.log 2>&1

配置

以下配置变量存在,可以将其插入项目的 config.yml 中

kuborgh_queue:
    # How many queue jobs can be run parallel
    concurrency: 1

    # Remove successfull jobs from the queue after 3 days
    auto_cleanup: true

    # Path to console command to execute the jobs
    console_path: %kernel.root_dir%/console

调试/监控

要查看队列当前正在做什么,最好的地方是数据库或日志文件。在终端,以下命令最有助于查看当前进程

$ ps uxf

Logentries集成

通过使用kuborgh/logentries-bundle,可以将队列的统计信息定期发送到logentries。这允许您设置一个警报,当队列挂起时,或识别长时间运行的作业。

  1. 安装和配置kuborgh/logentries-bundle以拥有一个名为 queue 的记录器服务。
kuborgh_logentries:
    logger:
        queue:
            log_set: MyHost
            log: Queue
  1. 将作业添加到crontab中
# Report state to logentries.com
*/5 * * * * php app/console queue:logentries > /dev/null 2>&1