laelaps/symfony-gearman-bundle

This package is abandoned and no longer maintained. No replacement package was suggested.

Integrates Gearman into Symfony

Installs: 36 453

Dependents: 0

Suggesters: 0

Security: 0

Stars: 14

Watchers: 3

Forks: 9

Open Issues: 1

Type:symfony-bundle

1.2.0 2019-06-07 12:55 UTC

This package is not auto-updated.

Last update: 2023-09-15 13:03:26 UTC


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