PHP code example of horrible / gearman-bundle

1. Go to this page and download the library: Download horrible/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/ */

    

horrible / gearman-bundle example snippets


    

    namespace AppBundle\Job;

    use Horrible\GearmanBundle\Job\JobInterface;
    use Horrible\GearmanBundle\Workload\WorkloadInterface;

    class Job implements JobInterface
    {
        /**
         * {@inheritDoc}
         */
        public function execute(WorkloadInterface $workload)
        {
            $data = $workload->getDecodedData();
            //do your stuff
        }

        public function getName()
        {
            return 'your:job:name';
        }
    }
    

    php bin/console horrible:worker:work &
    



namespace AppBundle\EventListener;

use Horrible\GearmanBundle\Event\JobFailedEvent;
use Horrible\GearmanBundle\Event\JobFinishedEvent;
use Horrible\GearmanBundle\Event\JobStartedEvent;

class MyEventListener
{
    public function onJobStarted(JobStartedEvent $event)
    {
        $jobName = $event->getJobName();
        $workload = $event->getWorkload();
        $workerId = $event->getWorkerId();
        //Do your stuff...
    }

    public function onJobFinished(JobFinishedEvent $event)
    {
        $jobName = $event->getJobName();
        $workload = $event->getWorkload();
        $jobResult = $event->getJobResult();
        $workerId = $event->getWorkerId();
        //Do your stuff...
    }

    public function onJobFailed(JobFailedEvent $event)
    {
        $jobName = $event->getJobName();
        $workload = $event->getWorkload();
        $exception = $event->getException();
        $workerId = $event->getWorkerId();
        //Do your stuff...
    }
}