2017 © Pedro Peláez
 

symfony-bundle queue-bundle

Queue (producers + consumers) system for Symfony framework based on Pheanstalk

image

mate/queue-bundle

Queue (producers + consumers) system for Symfony framework based on Pheanstalk

  • Monday, March 12, 2018
  • by Radhi
  • Repository
  • 1 Watchers
  • 1 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

QueueBundle

Queue (producers + consumers) system for Symfony framework based on Pheanstalk, (*1)

Requirements

Installation

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest version of this bundle:, (*2)

``` bash $ composer require mate/queue-bundle dev-master, (*3)


### Step 2: Enable the Bundle Then, enable the bundle by adding it to the list of registered bundles in the `app/AppKernel.php` file of your project: ``` php <?php // app/AppKernel.php class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Mate\QueueBundle\MateQueueBundle(), ); } }

Step 3: Add the overriding parameter

Add the your pheanstalk server host parameter to the app/config/parameters.yml file:, (*4)

# app/config/parameters.yml
mate_worker_host: 127.0.0.1

Getting started

This bundle make it simple to create your own jobs/tasks that takes long time to executed. Let's imagine that we have an application that send confirmation mails to users., (*5)

Create Job (ConfirmationMailJob.php)

Let's create a class called ConfirmationMailJob.php in AppBundle\Job namespace, (*6)


namespace AppBundle\Job; use Mate\QueueBundle\Worker\Job; use Symfony\Component\Security\Core\User\User; class ConfirmationMailJob extends Job { /** @var User */ protected $user; /** @var \Swift_Mailer */ protected $mailerService; /** * ConfirmationMailJob constructor. * * Dependencies: * * @param User $user * @param \Swift_Mailer $mailerService */ public function __construct( User $user, \Swift_Mailer $mailerService ) { $this->user = $user; $this->mailerService = $mailerService; } /** * Method used to execute this job * by our Queue worker later */ public function handle() { // } }

Our ConfirmationMailJob class should extend the Job (abstract) class of this bundle, and it require the handle() method., (*7)

Now lets write some basic code to send mail to the authenticated user in our handle() method., (*8)

/**
 * Method used to execute this job
 * by our Queue worker later
 */
public function handle()
{
    $message = (new \Swift_Message('Hello Email'))
        ->setFrom('send@example.com')
        ->setTo($this->user->getEmail())
        ->setBody('Confirmation mail here', 'text/plain')
    ;

    $this->mailerService->send($message);
}

That's it :), the last thing we should do here is to call the Producer service in our controller and pass this job to its the produce() method, (*9)

/**
 * @param Producer $producer
 * @param \Swift_Mailer $mailerService
 *
 * @return Response
 *
 * @Route("/", name="homepage")
 * @Security("has_role('ROLE_USER')")
 *
 * @throws \Exception
 */
public function indexAction( Producer $producer, \Swift_Mailer $mailerService ): Response
{
    /** @var User $user */
    $user = $this->getUser();

    // Produce the job to our Pheanstalk server
    // And injecting the desired services
    $producer->produce(new ConfirmationMailJob($user, $mailerService), 3);

    return $this->render('default/index.html.twig');
}

That's it :), you're done. Please note that the produce(Job $job, $delay = 0, $timeToRun = 60) method accept these parameters:, (*10)

  1. Job class
  2. Delay (seconds): by default 0 - no delay
  3. Time to run (seconds): by default 60

Run the queue worker

The last thing we should do is to listen to the executed jobs on our application, to do this you have to run the command, (*11)

$ php bin/console mate:queue:work

Now try to run your application server, and navigate to http://localhost:8000/ , take a look to your console:, (*12)

[2017-10-08 23:31:30] Processing: AppBundle\Job\ConfirmationMailJob
[2017-10-08 23:31:30] Processed : AppBundle\Job\ConfirmationMailJob

Make sure to find some tools like Supervisor to automatically restart your mate:queue:work process if it fails., (*13)

Advanced topics

Queue Events

This package provide some events to help you manage your jobs (Mate\QueueBundle\Event\JobEvent):, (*14)

  • MATE_QUEUE_JOB_INITIALIZED (onInitialized)
  • MATE_QUEUE_JOB_EXECUTED (onExecuted)
  • MATE_QUEUE_JOB_FAILED (onFailed)
  • MATE_QUEUE_JOB_DELETED (onDeleted)

// TODO, (*15)

The Versions

12/03 2018

dev-master

9999999-dev https://github.com/MateHelpers/QueueBundle

Queue (producers + consumers) system for Symfony framework based on Pheanstalk

  Sources   Download

MIT

The Requires

 

by Mohamed Radhi Guennichi

consumers producers jobs symfony3 queue tasks