catch-of-the-day / php-resque-bundle
Requires
- catch-of-the-day/php-resque: dev-master
This package is not auto-updated.
Last update: 2024-09-24 07:02:44 UTC
README
PHPResqueBundle 是 php-resque 项目的衍生 https://github.com/chrisboulton/php-resque/。
此包支持 php-resque 项目的所有功能。包括:工作者、入队、分叉工作者、事件、命名空间等。
安装
此项目使用 composer 项目来安装依赖项。请查看https://github.com/composer/composer 中的 composer.json 文件以安装所需的依赖项。
工作原理
工作者
要启动一个工作者,你需要
$ php app/console resque:worker
这将启动一个在 'resque' 命名空间下名为 'default' 的队列的工作者,具有常规日志输出,迭代间隔为 5 秒。但,当然你可以通过可选参数来更改这些设置
--log (verbose|normal|none)
--interval 1..n
--forkCount 0..n (this will require a PECL module <http://pecl.php.net/package/proctitle>)
[queue_name] separated by commas if you want to work with more than one queue.
所以,如果你想以 10 秒的间隔在详细输出中与 'mailer' 和 'news_subscriptions' 队列一起工作
$ php app/console resque:worker --log verbose --interval 10 mailer,news_subscriptions
命名空间支持
如果你有以下命名空间:php, resque 对于 'php' 命名空间,你有:mailer 队列。对于 'resque' 命名空间,你有:news, parser, ftp 队列。
要仅运行 php 命名空间,你应该这样做
$ php app/console resque:worker "php:*"
要仅运行 "resque" 命名空间下的 'ftp' 和 'parser' 队列
$ php app/console resque:worker resque:ftp,parser
有关更多信息,你可以运行
$ php app/console resque:worker --help
队列
在默认队列中附加一个作业
$ php app/console resque:queue Namespace\\Of\\Class\\SomeJob
你可以使用可选参数 [queue_name] 来定义队列。将 'SomeJob' 放入 'mailer' 队列
$ php app/console resque:queue Namespace\\Of\\Class\SomeJob mailer
如果你使用命名空间
$ php app/console resque:queue Namespace\\Of\\Class\SomeJob your_namespace:mailer
使用 Queue 类(PHPResqueBundle\Resque 命名空间)附加作业
<?php
namespace Your\ProjectBundle\SubprojectBundle;
use PHPResqueBundle\Resque\Queue;
class MyJob {
public function attach() {
Queue::add(__CLASS__, 'default');
}
public function attachWithNamespace() {
Queue:add(__CLASS__, 'your_namespace:news');
}
public function perform() {
echo 'Perform !';
}
}
When you run MyJob->attach() method the job will be saved at 'default' queue.
状态
php-resque 允许我们检查作业状态。在 PHPResqueBundle 中,这可以通过以下方式完成
$ php app/console resque:status 50b0568a3057c4d80641dcee6de7cca9
如果你使用命名空间
$ php app/console resque:status 50b0568a3057c4d80641dcee6de7cca9 --namespace your_namespace
NOTE: if you DO NOT provide --namespace only default namespace will be searched for job status id.
当你在入队作业时提供了哈希状态。
更新作业状态
如果您需要更新作业状态,可以通过以下方式完成
$ php app/console resque:update [job_hash] [new_status]
$ php app/console resque:update [job_hash] [new_status] --namespace your_namespace
状态可以在原始 php-resque 项目中检查 https://github.com/chrisboulton/php-resque/
事件
使用事件,您可以在您的作业类中执行其他活动。实际上,php-resque 项目有这些事件
beforeFirstFork
,beforeFork
,afterFork
,beforePerform
,afterPerform
,onFailure
,afterEnqueue
。请参阅 php-resque 项目以获取有关这些事件的文档。
将 beforePerform 事件添加到 MyJob 类中
<?php
namespace Your\ProjectBundle\SubprojectBundle;
use PHPResqueBundle\Resque\Event;
class MyJob {
public function __construct() {
Event::beforePerform(__CLASS__, 'doitbetter');
}
public static function doitbetter() {
echo "An event was thrown !";
}
public function perform() {
fwrite(STDOUT, 'My_Job Resque running on Symfony 2');
}
}
所有事件都在 Event 类(PHPResqueBundle\Resque\Event)中可用,在运行时映射到现有方法,这些方法将命名为您希望的事件。这些方法的参数始终是:classname(尽可能使用 __CLASS
)和一个回调。回调可以是任何可以通过 call_user_func_array
函数触发的内容。有关回调可能成为的内容的更多信息,请参阅 php-resque 项目。
停止事件
PHP-Resque 提供了一种停止事件的方法。在 PHPResqueBundle 中,这可以通过 [event]Stop 反射方法来完成
<?php
namespace Your\ProjectBundle\SubprojectBundle;
use PHPResqueBundle\Resque\Event;
class MyJob {
public function __construct() {
Event::beforePerformStop(__CLASS__, 'doNotItbetter');
}
public static function doNotItbetter() {
echo "An event has gone away !";
}
public function perform() {
fwrite(STDOUT, 'My_Job Resque running on Symfony 2');
}
}
欢迎提出建议。如果您在使用 PHPResqueBundle 时遇到问题,请在 Github 项目问题追踪器中提交工单。有关 php-resque 的问题或贡献应在该项目的 https://github.com/chrisboulton/php-resque 中提出。如果您想通过代码贡献,请发送 pull request!