Symfony Gearman Bundle
Wrapper for Gearman so that you get command lines tools and can use annotation in Symfony., (*1)
Also supports the Symfony web profiling toolbar integration., (*2)
Installation
composer.json
``` yaml
{
"require": {
"laelaps/symfony-gearman-bundle": "1.*@dev"
}
}, (*3)
### 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](http://gearman.org/img/cluster.png)
``` yaml
laelaps_gearman:
client_server: localhost:4730
worker_servers:
- localhost:4730
app/AppKernel.php
``` php
<?php, (*4)
public function registerBundles()
{
$bundles = array(
// ...
new Laelaps\GearmanBundle\LaelapsGearmanBundle(),
// ...
);
}, (*5)
## 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
<?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, (*6)
$ ./app/console gearman:worker:run AcmeBundle:ExampleWorker
Note that this would look for Acme\Bundle\AcmeBundle\Worker\ExampleWorker, (*7)
$ ./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):, (*8)
$ ./app/console gearman:worker:run "./src/AcmeDemoBundle/Worker/*.php"
Runs all workers from all bundles:, (*9)
$ ./app/console gearman:worker:run "./src/*/Worker/*.php"
Calling job from controller
``` php
<?php, (*10)
class ExampleController
{
public function exampleAction()
{
// job name taken from PointOfEntry annotation
$this->get('laelaps.gearman.client')->doBackground('example_job_name', $optionalWorkload = '');
}
}, (*11)
### Calling job from command line
$ ./app/console gearman:job:run example_job_name, (*12)
$ ./app/console gearman:job:run example_job_name optional_workload_string, (*13)
### 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:, (*14)
``` php
<?php, (*15)
namespace AcmeDemoBundle\Worker;, (*16)
use Laelaps\GearmanBundle\Worker\HandlerInterface;
use Psr\Log\LoggerInterface;, (*17)
class ConsumerHandler implements HandlerInterface
{
/** @var LoggerInterface */
protected $logger;, (*18)
/**
* @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;
}
}, (*19)
And add this class to your service container with a tag:
``` yaml
acme.worker.consumer_handler:
class: AcmeDemoBundle\Worker\ConsumerHandler
arguments:
- "@logger"
tags:
- { name: laelaps.handler, queue_name: 'queueName'}
and run it with:, (*20)
$ ./app/console gearman:consumer:queueName