Laravel 11: Job thottling. A throttle is a try? #52101
Replies: 6 comments 8 replies
-
Hi. I use
in my jobs and always try catch the code from handle function. If the code fails I log the error and that is it. In that way the laravel logic for failed jobs is bypassed. If I want to create a new job, I dispatch a new one from the catch block. |
Beta Was this translation helpful? Give feedback.
-
I wrote a method that releases a job back on the queue without a try. Basically it creates a new job, thus any changes are remembered. Not sure if it helps with your situation, but if interested, I can share the code. It is quite simple really ... |
Beta Was this translation helpful? Give feedback.
-
Okay, maybe I found a way to work with this. I read in the documentation that there is a maxExceptions property. So if the max tries is set to 2 and the maxExceptions is set to 1, it should never run a second time when an exception is thrown. Throttling is not an exception, thus it should be able to throttle and still run the code and not retry upon an exception, even if there is still a retry left. I will try to test this today. |
Beta Was this translation helpful? Give feedback.
-
The logic as described works. It is throttling without failing. and on a exception it fails completely. il leave the discussion open for other to see en find the info als to keep it moving. As described above i did this different before. I will share my previous code below.
|
Beta Was this translation helpful? Give feedback.
-
@gldrenthe89 There is no need to do reflection. In my case it is just back on the queue. public function backOnTheQueue(): PendingDispatch
{
$this->delete();
$this->job = null;
$this->delay = 0;
$this->requeued += 1;
$this->releaseLock();
$job = clone $this;
return dispatch($job)
->onQueue($this->queue)
->onConnection($this->connection);
}
public function releaseLock(): void
{
if ($this instanceof ShouldBeUnique) {
(new UniqueLock(Cache::store()))->release($this);
}
} Then in handle when you need to throttle you can call |
Beta Was this translation helpful? Give feedback.
-
I will close this now as i have everything up en runnning the way it should be. thanks @henzeb for you insights |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there,
I am currently building a new application where I need to make connections to third-party APIs. Obviously, I need to throttle the calls to those APIs. I want to do this using a Throttle middleware asdocumented. I have this working, but I found out that each time a job is throttled, it gets a +1 on tries. In my humble opinion, this should not be the case, as the code inside the handle() function itself hasn’t been run yet. This is presenting me with an issue. It is important that these specific jobs only run once, even if they fail. They should not retry on failure. Thus, in my Laravel Horizon settings, I set max tries to 1. As you can predict, every job that is throttled gets an additional try, thus failing on the second attempt.
Any suggestions?
Beta Was this translation helpful? Give feedback.
All reactions