Horizon Wait Time Alerts0:00
Okay, let's take a look at how we can get notified when something important is happening with our queues. So the first example I want to take a look at is being notified when the max wait time reaches a certain threshold. So say, for example, we have a spike in traffic, and our max wait time for our queue jobs is pretty high. So we definitely want to be notified about that. So let's go ahead and go into the HorizonServiceProvider. And you see these three comments here for notifications. You can do email, which we'll do. You can do SMS and you can do Slack as well. So let me save that. And the
notifications. You can do email, which we'll do. You can do SMS and you can do Slack as well. So let me save that. And the threshold is defined in the config. So if you scroll up here, you'll see an option for the wait time. So let's set it to something really low. And then let's run php artisan horizon. And then we'll throw a bunch of queue jobs on the queue, and that should increase the max wait time, and we should get an email. So let's go here. It's active. And let's go here, and let's throw
Handling Failed Job Events1:36
processes until the traffic dies down. So it's good that we're being notified about this issue. So the next thing I want to take a look at is failing jobs. So we want a notification about that as well. And this is not specific to Horizon, but just queues in general. And I actually experienced this a few weeks ago. So we definitely want to be notified on failing jobs. So if you look at the Laravel docs, there is an event you can hook into and react accordingly. So if you go into queues, you'll see this section for failed job events. And here is the event we can hook into. So let's grab this.
this section for failed job events. And here is the event we can hook into. So let's grab this. And we can put this in our AppServiceProvider. So AppServiceProvider goes into our boot method. Is it boot method? Yep. Okay. Let's paste that in. Let's import Queue. And it's the facade. Let's import JobFailed. And it's this one. And now we can do whatever we want in here. So we can use a notification if we have more than one
Creating Failure Email Mailable2:40
this one. And now we can do whatever we want in here. So we can use a notification if we have more than one and I'll say Slack or SMS in addition to email. Or in this case, I'm just going to use a mailable. So let's go ahead and make a new mailable. Let's stop this. php artisan make:mail. Let's say order sorry not order JobFailedMailable. And let's say markdown equals emails.job-failed. Okay. Okay, back to our code. And
emails.job.failed. Okay. Okay, back to our code. And here we can call the mailable. So let's say Mail::to doesn't matter since we're in dev. And we can do queue. And it's new up that JobFailedMailable. Okay. And it's passed in the event here. So we can output the errors to the user in the email. And did it import JobFailedMailable? Okay, it did.
output the errors to the user in the email. And did it import JobFailed mailable? Okay, it did. So let's go to the JobFailed mailable. Let's go ahead and create a public event here. And let's pass that in to the constructor. So it's JobFailed event. Okay, let's import that. So it's this one. And let's set it here in the constructor. So this event equals event. Okay, so that should be passed through to the mailable since it's public. And it's been a subject here
equals event. Okay, so that should be passed through to the mailable since it's public. And it's been a subject here too. So subject a job failed. Okay. So let's go to that mailable and just output the errors. So let's say a job failed. So that we don't need this component. The job class is going to be remember we have access to the event. So event job. And we can call the resolve
remember we have access to the event. So event job. And we can call the resolveName method to get a string. The job body is also the event job. And we can call the getRawBody method to output a useful string. And the exception is going to be event exception. And we can do getTrace
event exception. And we can do getTraceAsString. So it's a string. And that should do it. Hopefully I did everything correctly. Let's force that exception in our someJob. So we get a failing job. So let's uncomment this. And let's go ahead and run horizon again. Okay. And let's put a few jobs
Debugging Email Notification Issues5:52
Okay. And let's put a few jobs on the queue to say two. Okay. And let's see if we get an email upon the job failure. So do we have any new failed jobs? Okay. So that didn't seem to work. I think I forgot to import Mail in our AppServiceProvider. So let's do that. Okay. And I have to stop my horizon again. Restart it. Okay. Let's force that failure again by hitting this route.
Restart it. Okay. Let's force that failure again by hitting this route. Okay. Any new jobs here? So it's trying pending jobs and they should fail. Okay. So the first one failed. Do we get an email? Okay. So it's not working. I checked the logs and we're getting this error. Serialization of closure is not allowed. So it's trying to serialize this but it's not working. So what you can do is just pass in the values that you need. So we need three values that
Fixing Serialization and Retesting6:56
but it's not working. So what you can do is just pass in the values that you need. So we need three values that we eventually output in the email. So we can pass in these three instead as strings or the quick fix is to not use queues and just send it directly using Mail::send. And that should do the trick. So let's try this again. Let's restart horizon. Let's throw some failed jobs on the queue. And we should get an email here in a few seconds after one of the jobs fails.
And we should get an email here in a few seconds after one of the jobs fails. And there it is. Job failed. Let's see if well there's multiple because I have multiple jobs and you can see the Job class job body and the exception. So everything is working as expected. So definitely set up notifications when you're making use of queues and Horizon in Laravel.
