Laravel Queues Lesson 2 — Failed jobs: listing, retrying and handling them

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys how's it going and welcome to a new video on the last video we spoke a lot about cues about how to push jobs into EQ how to list the jobs that failed all that stuff and on this lesson we're gonna cover a little bit more about Fair jobs how can we see the fair jobs where are they stored and how can we retry them so with no further Ado let's jump into the last before we jump into the lesson I'm going to ask you to please leave a like on this video subscribe to the channel check out the newsletter that really really really helps me and if you think this video was good or any other video on the channel was good content I would really appreciate if you could share it with some friends on Twitter maybe because it really really really helps me but anyway let's jump into the lesson all right so just like in the previous lesson I have the message sender service here and it's just sleep for one second to mimic a very slow API and then it is throwing an exception so I already seated the database worth a hundred fake messages so if we go to Massachusetts we have a couple records what we want to do right now is go here and hit the send API and okay so the messages should be cute if we go into radius and let me close this Let me refresh it we see a hundred records so now we just want to run Artisan Q work so we can run PHP art using Q work and you can see we got one fail a bunch of failures that's enough now if we go into our database refresh it and go into fail jobs you can see we now have a couple records here we have the job euid we have the connection and we're using radish we have the queue and we haven't specified a queue so it's fall back into default we have the payload and the payload includes everything that you would see just like on the radish shop so if we go to Radice and click on the job for instance this one you can set it hasn't payload like I said you have the class that should be that should be executed you have the arguments all that stuff so it's stored here and you have the exception that it was thrown um this is really useful if you want to check logs or something like that and you also have when the job failed so we have four jobs that failed and and we can also shock the third job so we can run Artisan Q failed and it is going to list the fair jobs we also have a command called origin Q retry and we can pass a job euid for instance I'm going to pick this one uh let me just select this properly I'm having a hard time I can say art doesn't retry and I'm going to remove this exception here because we want to see it succeed so if I run this you can see that it worked so if I go back and refresh it you can see that it was removed from the fail job stable now Okay cool so we still have some jobs here how can we retry all of them well if you want to retry any specific queue you can pass that as an argument to this method but you can also pass all so if I run Artisan Q retry all it is going to retry all jobs now one interesting thing is that we can also specify how many times we want to try a job so for instance let's process all the jobs that we have on the Queue and let's remove this delay just so it's a little bit faster so let me go here I'm gonna comment this rerun artisanq work and one of them failed that's interesting let's see what happened um Max attempts okay so this is what we're going to talk about let me delete this one let's stop this all right it's cool so I made a small change uh we're now getting one single message and that so we can test things the message sender is throwing an exception once again we don't have anything on Radice we don't have anything on the fair jobs table so let's see that API we should now have something on radius let's refresh it there we go and I'm gonna run a different command I'm gonna say Artisan Q work and I'm going to say I want to try a job three times so let's run it okay fail fail fail and you can see that we got two failures with this yellow warning and then we got one failure with this red warning and if we go to SQL A's you we can see the fair job and we can retry it we can re-add it to the queue so let me say arts and Q retry all it's back into the queue fail fail fail so we're instructing where available to go hey try this job once if it doesn't work try it again if it doesn't work try it again and then if nothing works just send it to the failed jobs table so I'm gonna stop this I'm going to clear this table and let's redo this with a small change now you saw that it specify this on the CLI command but there's another way to do this you can add this to the job as well you can go here and you can add a property called tries so you can see let's try five times and let's do the same thing let's add it to the queue and now let's process it and I'm not going to add the choice option the trace flag let's try fail fail fail fail and then the right fail so you can have the tries option both in the Q level in the queue worker level as well as in the job level what do you want to do well it depends sometimes you want to have some jobs being retry a couple more times sometimes you want jobs to have different timeouts and we're not gonna go into timeouts but you can also specify different types different levels of timeouts on the job itself this is all in the documentation but this is pretty much how you handle failure now another really cool thing about laravel is you can add a method call let's um it's called fail and it expects a throwable so we can say throwable and for now we're only going to dampen die so do we have something in the queue I don't think so let's yeah we have a fair job let's remove it and redo what we were doing so let's refresh it oops unclose line oh I see it so my value guys you see how this happens to everyone okay let's retry it okay it's being added to the queue let's run the queue failed failed failed and you can see that on the actual failure when it sends the job into the fair job Sable we can run something so maybe you want to alert someone maybe you want to send a message on slack maybe you want to do some action on your application but you can definitely handle their jobs so when you see the yellow fail it's not actual it's not it's not an actual failure uh I mean it is a failure but it's going to be retried it really fails when level sends the job to the fair job saver it's like hey we try processing that we tried doing this as many times as you wanted it did not work out so here you go this is what happened and if you want to retry it feel free to but that's pretty much how retrying Works in learn file all right I think there was quite a lot um I hope you managed to understand how to deal with Fair jobs in Largo Largo is really really robust so it provides you everything that you need to manage those jobs in a very organized way and in the next lesson we're going to take a look at Horizon which is a first party package by laravel and man it should be paid the competitors in other ecosystems uh packages that provide the same functionality are paid and it's searchable asset Horizon isn't I mean you guys are going to see it and you're going to agree with me that Horizon should be paid softer but thankfully we get it for free so see you in the next lesson like I said please leave a like Please Subscribe that really really helps me and if you could share this video with your friends I'd really appreciate it see you in the next video bye-bye
Info
Channel: Mateus Guimarães
Views: 4,709
Rating: undefined out of 5
Keywords: php, laravel, tdd, test driven development, clean code
Id: IR5BSaPg0mE
Channel Id: undefined
Length: 8min 31sec (511 seconds)
Published: Mon Nov 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.