david-garcia / resque-bundle
Requires
- php: >=5.5,<7.2
- david-garcia/php-resque: ^1.2
- david-garcia/php-resque-scheduler: ^1.1
- symfony/framework-bundle: >=2.0,<4
Requires (Dev)
- phpunit/phpunit: >=4.8
README
此项目是基于 michelsalib/BCCResqueBundle 的分支,用于 Symfony v2 & Symfony v3。
它受到 resque 的启发,resque 是一个基于 Redis 的 Ruby 库,用于创建后台任务,将它们放置在多个队列中,并稍后处理。
功能
- 创建一个带有容器访问以利用您的 Symfony 服务的工作任务
- 在给定的队列上入队一个带有参数的工作任务
- 在给定的队列上创建后台工作者
- 一个用户界面来监控您的队列、工作者和任务状态
- 能够计划在特定时间或延迟一段时间后运行的任务
- 能够自动重新入队失败的任务,带有回退策略
待办事项
- 更新 README.md(此文件)以匹配所有更新设置
- 日志管理
- 任务状态跟踪
- Redis 配置
- 本地化
- 测试
截图
仪表板
安装和配置
要求
请确保您的机器上已安装 redis: https://redis.ac.cn/
获取包
在您的 composer.json
文件中需要 ResqueBundle
包
{ "require": { ... "david-garcia/resque-bundle": "^1.1" } ... }
要安装,运行 php composer.phar [update|install]
。
将 Bundle 添加到您的应用程序内核
<?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
中定义的同一 root_dir
访问。
可选:配置延迟加载
此包已准备好进行延迟加载,以便仅在真正使用时才连接到 Redis。Symfony2 从 2.3 版本开始支持。要使其工作,需要执行额外步骤。您需要在 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);
在队列上运行工作器
执行以下命令将在 default
队列上创建一个工作
- 在
default
队列上: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
选项以停止所有工作进程。
自动重试
您可以通过为特定作业或所有作业添加重试策略
来自动重试失败的作业
以下将允许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