hautelook / gearman-bundle
Symfony2 Bundle 提供提交 Gearman 作业的服务
这个包的官方仓库似乎已经消失,因此该包已被冻结。
Requires
- php: >=5.3.0
- necromant2005/gearman-stats: 1.1
- symfony/console: ~2.1
- symfony/event-dispatcher: ~2.1
- symfony/framework-bundle: ~2.1
Requires (Dev)
- liip/monitor: ~1.0
- symfony/browser-kit: ~2.1
- symfony/class-loader: ~2.1
- symfony/form: ~2.1
- symfony/yaml: ~2.1
Suggests
- ext-gearman: The PECL Gearman extension
- liip/monitor: To enable the gearman monitor
README
提供提交 Gearman 作业接口的包
介绍
此包提供提交 Gearman 作业的服务。作业是需要实现 GearmmanJobInterface
的对象。
安装
当然,您需要安装 Gearman PECL 扩展。假设您已安装 composer.phar 或 composer 二进制文件(或将其添加到您的 composer.json
并运行 composer install)
$ composer require hautelook/gearman-bundle
您可以跟踪 dev-master
,或者使用更稳定的标签(由于各种原因推荐使用)。在 Github 仓库 或 Packagist 上,您总能找到最新的标签。
现在将 Bundle 添加到您的 Kernel 中
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Hautelook\GearmanBundle\HautelookGearmanBundle(), // ... ); }
配置
要配置此包,编辑您的 config.yml
或 config_{环境}.yml
# Hautelook Gearman Bundle hautelook_gearman: servers: server1: host: localhost port: 1234 server2: host: localhost port: 4567
或使用默认值(localhost:4730)
# Hautelook Gearman Bundle hautelook_gearman: servers: default: ~
使用
作业
要开始提交作业,首先创建一个表示作业的类
<?php namespace Acme\DemoBundle\GearmanJob; use Hautelook\GearmanBundle\Model\GearmanJobInterface; class StringReverse implements GearmanJobInterface { private $string; public function setString($string) { $this->string = $string; } /** * {@inheritDoc} */ public function getWorkload() { return array('str' => $this->string); } /** * {@inheritDoc} */ public function setWorkload(array $workload) { if (isset($workload['str'])) { $this->string = $str; } } /** * {@inheritDoc} */ public function getFunctionName() { return 'string_reverse'; } /** * {@inheritDoc} */ public function getUnique() { } }
然后,为了提交作业,您可以这样做
$job = new Acme\DemoBundle\GearmanJob\StringReverse(); $job->setString('string to reverse'); $jobStatus = $this->get('hautelook_gearman.service.gearman')->addJob($job); if (!$jobStatus->isSuccessful()) { $logger->err('Gearman Job ' . $jobStatus->getFunctionName() . ' failed with ' . $jobStatus->getReturnCode()); }
事件监听器
在将工作负载绑定到作业之前,此包将触发一个类型为 gearman.bind.workload
的事件。您可以为添加额外的信息到工作负载、记录日志等添加监听器。
示例监听器
<?php namespace Acme\DemoBundle\EventListener; use Hautelook\GearmanBundle\Event\BindWorkloadDataEvent; class GearmanListener { public function onBindWorkload(BindWorkloadDataEvent $event) { $job = $event->getJob(); $this->injectWorkloadEnvironment($job); } private function injectWorkloadEnvironment($job) { // Do something } }
定义服务,并将其标记为监听器
<service id="acme.gearman.listener" class="Acme\DemoBundle\EventListener\GearmanListener"> <tag name="kernel.event_listener" event="gearman.bind.workload" method="onBindWorkload" /> </service>
Gearman 工作者
您可以使用命令运行单个 gearman 工作者。例如
$ app/console hautelook:gearman:run Fully\\Qualified\\NameSpace\\ToYour\\WorkerClass \ functionToCall jobName[, alternativeJobName,...]
这将在从 job_name
、alternativeJobName
队列中获取作业后调用实例化的 WorkerClass
上的 functionToCall
函数。您的工作者类应如下所示
<?php namespace Fully\\Qualified\\NameSpace\\To\\Your; class WorkerClass { public function functionToCall(\GearmanJob $job) { // Do the work here } }
从工作者访问容器
如果您的工作者需要访问 Symfony DI 容器,您只需让您的工作者类实现 Symfony\Component\DependencyInjection\ContainerAwareInterface
,容器就会为您设置。
Gearman 监视命令
此包提供了一个可以像这样运行的控制台命令
$ app/console hautelook:gearman:status
Status for Server server1
solr_update_item_inventory Jobs: 0 Workers: 0 / 0
solr_update_item_popularity Jobs: 0 Workers: 0 / 0
solr_update_item Jobs: 4261 Workers: 1 / 1
Gearman 监视
此包提供了一个与 Liip 兼容的监视器。您可以通过将以下内容添加到配置中启用它
# Hautelook Gearman Bundle hautelook_gearman: monitor: solr_update_item: queue_size: 10, # Maximum number of jobs in the queue workers: 1 # Minimum number of workers
队列大小违规将导致警告状态,而工作器大小违规将导致临界状态。
待办事项 & 未来计划
- 可以通过作业定义优先级和后台/前台。
- 重新添加Gearman异常。