[0:00] welcome back to the what is Laravel series I'm  your host Aaron Francis let's Dive Right [Music] [0:09] In Laravel Horizon got to be one of my favorite  packages in the Laravel ecosystem it is a first   [0:20] party package so it's written and maintained by  the laravel team it's been around since Laracon   [0:26] 2017 I think so it's been around for a long time  time now and what it does is it provides you a   [0:33] beautiful dashboard for monitoring your Q workers  it also gives you a code driven way to configure   [0:43] the number of workers the balancing strategy the  the processes and so your entire Q system can be   [0:51] wrapped up and put into this Horizon configuration  file now importantly this only works with redis   [0:58] backed cues so if you're using a database backed  Q or an sqs backed Q it's not going to work with   [1:05] that and so that's one maybe drawback but I pretty  much use redis back cues for everything if you're   [1:12] coming from a different ecosystem this is very  similar to sidekick over in the Ruby world or   [1:19] I think it's called obon over in The Elixir world  and so if you're coming from a different spot that   [1:26] probably sounds more familiar to you than Horizon  but they're all basically the same thing except   [1:31] Horizon is free and open source so I'm going to  show you how it works here and then we'll talk a   [1:35] little bit more at the end I've got a very basic  Laravel application here if we open it in the   [1:40] browser we see it's just this invite a user and  this doesn't really do anything except queue up a   [1:47] job so this is the application that we're working  with I'm going to go ahead and do composer require   [1:52] Laravel Horizon that's all it takes it's going to  pull the package into my application and then you   [1:58] can see that we are left with we're left with a  lot of horizon commands we're not going to cover   [2:04] all of them but the first thing I am going to do  is say PHP Artisan Horizon install and what that's   [2:11] going to do it's going to publish the assets the  service provider and importantly the configuration   [2:16] we'll go over the configuration here in a second  but I want to show you that in the matter in a   [2:22] matter of 30 seconds here we now have a full on  if we hit in the browser we now have a full on Q   [2:29] dashboard monitoring metrics pending jobs  completed jobs silence jobs failed jobs all   [2:36] of this lives inside of our application it's  being served by this Laravel Horizon package   [2:43] so all of this all of this chrome all of these  assets all of this logic is inside that Laravel   [2:49] Horizon package now it's inside of our app now  this dashboard is totally open to the public   [2:55] where everybody can see it in local development  only by default Inu production nobody can see this   [3:01] and you have to modify the authorization gate to  allow people to look at this in production which   [3:08] is kind of important that's kind of what you  want so let me show you how that works and then   [3:12] we'll dive in a little bit deeper to modify the  authorization we're going to look at the Horizon   [3:16] service provider which has been published into our  application so you can see that it lives inside   [3:21] of our app providers directory that was put there  by the Horizon install command first thing you'll   [3:27] see is this looks interesting but we're going to  skip that for now so we'll come back to it pay   [3:32] attention we're going to jump down here to this  view Horizon gate and by default it's just saying   [3:39] great put some people's emails here and we'll  check to see if they're allowed to use it so we   [3:43] could put Aaron example.com please send me some  emails um and this would allow me to use this in   [3:50] production if if you aren't um if you don't have a  user login system and you want to limit by IP only   [3:58] I think you can say equals null and then you don't  have to check the user and you can do anything   [4:04] that you want down here it's just a function you  can do anything you want to decide if this person   [4:08] is able to view the Horizon dashboard scrolling  back up to this very interesting thing Horizon   [4:15] also comes with notifications so if a queue is  overwhelmed or backed up or jobs are taking a   [4:21] really long time to complete you can just route  notifications to many different channels just like   [4:28] that all you have to do is put the information in  here with SMS you would have to set up you would   [4:34] have to set up your SMS provider but this uses  laravel's notification system under the hood so   [4:39] it's pretty it's pretty straightforward let's look  at the configuration file before we look at this   [4:45] configuration file I have to warn you it's pretty  robust which is a good thing we want to be able to   [4:49] configure it exactly how we want I am not however  going to show you every single option there's too   [4:54] much right here but I can show you uh a few of  the interesting ones you can serve The Horizon   [5:00] dashboard from a subdomain so if you don't want  it on your main domain you want it on like app dot   [5:05] or o or admin dot you could do that you can also  change the path where it is served so it doesn't   [5:11] have to be SL Horizon it can be SL whatever else  pretty cool not what I'm here to show you I am   [5:16] here to show you this environments key so in this  environments key you've got production and you've   [5:22] got local and this is where you get to Define per  environment you get to Define how many workers   [5:29] what the balancing strategy is what cues are  being worked so inside of these Keys you've   [5:35] got something called supervisor one doesn't need  to be called that it could be called Fu one it   [5:40] could be called anything you want we'll leave  it at supervisor in this context supervisor   [5:45] just means um a collection of processes a group a  bundle that's it nothing more fancy than that the   [5:52] reason you can have multiple is let's say you want  to have um this supervisor this group of processes   [6:00] this configuration you just want it to process the  default q and maybe the high Q so maybe you have   [6:08] a high and a low q and you need more throughput  on the high Q than you do on the low Q this is a   [6:13] great place to do that in fact let's go ahead and  turn that up to 15 we'll turn this down to three   [6:19] because we don't super care about it and then  we'll say for this supervisor all I want you to   [6:24] process is in fact just process the low Q that's  all that I want you to care about three processes   [6:32] is fine I don't really care about that so much  now you can say you can say I want to balance the   [6:39] default is auto which is good I like that I think  the other options are simple and then I think   [6:46] false is an option but since Auto is the default  I usually leave that off and what this is going to   [6:52] do is this is going to allocate processes based on  how deep the cues are so if your high Q is totally   [6:59] slammed and your default queue is empty it's going  to move a bunch of processes over to process the   [7:05] high Q you can say hey don't don't leave me  totally high and dry um let's just say that   [7:13] the Min the minimum number of processes working  on a single Q let's make that two so you can move   [7:19] 13 over to the high q but leave two on to process  the default Queue Now you can also have something   [7:27] I think it's Auto scaling strategy let's I think  that's right let's check the notes autoscaling   [7:35] strategy that is correct you've got time and  you've got size and so if you have some jobs that   [7:43] take a really long time you can say actually let's  let's balance based on how long we think it will   [7:49] take to empty out the queue instead of balancing  based on Raw number of jobs so you can change   [7:56] that if you want and then down here it's going  to say how many processes do you want to shift   [8:02] at a time in our case we're saying one process  every 3 seconds and so there's so much here that   [8:09] you can configure to match your exact setup and  you can find the balance between throughput and   [8:17] absolutely hammering your server um this is where  I think Horizon shines the best is putting all of   [8:24] this in Version Control and then putting all of  this behind a single command so you can have 50   [8:31] processes but for you to start it all you have  to do is run PHP Artisan Horizon in fact let's   [8:37] go ahead and do that right now so if we run PHP  Artisan Horizon that's it that's all you have to   [8:44] do now it is running under the hood it's running  up to what did we say 15 plus 5 I think so it's   [8:50] running up to 20 processes watching those cues  for us and this this is the only command that we   [8:58] have to keep alive this is it I'm going to show  you these commands in a second but first let me   [9:02] show you configuration from a real app that I've  worked on so you can see some of the nuances there   [9:07] this is the configuration from an app that I  have worked on you see we have a supervisor   [9:11] short supervisor long and the connection one is on  redis and one is on redis long that makes enough   [9:16] sense timeout is 90 timeout is 1,200 and you see  this one processes the high and the default cues   [9:24] and this one processes the long and the scrapers  cues so these jobs take a whole lot longer and so   [9:30] we give it a much longer time out and a and just  a few fewer processes than the ones that need to   [9:36] have a lot higher throughput starting Horizon is  easy like we just looked at PHP Artisan Horizon   [9:42] this is the command that you would um that you  would want to keep alive if you're running on   [9:48] Forge it's a simple toggle you just say keep  Horizon alive if you're running somewhere else   [9:53] you'd need to set up actual supervisor to make  sure that this stays alive because when this   [10:00] command goes down all of your key workers go down  and in fact you want to roll this you want to   [10:06] terminate this gracefully when you are deploying  a new version of your application you can do that   [10:12] by saying PHP Artisan Horizon terminate so you  send the terminate signal and over here it stops   [10:21] and this is really important when you deploy new  code because this is a long running process it   [10:26] is going to put your application in memory and  so the the last thing you probably want to put   [10:30] in your deploy script is Horizon colon terminate  and then let supervisor bring that back to life   [10:36] and it's going to load your new application into  memory so we'll go ahead and start this again and   [10:42] over here I'm just going to run PHP artisan and I  think it's invite um let's grip for I think it's   [10:52] invite yeah PHP Artisan invite users and this  is going to put a th jobs on the Queue and we   [11:01] should start to see that working so this this  job sleeps for 2 seconds to simulate you know   [11:08] calling out to an API or something but you can  see that we're we're churning through these jobs   [11:13] and if we look in the browser if we look in the  browser you'll see we're running three processes   [11:19] on the default and we've got some failed jobs so  you can click into the failed jobs and see okay   [11:26] well this failed to send in fact let me just let  me just retry that guy and if you click retry from   [11:32] the dashboard it's going to put it back in the  queue and you're going to get um you're going to   [11:36] get this the retry down here it shows that it's  pending it's waiting so let's uh let's go ahead   [11:42] and kill let's kill Horizon again and hop back to  the browser and if we look in the browser you see   [11:49] that it is inactive and we're going to come over  to we're going to come down here to local and   [11:57] let's change the max process is to Let's Go Crazy  let's make it uh let's make it 25 and if we start   [12:04] this again I bet those are going to turn through  a lot faster we see we're now running 25 processes   [12:13] and it gives us a little bit it gives us a little  bit of information about the cues themselves you   [12:18] can see we've got 800 700 um jobs in this default  queue and so this gives you a nice overview of not   [12:25] only your cues but also the supervisors that are  running and if we check back here in local we just   [12:32] have the one supervisor so that checks out that  it's correct and you can see this is just totally   [12:37] churning through these jobs now you can check the  completed jobs to look in there see a little bit   [12:43] of information if you have a bunch of jobs that  you don't want mucking up your completed jobs view   [12:48] right here you can add those to silence jobs and  those won't show up sometimes that's thirdparty   [12:52] packages running jobs or um notifications or  something like that that you don't really care to   [12:58] see you can put you can put those in the silenced  jobs let me show you a few of the Horizon commands   [13:04] before we go PHP Artisan Horizon let's start  with status you can see Horizon is running which   [13:09] is good then you can say list and this will show  all of the machines and the supervisors that are   [13:15] running you could also do PHP Artisan supervisors  and that's going to show you um a little bit more   [13:24] detail about each individual supervisor you can  also pause so if if you're like if your server's   [13:31] getting totally hammered by something you can say  who let's not let's not work with any cued jobs   [13:36] at the moment so you can log in or you can ssh  in pause the workers and everything pauses one   [13:43] command and you could potentially pause hundreds  of workers and then after you fix whatever is   [13:50] broken it's not resume it's continue after you  fix it you can just say all right we're good to   [13:54] go back to work people and finally remember the  one that you want to run when you're deploying is   [14:03] PHP aren Horizon terminate there you go and  Horizon will stop working speaking of it is   [14:11] time to terminate this video pretty proud of that  one um why would you why would you maybe not use   [14:17] Horizon I think potentially the best reason to not  use Horizon is you're using sqs as your as your   [14:24] Q driver and therefore you can't use Horizon if  you're already using redis as your queue driver I   [14:31] don't see a good reason not to use it frankly free  open source first party battle tested extremely   [14:38] powerful makes your life extremely convenient  so give Horizon a try if you want to watch a   [14:43] video about Laravel octane in the same style  I'll put that here until the next time see you