Differences between Queue:listen and Queue:work--daemon in the Laravel framework

Source: Internet
Author: User
Tags comments redis sleep
Three types of cases:Queue:work only executes a queue request by default, and terminates when the execution of the request is complete; Queue:listen Listen for queue requests, as long as they run, you can always accept requests, unless manually terminated; Queue:work--daemon and listen, as long as the operation, can always accept the request, not the same place is in this mode of operation, when the new request arrives, do not reload the entire frame, but direct fire action.

Can be seen, Queue:work--daemon is the most advanced, generally recommended to use this to handle queue monitoring.

Note: Using Queue:work--daemon, when updating the code, you need to stop and then reboot to apply the modified code. As for the necessity of existence first,--daemon was added after the Laravel 4.2. Secondly, judging from the above analysis, Queue:listen and Queue:work--daemon still have a difference.

A variety of queue services can be selected in Laravel, and Redis and Beanstalk are common, and because Redis is a simple db, the data is completely transparent so it is better understood to use Redis as the object of instruction.

The Laravel Squadron column starts with Queue:listen and queue:work, where queue:work means running the next job separately. For the difference please see: the difference between Queue:listen and Queue:work--daemon. Queue:listen internal is also a call queue:work implementation, now we look at the work command.

Did you see the fire method we were familiar with in the Illuminatequeueconsoleworkcommand? Yes, that's it. Continue chasing, call Runwork mode to find job and execute. Here is a daemon option that indicates whether to run as a daemon. Daemon is considered false directly (because daemon is actually a while (true)): joy:.

See Runwork is actually called the Illuminatequeueworker::p op method

Public function Pop ($connectionName, $queue = null, $delay = 0, $sleep = 3, $maxTries = 0)
{
 $connection = $this-& Gt;manager->connection ($connectionName); Here is what you say the connection name, is the actual choice of a reasonable queue service, as long as you do not use a specific queuing service method, then the individual queues can be switched at any time to use.

 $job = $this->getnextjob ($connection, $queue); Find a Job Object

 //If we ' re able to pull a job off of the stack, we'll process it and
 //then immediately return BAC K out. If there is no job on the queue
 //We'll "sleep" the worker for the specified number of seconds.
 if (! Is_null ($job))
{
//If job exists then perform task
 return $this->process (
 $this->manager->getname ($ ConnectionName), $job, $maxTries, $delay
);
}
$this->sleep ($sleep);
 return [' job ' = null, ' failed ' = false];
}

To the end of the job execution (delete blank lines and comments, because I have to come up with crappy Chinese comments.) ( Escape):

Public function process ($connection, Job $job, $maxTries = 0, $delay = 0)
{
 if ($maxTries > 0 && $job-& Gt;attempts () > $maxTries) {//See, when running, check the job execution number if it exceeds the maximum number of times, then go directly to the logfailedjob process, which is to store the failed job in the specified database and observe ' The ' attempts ' method in Illuminatequeuejobsredisjob ' and ' release ' method. A view of the number of attempts, an increase in the number of attempts (of course, release and other operations). Then find someone who called the release method (see the Trycatch below, The release is called after an exception has occurred and the job has not been deleted). If no exception is thrown. Then attempts is not going to change.
 return $this->logfailedjob ($connection, $job); 
}
try{
 $job->fire ();//The Fire method of the job has actually used the jobclass we specified with Container::make and executed the fire method.
 if ($job->autodelete ()) $job->delete ();
 return [Job ' = $job, ' failed ' = false];
 } catch (Exception $e) {
 if (! $job->isdeleted ()) $job->release ($delay);

 throw $e;
}
}

Well, that's pretty much what happened.

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.