Laravel Horizon from Scratch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys Andre here and today I wanted to show you how to use laravel cues with Redis alongside their Bell horizon I was implementing cues into a current project I'm working on and I wanted to give horizon a try it's fairly new to the laravel ecosystem and there's a great video of Taylor launching a lyric on definitely give that a watch I will link it in the description below it starts around the 30 minute mark now if you're ready I'll show you how to get horizon up and running from scratch now before we get started make sure you have Redis installed as of this time horizon only supports Redis for cues if you don't know what Redis is just think of it like a database that stores key value pairs and in terms of building they're about what apps you can use Redis for things like caching sessions and in our case queues so to install Redis on a Mac just do this use homebrew and do brew install Redis I'm not gonna do that because I already have it installed and to make sure it's running just do this command right is CLI ping and if you get pong you know it's running if you're using laravel forge it's already installed for you automatically and more on level four layer on so here I have a fresh layer bail installation the only thing I've done is added the auth scaffolding to allow for user authentication I also have two users in the database which I'll be making use of later on I'll show you so let's get into our code here first thing we need to do is install a package called Prentiss which is just a package you need to use Redis with in there of else composer require Redis Prentiss and the next thing we can do is change our cue driver to Redis now if you're not familiar with cues cues allow you to defer a time-consuming process to a later time this will allow for your application to return to the user as soon as possible making for a better user experience now let's use the classic example of sending an email it's probably a good idea to queue all the emails that you send out in your app because it takes some time to complete that task so let's go ahead and make a mailable here HP artist and make mail it's called it user registered or something like that all right user registered and in here we'll just return the views so I'm just gonna call it email and just to emphasize how long it takes I'm gonna sleep it it's gonna sleep the app for two seconds now let's go ahead and make that view use let's just make a new file called email and then we'll just make some placeholder here email this is the email that gets sent out when a user registers okay that's good enough and in our routes file let's send a mail so we would do mail to just put an example in here and we'll send it it's called no user registered and it's import this and it's import mail as well and I'm gonna be using mail trap here if you're not familiar with mail trap it just allows you to send test emails to yourself I'm gonna put the credentials in here and will send an email out okay so now if we refresh the page this should send out an email but you'll see how long it takes here because I slept it for two seconds and also the process of something an email takes a few seconds to complete so you'll see here and it's done if we take check mail trap and if I just log in here the email should be in there there we go now let's throw this task on to a queue and that a process in the background while we instantly return to the user the most common way to do this is to make a job let's go back into our code here but if we're just using mail we can actually just use the queue method to throw it onto the queue and if you see now and if you're fresh this page just should this should instantly return to the user there you go it's much faster than it was before and if we check our mail it's not gonna be here yet because we through the task onto the queue but we have yet to process the queue so traditionally the way we did that was we ran PHP artisan queue work and you'll see it process here there you go it's processed and you see the mail up here here in the background now before horizon to get insight on the jobs on the queue failed jobs or anything queue related there's a bunch of command line tools you can interact with you can also go into Redis and mess around there to see what's on the cube but horizon makes it much more user-friendly with a very nice GUI easy-to-use tools and you can definitely see and monitor what's going on with your queues so let's go ahead and install laravel horizon so here is the documentation page you can just compose your require there will arise in let's stop this and let's publish the assets subscribe this in there well 5/5 you don't have to specify a provider because you can just do this and then it's going to ask you which provider you want to publish in our case it is one yes horizon service provider and that's been published into our app and if we go into our app again what was it called laravel horizon yep sorry I don't want to do that we wanted to go into slash horizon and we can see our beautiful dashboard here here is where we have an overview of all the jobs and all the cues that are currently running you can see that this in active status means that our queue worker is not running so to do that we would - instead of PHP artisan queue work like we did before we do PHP artisan horizon there you go that's active and you can see our default queue and you can see the server that it's running on so the first thing I want to show you is authentication by default this horizon endpoint is only available in your local environment but if you want to override that then just go to the documentation there's a little code snippet here and if we go into our app service provider and paste it in the boot method here we can specify who gets authenticated now in your real app you would probably authenticate like an admin user but in our case since we have some users here that's only allow the user with ID 1 to have access if that works actually we have to import horizon there you go and let's go back here ok so let's try it with the user ID 2 this should not work if you gotta rise in there you go you can catch that exception and do what you want with it but if we login using ID user ID 1 which is user at user com that should work or rise in and there's our dashboard and the next thing I want to show you is recent jobs now this application is a single page app and it updates in not real time but every few seconds so if we go back to our main page where we have males and yellow and refresh a few times you'll see that the jobs update and process after a few seconds you see all those processed and we're completed and if you see the mail was sent now the next thing I want to show you is failed jobs at one point one of your jobs is gonna fail and they will appear in this failed tab over here now to simulate a failure in our case let's go ahead and for the credentials of male job let's just change it to something that's not valid stopper or rise in there start it up again and this should fail refresh this again you'll see the job come on to the queue and you'll see it's taking a bit longer because it's retrying a few times and it will fail eventually if I'm not mistaken and there you go you see that our job failed here you can see in the recent jobs you can also see it in the failed tab and you can go into it and see the error exception and the stacktrace you can see here failed to authenticate SMT server because we changed the user name to an invalid one now a failed jobs you have this very handy retry button up here that you can retry that failed job and you'll see it associate with all the retries down here again this is gonna fail again because we haven't fake the credentials and there you see you fail but if we go back to our code fix our credentials save restart or worker all back here retry again and there you go you see that this initial failed job is associated with all the retries and you can see it completed here now the next thing i want to show you is tags you can see that so far our tags are empty and I'll show you why go back into our code and let's make a new job let's top this works like PHP artists in make job some job and let's just sleep in here for two seconds and let's go back into our routes file and let's make another route here just grab this doing some initial setup so I can show you how types works okay am i doing here let's click jobs and it's passed and a jobs parameter and we'll grab it here just so we can specify how many jobs do you want to run and we'll just make a for loop in here typical for loop that will iterate over the number of jobs make sure to import some job and save it and if we go back in here and do jobs so I say 10 I did not work I am missing a closing bracket okay so that should dispatch ten jobs you can see him there and there's still no tag on this that's because there's no model associated with this job now if we see except a user in here or any model you have and it's just protected user and it's take it in here this yes visitor now if we take in a user and just touch it with a user let's just return something here and user user gets user find one make sure we import the user and take in that user now if we go back here just touch another ten jobs I did not import it here so I put it there okay again okay ten jobs now you can see that there's a tag associated with that with all the jobs this allows you to just you know if you have failing jobs you can see which ID and it's easier just to debug things if you have a tag you can also specify exactly what you want in the tag if you just tags and return something like this user : this user ID if you want to specify your own tags if you just do that and it's one another ten jobs and you can see the tag is what you specified in that method you probably notice that these jobs weren't being processed that's just because I wasn't running horizon here so make sure that's running and you'll see these jobs process they should be posting you go okay so the next thing I want to show you is monitoring monitoring is just stick that one up if there's something weird going on with your jobs you can monitor one of them so say for example a user was having an issue and you knew their ID I'd only have one in here for now so let's just monitor that tag user one monitor it throw some jobs on the queue and you'll see that they appear here the next thing I want to show you is queue related configuration which is now all done through code if you go to config horizon you can see that all the queue related config is located here we have one for production and we have in for local the first thing I want to show you is you can specify the number of queues you want here by default there's only one called default but if you wanted another cube called another I can put it here let's just stop our worker start it up again and if you go back to our dashboard we'll see that there's two now you can also specify the number of processes you want here depending on how many jobs you're processing at a certain given amount of time I don't know why it was at three I think default is 10 change it 20 shut it down start it up again and you can see that there's 20 processes split between the two the next thing I want to show you is cue balancing strategies now let's change the balancing property to auto save that stop this start again okay so Taylor gave an example of this in his demo but what this does is let's just throw a lot of jobs onto the cue say like 200 ok now you'll see that they all get thrown onto this cue because that's the default behavior but you can see the processes here since it sees that there's a lot of jobs on this cue it donates its processes there until it's done so the jobs can process faster and once it's done you'll see that the processes will go back to the other cue there you go you'll see it start to balance last thing I want to do is to put this up on forge and get horizon working on there now there's actually an article that Taylor released on medium on how to do this there it is and it's pretty straightforward it's just two steps adding a new daemon such that the PHP artisan horizon command is always running and then updating your deploy scripts such that the terminate command runs and then restarts so let's go ahead and do that I'm not going to show the entire process of getting your application up on forge but once it's up and running well I'll check back in with you there all right I set it up on forge on my server at Horizon des Andre demo CA the only thing I had changed in the code was this app service provider here I just made it return true so I'd have to bother logging in to access the horizon dashboard so let's go ahead and follow the instructions here so I need to add a daemon and make it run PHP artisan horizon in the folder of my sight alright so let's go ahead and do that server details there's a daemon here and ignore this one this is the one I initially did not playing around with it so the command is PHP artisan horizon user is Forge and the directory is home stash forge slash horizon Andre Timo's that's yeah and it starts at daemon so what this demon does it is and make sure that this command is always running even if it fails it it's gonna restart it again and the next step is you just have to add PHP artisan rise and terminate to the end of your deployment script and as you can read here it says this middle make sure that the horizon workers are graciously terminated and fresh horizon workers will be started okay that's actually it it's where is it those lights are eyes in and it's at it right here save script okay let's deploy it okay that was successful now let's go to site horizon.com great demos dossier okay that worked let's try horizon and there you go see if a job is running here yeah it's not working because I forgot to change my environment file to set Q the Q driver to Redis so I'm gonna do that behind the scenes because I don't want to show you my passwords alright I set the driver appropriately and that looks like a work because the return right away there you go recent jobs there you go and let's try our jobs and point jobs now just try a hundred that return right away done and you can see all the jobs here awesome so I hope this gave you a good overview of how to use laravel horizon and queues with Redis and I hope you can start using queues in your application hope you learn something new please like please comment please subscribe and I'll see you guys in the next one okay thanks bye
Info
Channel: Andre Madarang
Views: 25,956
Rating: undefined out of 5
Keywords: laravel, horizon, queue, redis, lesson, php, laravel 5.5, laravel horizon, drehimself, andre madarang, andre, madarang, horizon laravel, laravel horizon demo, laravel horizon example, horizon in laravel, laravel 5.5 horizon, laravel horizon tutorial, install laravel horizon, how to use laravel horizon, laravel forge queues, laravel/horizon, laravel queue, queue laravel, laravel queues, queues laravel
Id: Z5w0z6Ar3tQ
Channel Id: undefined
Length: 24min 46sec (1486 seconds)
Published: Thu Sep 14 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.