定制 laelaps/symfony-gearman-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laelaps/symfony-gearman-bundle

最新稳定版本:1.2.0

Composer 安装命令:

composer require laelaps/symfony-gearman-bundle

包简介

Integrates Gearman into Symfony

README 文档

README

Wrapper for Gearman so that you get command lines tools and can use annotation in Symfony.

Also supports the Symfony web profiling toolbar integration.

Installation

composer.json

{
    "require": {
        "laelaps/symfony-gearman-bundle": "1.*@dev"
    }
}

config.yml

We can configure one server for the client. Because only one is used. You need load balancer if you would like to share the load over multiple Gearman servers.

We can configure multiple servers for the workers. Because they do look for work on all configured Gearman servers.

Gearman cluster

laelaps_gearman:
    client_server: localhost:4730
    worker_servers:
        - localhost:4730

app/AppKernel.php

<?php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Laelaps\GearmanBundle\LaelapsGearmanBundle(),
        // ...
    );
}

Worker supervisor cron tool

There is a simple supervisor bash script available. For instructions, see:

#2 (comment)

Examples

Worker

<?php

// AcmeDemoBundle\Worker\ExampleWorker.php

namespace AcmeDemoBundle\Worker;

use GearmanJob;
use Laelaps\GearmanBundle\Annotation as Gearman;
use Laelaps\GearmanBundle\Worker;
use Symfony\Component\Console\Output\OutputInterface;

class ExampleWorker extends Worker
{
    /**
     * @Gearman\PointOfEntry(name="example_job_name")
     * @param GearmanJob $job
     * @param Symfony\Component\Console\Output\OutputInterface $output
     * @return boolean returning false means job failure
     */
    public function doExampleJob(GearmanJob $job, OutputInterface $output)
    {
        // do your job
    }
}

Running worker

Symfony Style Notation

$ ./app/console gearman:worker:run AcmeBundle:ExampleWorker

Note that this would look for Acme\Bundle\AcmeBundle\Worker\ExampleWorker

$ ./app/console gearman:worker:run ./src/AcmeDemoBundle/Worker/ExampleWorker.php

Wildcard is also available (not recommended but possible - results in single process for multiple workers):

$ ./app/console gearman:worker:run "./src/AcmeDemoBundle/Worker/*.php"

Runs all workers from all bundles:

$ ./app/console gearman:worker:run "./src/*/Worker/*.php"

Calling job from controller

<?php

class ExampleController
{
    public function exampleAction()
    {
        // job name taken from PointOfEntry annotation
        $this->get('laelaps.gearman.client')->doBackground('example_job_name', $optionalWorkload = '');
    }
}

Calling job from command line

$ ./app/console gearman:job:run example_job_name
$ ./app/console gearman:job:run example_job_name optional_workload_string

Consumer (alternative implementation for Worker)

As an alternative to the Worker implementation, there is a Consumer-Handler implementation.

place jobs on the queue with:

<?php
$gearman->doBackground('queueName', serialize($workload));

write a handler like:

<?php

namespace AcmeDemoBundle\Worker;

use Laelaps\GearmanBundle\Worker\HandlerInterface;
use Psr\Log\LoggerInterface;

class ConsumerHandler implements HandlerInterface
{
    /** @var LoggerInterface */
    protected $logger;

    /**
     * @param LoggerInterface $logger
     */
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * {@inheritdoc}
     */
    public function handle($message)
    {
        try {
            $workload = unserialize($message);
            echo $workload;
        } catch (Exception $e) {
            $this->logger->error(sprintf("%s: %s", static::class, $e->getMessage()));

            return false;
        }

        return true;
    }
}

And add this class to your service container with a tag:

    acme.worker.consumer_handler:
        class: AcmeDemoBundle\Worker\ConsumerHandler
        arguments:
            - "@logger"
        tags:
            - { name: laelaps.handler, queue_name: 'queueName'}

and run it with:

$ ./app/console gearman:consumer:queueName

laelaps/symfony-gearman-bundle 适用场景与选型建议

laelaps/symfony-gearman-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 36.47k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2013 年 02 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「bundle」 「gearman」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 laelaps/symfony-gearman-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 laelaps/symfony-gearman-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 laelaps/symfony-gearman-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 36.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 14
  • Watchers: 3
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-02-06