hautelook / gearman-bundle

Symfony2 Bundle 提供提交 Gearman 作业的服务

这个包的官方仓库似乎已经消失,因此该包已被冻结。

安装次数: 69,877

依赖者: 0

建议者: 0

安全: 0

星标: 20

关注者: 22

分支: 13

开放性问题: 6

类型:symfony-bundle

0.5.8 2014-01-07 16:48 UTC

This package is not auto-updated.

Last update: 2022-01-22 01:29:04 UTC


README

提供提交 Gearman 作业接口的包

Build Status Scrutinizer Quality Score

介绍

此包提供提交 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.ymlconfig_{环境}.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_namealternativeJobName 队列中获取作业后调用实例化的 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异常。