Laravel Queue Service

Source: Internet
Author: User
The registration of various Laravel services is mostly bound through various serviceproviders, and the queue service is no exception. open the namespaceIlluminateQueueQueueServiceProvider file and locate the register method,

QueueServiceProvider

The registration of various Laravel services is mostly bound through various serviceproviders, and the queue service is no exception. open the namespace IlluminateQueueQueueServiceProvider file and locate the register method,

Public function register () {// register the queue manager to register various resolvers for the queue connector once instantiated. These connectors are responsible for creating classes for receiving queue configurations and instantiating various queue processing. // Follow the configuration file to register a default connection method here using redis $ this-> registerManager (); // register various command queues in the queue for connection restart and so on. $ This-> registerWorker (); // register the queue listening command $ this-> registerListener (); // discard $ this-> registerSubscriber () after 5.1 (); // handle registration queue failure $ this-> registerFailedJobServices (); // Register the Illuminate queued closure job. for more information, see. $ This-> registerQueueClosure ();}

Task Creation and allocation

php artisan make:job SendReminderEmail

A queue task class is generated according to the document. this class inherits namespaceAppJobsJob and implements the SelfHandling and ShouldQueue interfaces, you may ask what specifications are not required for these two interfaces (skipped first). The focus is on the two trait operations, deletions, retries, and latencies for queue tasks.
We used the auxiliary function dispatch when assigning tasks. it is actually the dispatch method of the Dispatcher class in IlluminateBus.

Public function dispatch ($ command, Closure $ afterResolving = null) {if ($ this-> queueResolver & $ this-> commandShouldBeQueued ($ command )) {// The queue executes return $ this-> dispatchToQueue ($ command);} else {// immediately executes return $ this-> dispatchNow ($ command, $ afterResolving );}} protected function commandShouldBeQueued ($ command) {if ($ command instanceof ShouldQueue .. Return true;} return (new ReflectionClass ($ this-> getHandlerClass ($ command)-> implementsInterface ('illuminate \ Contracts \ Queue \ ShouldQueue ');}

Here, let's take a look at

Public function register () {$ this-> app-> singleton ('illuminate \ Bus \ Dispatcher ', function ($ app) {return new Dispatcher ($ app, function () use ($ app) {// 'queue. connection '=> 'illuminate \ Contracts \ Queue', and then check the registerManager method of QueueServiceProvider. Return $ app ['illuminate \ Contracts \ Queue ']; // Default Queue connection });});}

Let's take a look at dispatchToQueue.

Public function dispatchToQueue ($ command) {$ queue = call_user_func ($ this-> queueResolver); // The default value set here will instantiate RedisQueue // An exception will be thrown! If (! $ Queue instanceof Queue) {throw new RuntimeException ('queue resolver did not return a Queue implementation. ');} if (method_exists ($ command, 'queue') {// You can customize return $ command-> queue ($ queue, $ command );} else {// Here, the final result of entering the queue is similar to $ queue-> push (); see the push method under RedisQueue. Return $ this-> pushCommandToQueue ($ queue, $ command );}}

The entire process of the preceding task entering the queue is clear. What about the task output queue? In this document, we can see that we listen to the queue by executing the php artisan queue: work statement. here we can see the namespace IlluminateQueueConsoleWorkCommand: fire (), which is very deep in the night, let's take a look!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.