Installing Horizon Package0:00
Laravel Horizon is a Redis Queue dashboard built by the creator of Laravel, Taylor Atwell. Let's see if we can get it up and running on our machine. So here's the GitHub page, let's come down to the documentation, installation, and you can see we need to require the package, and then install the dependencies, and then finally set up the failed table, but we already did that in the previous episode. All right, let's run that, all right, and then run horizon:install, and this should give us, yeah, it'll give us a HorizonServiceProvider, various assets in the public directory, and then a horizon.php configuration file. So again, you'll find your Horizon config here, you can set up a domain, a prefix, you can set up the environments, you can specify how long Horizon should maintain older records.
Reviewing Configuration Files0:43
So again, you'll find your Horizon config here, you can set up a domain, a prefix, you can set up the environments, you can specify how long Horizon should maintain older records. Mostly though, you can refer to this and update it when you need it, otherwise, don't feel bogged down. Okay, next, in your providers directory, you'll have a new HorizonServiceProvider. This will give you a place to specify where SMS notifications, or Slack, or email, that can be done automatically. You can specify a night theme, and we'll look at that in just a minute, and then also you can set up your authorization gate, and we'll talk about that in just a minute. Finally, like I said, in your public vendor directory, you'll have some new Horizon assets.
Launching the Dashboard1:19
can set up your authorization gate, and we'll talk about that in just a minute. Finally, like I said, in your public/vendor directory, you'll have some new Horizon assets. So with everything now installed, you can go to your domain, /horizon. And there we go, our dashboard is up and running. But you'll notice the status is currently inactive, and that's because to boot it up, we run php artisan horizon. Think of this, among other things, as a variant for Queue, but it will also set up a number of Horizon-specific operations. Anyways, as we run that, the status now turns to active, and we can see we have our default queue, three processes, and at the moment, there have been zero new jobs in the last
Forcing a Failed Job1:54
Anyways, as we run that, the status now turns to active, and we can see we have our default queue, three processes, and at the moment, there have been zero new jobs in the last hour. Okay, let's go ahead and refresh this page, and if I switch back, we should now see, if we give it a second, there we go. One new job has been executed, and we can take a look at that job right here, reconcileAccount. Now, at the moment, we have zero failed jobs, but why don't we tweak that? So I'm going to come back to our reconcileAccount, and like we did in the last episode, I'm going to throw a new exception to trigger a failed job.
So I'm going to come back to our reconcileAccount, and like we did in the last episode, I'm going to throw a new exception to trigger a failed job. Okay, so if I come back, and we give this another run, we'll go to the dashboard, we can see two jobs, and we should have a, oh, I'm sorry, I forgot to reboot php artisan horizon. We've made a code change, so we, of course, have to restart that. And don't forget, as part of your deployment process, things like that would happen automatically, so it's not really something you have to be aware of. It's in your deployment script. Okay, but anyways, I'll give this a refresh, and that should trigger a new failed job, as we see right there.
Monitoring Jobs with Tags3:55
What else? We can go to Metrics and review all the jobs, we can monitor tags, which I'll show you in just a second, and of course, don't forget your main dashboard here. Okay, so let's talk about monitoring. So you'll notice, if we're dealing with Eloquent Models, they will automatically be tagged, as you can see here. So it's basically the class path, and then the ID of the associated User, which means if for whatever reason you want to monitor only jobs associated with that User, you could do it here. So I can monitor this tag.
do it here. So I can monitor this tag. And now, for any new jobs that enter the queue, it will track that. Give that just a second. There we go, I had to give it a refresh, but I can now see here are the jobs associated with that tag. Now again, this is dynamically created, but if you want, on your Job class, you could specify the tags. And this can be an array, so if it's related to a User, you can still apply it like so. Because remember, if you add a tags method here, it will override anything that Horizon
And this can be an array, so if it's related to a User, you can still apply it like so. Because remember, if you add a tags method here, it will override anything that Horizon does by default. But yeah, add whatever tags you need there. And now, if we give it a refresh, let's go back to our Recent Jobs, and now you'll see this most recent job has that tag applied. So now I could say, okay, we're only looking at jobs related to our accounts at the moment, because maybe there's some kind of issue or failed job that we want to keep an eye on. So we'll run that, and this will update in real time. Now I'll let you dig through the documentation for anything else you might want to know.
Securing Dashboard Access5:24
So we'll run that, and this will update in real time. Now I'll let you dig through the documentation for anything else you might want to know. But real quick, something to be aware of is for local development, you can access this. But of course, for production, you don't want just anyone seeing this dashboard. So if I switch back to the HorizonServiceProvider, this is where your gate comes into play. So you can see by default, it's recommending just look at the email so often. That's all you need. So in my case, I might say, if the User's email is mine, then I have permission to view this dashboard.
So in my case, I might say, if the user's email is mine, then I have permission to view this dashboard. Otherwise, you don't. And then you'll see... Let me show you. Let's go to the ParentServiceProvider. When it boots, it'll apply the authorization. And yeah, here you go. So here's the HorizonAuth. So if the app environment is local, then return true, which means anyone on a local environment
So here's the Horizon Auth. So if the app environment is local, then return true, which means anyone on a local environment can see their Horizon dashboard. Otherwise, for staging or production, we will run this gate authorization while passing through the user. And that authorization will run this. If the authenticated user's email is in this array, then you have permission. Okay, so to simulate that, let's just return this. And then you'd see if you were in production, I'm not currently signed in, so I do receive a 403 forbidden.
And then you'd see if you were in production, I'm not currently signed in, so I do receive a 403 Forbidden. Okay, that's Laravel Horizon for your Redis queues.
