mpclarkson/resque-bundle

该包已被弃用且不再维护。作者建议使用 https://github.com/resquebundle/resque 包。

Symfony 2-3 扩展包,用于管理 Resque 作业队列

安装: 397

依赖者: 0

建议者: 0

安全: 0

星标: 5

关注者: 4

分支: 4

开放问题: 0

类型:symfony-bundle

dev-master / 1.x-dev 2016-08-03 19:36 UTC

This package is auto-updated.

Last update: 2022-02-01 12:59:50 UTC


README

Scrutinizer Code Quality Build Status

此仓库已迁移至其自己的 GitHub 组织,此处代码仅用于依赖者。新的代码、错误修复、拉取请求等应在新仓库中完成:https://github.com/resquebundle/resque

这是一个 BCCResqueBundle 的分支,因为该包不再维护。有许多悬而未决的问题、拉取请求和需要修复的错误。 欢迎贡献 - 我没有能力独自维护。

resque 扩展包提供了将 php-resque 集成到 Symfony2-3 的功能。它受到了 resque 的启发,resque 是一个基于 Redis 的 Ruby 库,用于创建后台作业,将它们放置在多个队列中,并在稍后进行处理。

特性

  • 创建一个作业,以便利用您的 Symfony 服务容器访问
  • 在指定的队列中排队一个带有参数的作业
  • 在指定队列上创建后台工作进程
  • 监控您的队列、工作进程和作业状态的接口
  • 安排作业在特定时间或延迟秒数后运行
  • 自动重新排队失败的作业,带有回退策略

待办事项

  • PSR4
  • 更新管理界面到 Bootstrap 3
  • BCC笔记迁移
  • Travis CI
  • Symfony 3 兼容性
  • 实现完整单元测试
  • 决定支持 PHP 7+ ;-)
  • 代码质量 - Scrutinizer 9.5+
  • 复制 resque-web ruby 库的功能(例如重试和删除失败的作业等)
  • 社区贡献 / 忽略的 PR
  • 修复错误

原始待办事项

  • 日志管理
  • 作业状态跟踪
  • Redis 配置
  • 本地化
  • 测试

BCCResqueBundle 迁移

以下是一些简化从 BCCResqueBundle 迁移的笔记

  • 在您的应用程序中找到并替换所有 BCC\ResqueBundle 实例为 Mpclarkson\ResqueBundle(例如使用语句)
  • 通过将 @BCCResque 替换为 @ResqueBundle 更新您的 routing.yml
  • 已删除所有命令的 bcc: 前缀
  • 停止并重新启动所有工作进程
  • 容器服务定义 bcc_resque.resque 已被替换为 resque。您可以搜索并替换此内容,或按以下方式创建别名
 bcc_resque.resque:
      alias: resque
      lazy: true

安装和配置

要求

请确保您已在您的机器上安装了 Redis: https://redis.ac.cn/

获取包

mpclarkson/resque-bundle 添加到依赖中

{
    "require": {
        ...
        "mpclarkson/resque-bundle": "dev-master"
    }
    ...
}

要安装,运行 php composer.phar [update|install]

将 ResqueBundle 添加到您的应用内核

<?php

    // app/AppKernel.php
    public function registerBundles()
    {
        return array(
            // ...
            new Mpclarkson\ResqueBundle\ResqueBundle(),
            // ...
        );
    }

导入路由配置

将以下内容添加到 routing.yml

# app/config/routing.yml
ResqueBundle:
    resource: "@ResqueBundle/Resources/config/routing.xml"
    prefix:   /resque

您可以按需自定义前缀。

您现在可以通过以下 URL 访问仪表板: /resque

为了保护仪表板,您可以将以下内容添加到您的 security.yml 中,假设您的管理员角色是 ROLE_ADMIN

access_control:
  - { path: ^/resque, roles: ROLE_ADMIN }

现在只有具有角色 ROLE_ADMIN 的用户才能访问此 URL 的仪表板: /resque

可选:设置配置

您可能想在您的 config.yml 中添加一些配置

# app/config/config.yml
resque:
    class: Mpclarkson\ResqueBundle\Resque    # the resque class if different from default
    vendor_dir: "%kernel.root_dir%/../vendor"  # the vendor dir if different from default
    app_include: /pathto/bootstrap.php.cache # app include file if different from default (i.e. /var/bootstrap.php.cache)
    prefix: my-resque-prefix                 # optional prefix to separate Resque data per site/app
    redis:
        host: localhost                      # the redis host
        port: 6379                           # the redis port
        database: 1                          # the redis database
    auto_retry: [0, 10, 60]                  # auto retry failed jobs
    worker:
        root_dir: path/to/worker/root        # the root dir of app that run workers (optional)

有关如何使用 auto_retry 的更多信息,请参阅 自动重试 部分。

当工作系统托管在创建队列的独立服务器/目录上时,设置 worker: root_dir: 以防止作业运行失败。当运行为多个工作者配置的多个应用时,所有应用都必须能够通过在 worker: root_dir 中定义的相同根目录访问。

可选:配置懒加载

此包已准备就绪,以便在真正使用时才建立与 Redis 的连接。从 2.3 版本开始,Symfony2 支持。为了使其正常工作,需要执行额外一步。您需要在您的 Symfony2 项目中安装一个代理管理器。有关添加代理管理器的完整文档,请参阅 Symfony2 懒服务文档

创建作业

作业是 Mpclarkson\ResqueBundle\Job 类的子类。如果您需要在作业执行期间利用容器,还可以使用 Mpclarkson\Resque\ContainerAwareJob。您必须实现包含您的作业逻辑的 run 方法。

<?php

namespace My;

use Mpclarkson\ResqueBundle\Job;

class MyJob extends Job
{
    public function run($args)
    {
        file_put_contents($args['file'], $args['content']);
    }
}

如您所见,您获得了一个 $args 参数,它是您的作业的参数数组。

将作业添加到队列

您可以通过容器简单地获取 resque 服务。从您的控制器中,您可以这样做

<?php

// get resque
$resque = $this->get('resque');

// create your job
$job = new MyJob();
$job->args = array(
    'file'    => '/tmp/file',
    'content' => 'hello',
);

// enqueue your job
$resque->enqueue($job);

在队列上运行工作者

执行以下命令将在

  • default 队列上创建工作: app/console resque:worker-start default
  • q1q2 队列: app/console resque:worker-start q1,q2(使用 , 分隔名称)
  • 所有现有队列: app/console resque:worker-start "*"

您还可以通过添加 --foreground 选项来在后台运行工作者;

默认情况下,调用 php-resque 时设置 VERBOSE 环境变量

  • --verbose 选项设置 VVERBOSE
  • --quiet 禁用两者,因此不会抛出调试输出

有关 php-resque 日志选项,请参阅: https://github.com/chrisboulton/php-resque#logging

将延迟作业添加到队列

您可以指定作业在特定时间运行或在特定延迟(以秒为单位)后运行。

从您的控制器,您可以执行以下操作

<?php

// get resque
$resque = $this->get('resque');

// create your job
$job = new MyJob();
$job->args = array(
    'file'    => '/tmp/file',
    'content' => 'hello',
);

// enqueue your job to run at a specific \DateTime or int unix timestamp
$resque->enqueueAt(\DateTime|int $at, $job);

// or

// enqueue your job to run after a number of seconds
$resque->enqueueIn($seconds, $job);

您还必须运行一个scheduledworker,它负责从特殊的延迟队列中取出项目并将它们放入最初指定的队列。

app/console resque:scheduledworker-start

稍后停止它,使用app/console resque:scheduledworker-stop

注意,当以后台模式运行时,它将在'cache//resque_scheduledworker.pid'中创建一个PID文件。如果在scheduledworker运行时清除缓存,您将无法使用scheduledworker-stop命令停止它。

或者,您可以使用--foreground选项在前台运行scheduledworker。

注意,您应该始终只有一个scheduledworker正在运行,如果PID文件已经存在,您将必须使用--force选项来启动scheduledworker。

使用supervisord管理生产作业

最好使用supervisord(http://supervisord.org)来在生产中运行作业,而不是重新发明作业生成、监控、停止和重新启动。

以下是一个示例配置文件

[program:myapp_phpresque_default]
command = /usr/bin/php /home/sites/myapp/bin/console resque:worker-start high --env=prod --foreground --verbose
user = myusername
stopsignal=QUIT

[program:myapp_phpresque_scheduledworker]
command = /usr/bin/php /home/sites/myapp/prod/bin/console resque:scheduledworker-start --env=prod --foreground --verbose
user = myusername
stopsignal=QUIT

[group:myapp]
programs=myapp_phpresque_default,myapp_phpresque_scheduledworker

(如果您使用自定义的Resque前缀,请添加一个额外的环境变量:PREFIX='my-resque-prefix')

然后在Capifony中,您可以这样做

在部署您的应用程序之前使用sudo supervisorctl stop myapp:*,部署完成后使用sudo supervisorctl start myapp:*

更多功能

更改队列

您只需通过设置作业的queue字段来更改作业队列

在作业内部

<?php

namespace My;

use Mpclarkson\ResqueBundle\Job;

class MyJob extends Job
{
    public function __construct()
    {
        $this->queue = 'my_queue';
    }

    public function run($args)
    {
        ...
    }
}

或在作业外部

<?php

// create your job
$job = new MyJob();
$job->queue = 'my_queue';

从作业内部访问容器

只需扩展ContainerAwareJob

<?php

namespace My;

use Mpclarkson\ResqueBundle\ContainerAwareJob;

class MyJob extends ContainerAwareJob
{
    public function run($args)
    {
        $doctrine = $this->getContainer()->getDoctrine();
        ...
    }
}

停止作业

使用app/console resque:worker-stop命令。

  • 没有参数将显示可以停止的运行中的作业。
  • 添加一个作业ID来停止它:app/console resque:worker-stop ubuntu:3949:default
  • 添加--all选项来停止所有作业。

自动重试

您可以通过添加特定作业或所有作业的retry strategy来让包自动重试失败的作业

以下将允许Some\Job重试3次。

  • 立即
  • 延迟10秒后
  • 延迟60秒后
resque:
    redis:
        ....
    auto_retry:
        Some\Job: [0, 10, 60]

设置所有作业的策略

resque:
    auto_retry: [0, 10, 60]

对于除特定作业之外的所有作业使用默认策略

resque:
    auto_retry:
    	default:        [0, 10, 60]
        Some\Job:       [0, 10, 120, 240]
        Some\Other\Job: [10, 30, 120, 600]

default策略(如果提供)将应用于所有没有特定策略附加的作业。如果没有提供,这些作业将没有自动重试。

您可以使用空数组来禁用特定作业的auto_retry

resque:
    auto_retry:
    	default:        [0, 10, 60]
        Some\Job:       []
        Some\Other\Job: [10, 30, 120, 600]

在此处,Some\Job将没有任何auto_retry附加。

请注意

要使用auto_retry功能,您还必须运行调度作业

app/console resque:scheduledworker-start