06

апр

Queue System in PHP Need to develop a queue where jobs will proceed in sequence once finished. Do not bid if you do not have an idea of how to proceed with it. Think about it before bidding.

Laravel receives a request, does some work, and then returns a response to the user, this is the normal synchronous workflow of a web server handling a request, but sometimes you need to do some work behind the scenes that doesn't interrupt or slowdown this flow, say for example sending an invoice email to the user after making an order, you don't want the user to wait until the mail server receives the request, builds the email message, and then dispatches it to the user, instead you'd want to show a 'Thank You!' screen to the user so that he can continue with his life while the email is being prepared and sent in the background.

Php Queue System

Laravel is shipped with a built-in queue system that helps you run tasks in the background & configure how the system should react in different situation using a very simple API.

You can manage your queue configurations in config/queue.php, by default it has a few connections that use different queue drivers, as you can see you can have several queue connections in your project and use multiple queue drivers too.

We'll be looking into the different configurations down the road, but let's first take a look at the API:

The Queue facade here proxies to the queue container alias, if we take a look at QueueQueueServiceProvider we can see how this alias is registered:

The next video is starting stop. 50+ videos Play all Mix - Salam si Muharrem Ahmeti live2016 - SPECTACOL NEBUN 2016 live,CEL MAI NEBUN DUET YouTube Florin Salam & Muharrem Ahmeti - PREMIERA Duelul Regilor New Live 2016 NAS. Muharrem ahmeti florin salam.

So the Queue facade proxies to the QueueQueueManager class that's registered as a singleton in the container, we also register the connectors to different queue drivers that Laravel supports using registerConnectors():

This method simply calls the register{DriverName}Connector method, for example it registers a Redis connector:

The addConnector() method stores the values to a QueueManager::$connectors class property.

A connector is just a class that contains a connect() method which creates an instance of the desired driver on demand, here's how the method looks like inside QueueConnectorsRedisConnector:

So now The QueueManager is registered into the container and it knows how to connect to the different built-in queue drivers, if we look at that class we'll find a __call() magic method at the end:

All calls to methods that don't exist in the QueueManager class will be sent to the loaded driver, for example when we called the Queue::push() method, what happened is that the manager selected the desired queue driver, connected to it, and called the push method on that driver.

How does the manager know which driver to pick?

Let's take a look at the connection() method:

When no connection name specified, Laravel will use the default queue connection as per the config files, the getDefaultDriver() returns the value of config/queue.php['default']:

Once a driver name is defined the manager will check if that driver was loaded before, only if it wasn't it starts to connect to that driver and load it using the resolve() method:

First it loads the configuration of the selected connection from your config/queue.php file, then it locates the connector to the selected driver, calls the connect() method on it, and finally sets the connection name for further use.

Now we know where to find the push method

Yes, when you do Queue::push() you're actually calling the push method on the queue driver you're using, each driver handles the different operations in its own way but Laravel provides you a unified interface that you can use to give the queue manager instructions despite of what driver you use.

What if I want to use a driver that's not built-in?

Easy, you can call Queue::addConnector() with the name of your custom driver as well as a closure that explains how a connection to that driver could be acquired, also make sure that your connector implements the QueueConnectorsConnectorInterface interface.

Once you register the connector, you can use any connection that uses this driver:

Popular Posts