bcc / resque-bundle
BCCResqueBundle
Requires
- chrisboulton/php-resque: 1.2
- chrisboulton/php-resque-scheduler: 1.1
- symfony/framework-bundle: >=2.0,<3.0
This package is not auto-updated.
Last update: 2024-09-20 21:27:23 UTC
README
BCC resque 扩展包提供将 php-resque 集成到 Symfony2 的功能。它受到 resque 的启发,resque 是一个支持 Redis 的 Ruby 库,用于创建后台任务、将它们放入多个队列并在以后处理。
功能
- 创建一个作业,以便在作业执行期间访问容器以利用您的 Symfony 服务
- 在给定的队列上使用参数入队一个作业
- 在给定的队列上创建后台工作进程
- 一个用户界面,用于监控您的队列、工作进程和作业状态
- 能够在特定时间或延迟数秒后运行作业的能力
- 自动重新入队失败作业的能力,并具有退避策略
待办事项
- 日志管理
- 作业状态跟踪
- Redis 配置
- 本地化
- 测试
屏幕截图
仪表板
安装和配置
要求
确保您的计算机上已安装 Redis: https://redis.ac.cn/
获取扩展包
将 bcc-resque-bundle
添加到您的依赖项中
{ "require": { ... "bcc/resque-bundle": "dev-master" } ... }
要安装,请运行 php composer.phar [update|install]
。
将 BCCResqueBundle 添加到您的应用程序内核
<?php // app/AppKernel.php public function registerBundles() { return array( // ... new BCC\ResqueBundle\BCCResqueBundle(), // ... ); }
导入路由配置
将配置添加到您的 routing.yml
# app/config/routing.yml BCCResqueBundle: resource: "@BCCResqueBundle/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 bcc_resque: class: BCC\ResqueBundle\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
的更多信息,请参阅自动重试部分。
如果工作进程系统托管在与创建队列的系统不同的服务器/目录上,则在作业失败时运行时设置 worker: root_dir:
。当运行为多个配置应用程序的多个工作进程时,所有应用程序都必须能够通过在 worker: root_dir
中定义的同一根目录访问。
可选,配置懒加载
此扩展包已准备好进行懒加载,以便仅在真正需要时连接到 Redis。从 2.3 开始,Symfony2 支持。要使其工作,需要执行额外步骤。您需要将代理管理器安装到您的 Symfony2 项目中。有关添加代理管理器的完整文档可以在 Symfony2 懒服务文档 中找到。
创建作业
作业是 BCC\ResqueBundle\Job
类的子类。如果您需要在作业执行期间利用容器,还可以使用 BCC\Resque\ContainerAwareJob
。您将被迫实现包含您的作业逻辑的 run 方法。
<?php namespace My; use BCC\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('bcc_resque.resque'); // create your job $job = new MyJob(); $job->args = array( 'file' => '/tmp/file', 'content' => 'hello', ); // enqueue your job $resque->enqueue($job);
在队列上运行工作进程
执行以下命令将在
- 默认队列上创建工作:
app/console bcc:resque:worker-start default
- 在
q1
和q2
队列上:app/console bcc:resque:worker-start q1,q2
(使用,
分隔名称) - 所有现有队列:
app/console bcc: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('bcc_resque.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 bcc:resque:scheduledworker-start
稍后使用app/console bcc:resque:scheduledworker-stop
停止它。
注意,在后台模式下运行时,它将在'cache//bcc_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/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 BCC\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 BCC\ResqueBundle\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]
default
策略(如果提供)将应用于没有特定策略附加的所有作业。如果没有提供,这些作业将不会自动重试。
您可以使用空数组禁用选定作业的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