PHP code example of laelaps / symfony-gearman-bundle

1. Go to this page and download the library: Download laelaps/symfony-gearman-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

laelaps / symfony-gearman-bundle example snippets

 php


public function registerBundles()
{
    $bundles = array(
        // ...
        new Laelaps\GearmanBundle\LaelapsGearmanBundle(),
        // ...
    );
}
 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
    }
}
 php


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

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

 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;
    }
}