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

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: []
  • Update composer version
    By istaveren, 1 year ago
  • Return empty line at an interrupted process.
    By istaveren, 1 year ago
  • Make sure that timeout is called when process is stopped.
    By istaveren, 1 year ago
  • Merge branch 'master' of https://github.com/laelaps/symfony-gearman-bundle
    By istaveren, 2 years ago
  • Updating description
    By istaveren, 2 years ago
  • Remove php 7.x specific things to match >= 5.3 requirement.
    By istaveren, 2 years ago
  • Merge branch 'master' of https://github.com/laelaps/symfony-gearman-bundle
    By istaveren, 2 years ago
  • One server for client, multiple servers for workers.
    By istaveren, 2 years ago
  • Merge pull request #7 from lvanderree/alternativeWorker
    By web-flow, 2 years ago
  • added alternative worker implementation
    By Leon van der Ree, 2 years ago
  • add support for newer symfony versions
    By Leon van der Ree, 2 years ago
  • Adding a timeout option to the workers.
    By istaveren, 2 years ago
  • Log also Gearman request that throw an exception.
    By istaveren, 3 years ago
  • Wrong filename string used.
    By istaveren, 3 years ago
  • Merge branch 'master' of github.com:laelaps/symfony-gearman-bundle
    By istaveren, 3 years ago
  • Adding getServers so that you can get the list of configured services.
    By istaveren, 3 years ago
  • Support logger for multiple Gearman clients.
    By istaveren, 5 years ago
  • Show the request to gearman when possible.
    By istaveren, 5 years ago
  • Merge branch 'master' of github.com:laelaps/symfony-gearman-bundle
    By istaveren, 5 years ago
  • Create LICENSE
    By Mateusz Charytoniuk, 5 years ago
  • Adding collector information and twig template
    By istaveren, 6 years ago
  • Remove old Extension
    By isometriks, 6 years ago
  • Remove old Extension
    By isometriks, 6 years ago
  • Allowed usage of Symfony-style notation to init workers.
    By isometriks, 6 years ago
  • Dynamic servers
    By isometriks, 6 years ago
  • Updated readme
    By isometriks, 6 years ago
  • Rename Bundle so it has proper namespace "laelaps_gearman" instead of "gearman"
    By isometriks, 6 years ago
  • Added semantic config
    By isometriks, 6 years ago
  • Removed PHP 5.4 call method from array syntax. Using call_user_func_array http://php.net/manual/en/function.call-user-func-array.php
    By Mateusz Charytoniuk, 6 years ago
  • Removed PHP 5.4 array syntax shortcut: https://wiki.php.net/rfc/shortsyntaxforarrays
    By Mateusz Charytoniuk, 6 years ago