konekt / resque-ex-bundle
ResqueEx 的 Symfony 扩展包
Requires
- chrisboulton/php-resque: 1.2
- chrisboulton/php-resque-scheduler: 1.1
- symfony/framework-bundle: >=2.0,<3.0
This package is auto-updated.
Last update: 2024-09-18 20:27:38 UTC
README
这是一个 BCCResqueBundle 的分支,目的是用 php-resque-ex 替换 php-resque
resque-ex 扩展包提供将 php-resque-ex 集成到 Symfony2 中。它灵感来源于 resque,这是一个基于 Redis 的 Ruby 库,用于创建后台任务,将它们放置在多个队列中,并在稍后处理。
特性
- 创建一个具有容器访问权限的作业,以便利用您的 Symfony 服务
- 在指定队列上入队一个作业,并带有参数
- 在指定队列上创建一个后台工作者
- 一个用于监控您的队列、工作者和作业状态的 UX
- 能够按指定时间或延迟秒数调度作业运行
- 能够自动重新入队失败作业,并带有回退策略
待办事项
- 日志管理
- 作业状态跟踪
- Redis 配置
- 本地化
- 测试
屏幕截图
仪表板
安装和配置
需求
确保您的机器上已安装 redis:https://redis.ac.cn/
获取扩展包
执行 composer require konekt/resque-ex-bundle
或
将 resque-ex-bundle
添加到您的依赖中
{ "require": { ... "konekt/resque-ex-bundle": "dev-master" } ... }
然后运行 php composer.phar [update|install]
以进行安装。
将 ResqueExBundle 添加到您的应用程序内核中
<?php // app/AppKernel.php public function registerBundles() { return array( // ... new Konekt\ResqueExBundle\ResqueExBundle(), // ... ); }
导入路由配置
将以下内容添加到您的 routing.yml
# app/config/routing.yml KonektResqueExBundle: resource: "@KonektResqueExBundle/Resources/config/routing.xml" prefix: /resque
您可以根据需要自定义前缀。
您现在可以通过此 URL 访问仪表板: /resque
为了安全仪表板,您可以将以下内容添加到您的 security.yml
- 假设您的管理员角色为 ROLE_ADMIN
access_control: - { path: ^/resque, roles: ROLE_ADMIN }
可选,通过角色保护仪表板
将以下内容添加到您的 security.yml
# app/config/security.yml access_control: - { path: ^/resque, roles: ROLE_ADMIN }
现在只有具有角色 ROLE_ADMIN 的用户才能通过此 URL 访问仪表板: /resque
可选,设置配置
您可能希望将一些配置添加到您的 config.yml
# app/config/config.yml konekt_resqueex: class: Konekt\ResqueExBundle\Resque # the resque class if different from default vendor_dir: %kernel.root_dir%/../vendor # the vendor dir if different from default 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
的更多信息,请参阅 自动重试 部分。
如果工作系统托管在与其他服务器/dir 相同的单独服务器上,并且当队列创建系统失败时作业无法运行,请设置 worker: root_dir:
。当运行为多个工作者配置的多个应用程序时,所有应用程序都必须能够通过在 worker: root_dir
中定义的相同 root_dir 访问。
可选,配置懒加载
此扩展包已准备好进行懒加载,以便仅在真正需要时连接到 Redis。从 2.3 版本开始,Symfony2 支持此功能。为了使其工作,需要执行一个额外的步骤。您需要将代理管理器安装到您的 Symfony2 项目中。有关添加代理管理器的完整文档,请参阅 Symfony2 懒服务文档。
创建作业
任务类是 Konekt\ResqueExBundle\Job
类的子类。如果您需要在任务执行期间利用容器,也可以使用 Konekt\ResqueExBundle\ContainerAwareJob
。您将不得不实现一个包含您任务逻辑的 run 方法。
<?php namespace My; use Konekt\ResqueExBundle\Job; class MyJob extends Job { public function run($args) { \file_put_contents($args['file'], $args['content']); } }
如您所见,您会得到一个 $args 参数,这是您的任务参数数组。
将任务添加到队列
您可以通过容器简单地获取 resque 服务。从您的控制器中,您可以这样做:
<?php // get resque $resque = $this->get('konekt_resqueex.resque'); // create your job $job = new MyJob(); $job->args = array( 'file' => '/tmp/file', 'content' => 'hello', ); // enqueue your job $resque->enqueue($job);
在队列上运行工作进程
执行以下命令将在 default
队列上创建工作:
- 在
default
队列上:app/console konekt:resque:worker-start default
- 在
q1
和q2
队列上:app/console konekt:resque:worker-start q1,q2
(使用,
分隔名称) - 所有现有队列:
app/console konekt: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('konekt_resqueex.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 konekt:resque:scheduledworker-start
稍后使用 app/console konekt:resque:scheduledworker-stop
停止它。
请注意,在后台模式下运行时,它会在 'cache//konekt_resqueex_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/prod/current/vendor/bcc/resque-bundle/BCC/ResqueBundle/bin/resque user = myusername environment = APP_INCLUDE='/home/sites/myapp/prod/current/vendor/autoload.php',VERBOSE='1',QUEUE='default' stopsignal=QUIT [program:myapp_phpresque_scheduledworker] command = /usr/bin/php /home/sites/myapp/prod/current/vendor/bcc/resque-bundle/BCC/ResqueBundle/bin/resque-scheduler user = myusername environment = APP_INCLUDE='/home/sites/myapp/prod/current/vendor/autoload.php',VERBOSE='1',RESQUE_PHP='/home/sites/myapp/prod/current/vendor/chrisboulton/php-resque/lib/Resque.php' 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 Konekt\ResqueExBundle\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 Konekt\ResqueExBundle\ContainerAwareJob; class MyJob extends ContainerAwareJob { public function run($args) { $doctrine = $this->getContainer()->getDoctrine(); ... } }
停止工作进程
使用 app/console bcc:resque:worker-stop
命令。
- 没有参数将显示您可以停止的运行中的工作进程。
- 添加工作进程 ID 来停止它:
app/console bcc:resque:worker-stop ubuntu:3949:default
- 添加
--all
选项来停止所有工作进程。
自动重试
您可以通过添加 retry strategy
为特定的作业或为所有作业添加自动重试策略
以下将允许 Some\Job
重试 3 次。
- 立即
- 延迟 10 秒后
- 延迟 60 秒后
bcc_resque: redis: .... auto_retry: Some\Job: [0, 10, 60]
为所有作业设置策略
bcc_resque: auto_retry: [0, 10, 60]
为除特定作业外的所有作业设置默认策略
bcc_resque: auto_retry: default: [0, 10, 60] Some\Job: [0, 10, 120, 240] Some\Other\Job: [10, 30, 120, 600]
默认策略(如果提供)将应用于所有没有特定策略附加的作业。如果没有提供,这些作业将不会自动重试。
您可以通过使用空数组来禁用选定作业的 auto_retry
。
bcc_resque: auto_retry: default: [0, 10, 60] Some\Job: [] Some\Other\Job: [10, 30, 120, 600]
在这里,Some\Job
将不会附加任何 auto_retry
。
请注意:
要使用 auto_retry
功能,您还必须运行调度作业
app/console bcc:resque:scheduledworker-start