Developed with love by KnpLabs Hire us for your project!
0

symfony-gearman-bundle

by laelaps

This bundle is finished and ready for production purposes.

Symfony Gearman Bundle

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:

https://github.com/laelaps/symfony-gearman-bundle/issues/2#issuecomment-16257507

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
<?php
$gearman->doBackground('queueName', serialize($workload));


write a handler like:

``` php
<?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
Copyright (c) 2014 laelaps

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
laelaps_gearman:
client_server: ~ # Required
worker_servers: []