Laravel 5.8 – From The Ground Up
Lesson 30 of 52 · Laravel 5.8 Tutorial From Scratch – e30 – queue:work In The Background · Education
Laravel 5.8 Tutorial From Scratch – e30 – queue:work In The Background
in the previous episode we talked about running our cue worker in our terminal while we are developing however the question did arise fairly quickly from
one of you asking well how do I run that in the background I don't want to have to run that in the foreground so we're gonna do a quick lesson today just to show you how we can put that in the
background and still maintain our output and being able to log that to a file so let's start off right where we left in the previous lesson we were running PHP artisan cue work and of course that
stays right there as you can tell it is just waiting for a job to come in let's give it a job let's go to Chrome add a new customer give it an email add customer and sure enough we are
processing our job we know our job takes roughly about 10 seconds because we're using that sleep timer on it and there we go all right so our job processed we head back and we have our email all
right so everything is working exactly as expected I'm going to hit ctrl C to exit out of that process and to put that command in the background it's actually quite simple all you have to do is add
an ampersand at the end so exactly the same command except at the end use that space and then ampersand and when you run that you see that now we have our prompt back and we have this five digit
here this is called a process ID all right so this is process ID 2900 eight you don't really have to remember that I'll show you how to look that up later on now right now that is running it is
listening for jobs to come in and to prove that we can run jobs and we see here we have a process that is running and the process running is PHP artisan queue work so let's add another customer
any user will do test a test add customer all right and there we go sure enough that process is running now let's go ahead and run jobs again and we see that we have that running but we
don't get that process ID to get that we need to run and pass in the flag of L and that gives us our process ID so when you want to kill this process just like that you
type in kill and then you need to give it the process ID in our case 2900 8 and that ends the process notice it says done and at that point it is done with that process
if we run jobs again that's empty there's nothing there and of course if we were to give it a new job it doesn't work all right now at this stage we've lost the ability
to see what things have processed and what kind of errors we are getting so to add to that what we could do is called the same command again so that is PHP artisan cue work but
before the ampersand we could put a greater than sign which basically pipes whatever comes out of that command into another place and the place that we're going to put it at is storage logs
there's a directory specifically for logs that comes in with lerigot and let's just call it jobs log run that again alright so we have that running in the background let's
give it a new job a new customer one more time and that's running and now let's switch back to PHP storm and if we go into storage logs we have this new jobs dot log and if we open that file we
see that same exact output that we were getting in the terminal but now we're getting it in a file and of course we are running if we go back over here we do have our new email that popped in so
now we have a place a log for all of our files to go into and that's pretty cool now one thing about this is that if that process was to fail nothing really happens I mean the jobs stopped running
and everything gets backed up so you need something else right you need supervisor supervisor is another piece of software that is beyond the scope of this course but you could do some Google
searches and find some stuff on how to set up supervisor and what supervisor will do is if this process stops running for any reason it will restart it for you and get
again that way your jobs don't get backed up so that's more like what you would do in a production situation however for development this is perfectly fine because you can always
run these jobs with the flag of L and you know if your process is still running if that process fails you can simply boot it back up by yourself so let me go ahead and kill that process
again so that's twenty nine seven fifty and now we're no longer processing jobs so with that now you know how to be able to run that queue work in the background that way you could get your terminal
prompt back up now typically in development like we talked about you wouldn't really do this you'd kind of just leave a tab there open for you you don't really need to run this in the
background but you can there's really no reason to do one over the other they're both basically the same in your development environment so that wraps it up for this lesson if you didn't catch
the previous episode episode 29 catch that one that is all about how we set up our queues and how we refactor our code two queued a ten-second long process that way the user doesn't have to wait
so that interest you go ahead and check out episode 29
Now that we have our jobs running, let’s explore how we can run the queue:work command in the background. As it turns out, it’s actually quite simple to do. Follow along as we set it up from scratch.
For the best experience, follow along in our interactive school at https://www.coderstape.com
Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)
About This Course
Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!