amadeus-m / resque
用于管理Resque工作队列的Symfony 2-3包
Requires
- chrisboulton/php-resque: dev-master
- chrisboulton/php-resque-scheduler: dev-master
- symfony/console: >=2.8,<4.0
- symfony/framework-bundle: >=2.8,<4.0
- symfony/process: >=2.8,<4.0
Requires (Dev)
- phpunit/phpunit: ~5.4
This package is auto-updated.
Last update: 2024-09-07 00:02:45 UTC
README
此项目正在积极开发中(2016年7月)
ResqueBundle
这是BCCResqueBundle的分支,因为该包不再积极维护。该项目中存在许多待解决的问题、拉取请求和错误,没有活动,因此我们进行了分支,并将在此存储库中积极支持和进一步开发代码。
这也是Mpclarkson\ResqueBundle的重命名,以便将代码置于GitHub组织下,以实现未来的分布式开发
欢迎贡献
resque包提供php-resque与Symfony2-3的集成。它灵感来源于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
的实例为ResqueBundle\Resque
(例如,使用语句) - 更新您的
routing.yml
,将@BCCResque
替换为@ResqueBundle
- 已删除所有命令的
bcc:
前缀 - 停止并重新启动所有工作者
- 容器服务定义
bcc_resque.resque
已替换为resque
。您可以选择搜索并替换此内容或创建以下别名
bcc_resque.resque: alias: resque lazy: true
从Mpclarkson\ResqueBundle迁移
- 在composer中替换为
composer remove mpclarkson/resque-bundle
composer require resquebundle/resque
- 将所有
Mpclarkson\ResqueBundle
替换为ResqueBundle\Resque
- 确保AppKernel.php加载
new ResqueBundle\Resque\ResqueBundle(),
安装和配置
要求
确保您的机器上已安装redis: https://redis.ac.cn/
获取包
将mpclarkson/resque-bundle
添加到您的依赖项
{ "require": { ... "resquebundle/resque": "dev-master" } ... }
要安装,请运行php composer.phar [update|install]
。
将ResqueBundle添加到您的应用程序内核
<?php // app/AppKernel.php public function registerBundles() { return array( // ... new ResqueBundle\Resque\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: ResqueBundle\Resque\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
中定义的同一 root_dir
。
可选,配置延迟加载
该组件已准备就绪,以便进行延迟加载,以便仅在真正需要时连接到 Redis。Symfony2 从 2.3 开始支持此功能。为了使其正常工作,需要执行一个额外的步骤。您需要将代理管理器安装到您的 Symfony2 项目中。有关添加代理管理器的完整文档,请参阅Symfony2 懒服务文档。
创建一个工作
工作是一个 ResqueBundle\Resque\Job
类的子类。如果需要在工作执行期间利用容器,您还可以使用 Mpclarkson\Resque\ContainerAwareJob
。您将不得不实现包含您的作业逻辑的运行方法。
<?php namespace My; use ResqueBundle\Resque\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
队列上创建工作:
default
队列:app/console resque:worker-start default
q1
和q2
队列: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) 在生产中运行工作器,而不是重新发明作业 spawning、监控、停止和重新启动。
以下是一个示例配置文件:
[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 ResqueBundle\Resque\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 ResqueBundle\Resque\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
选项以停止所有工作者。
自动重试
您可以通过为特定作业或所有作业添加 重试策略
来让包自动重试失败的作业。
以下将允许 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