Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tylerssn's avatar

Can the current exception count be accessed from within a queue job?

I have a standard job configured. I would like to determine my $job->release() value based on the current exception count.

I was using $job->attempts() to modify the $job->release() value but the problem that I am having is that Laravel actually increments $job->attempts() despite whether the job was attempted or not. For example, if I allow only two jobs to run at once, when Laravel prepares to run the third job while the other two or running, it will mark job #3 as attempted though really it was only queued for later - it never attempted to run.

For my use-case, based on the number exceptions that have been logged for the job, I want to adjust my release() value.

p.s.

I see that the \Illuminate\Queue\Worker class calls $this->cache->increment('job-exceptions:'.$uuid) to keep count of the exceptions. This would be the value that I am hoping to gain access to.

0 likes
2 replies
tylerssn's avatar

Assuming that $this->cache->increment('job-exceptions:'.$uuid) refers to the default cache store, clearly the exception count will be available at $this->cache->get('job-exceptions:'.$uuid). I'll put that to the test.

jjudge's avatar

Check out this article if you want to work around the number of attempts being reached. Basically, you can catch the too-many-retries exception and dispatch the job again with its count starting at zero.

https://mauricius.dev/release-laravel-jobs-in-queue-without-increasing-attempts/

I'm using it for a job that needs to grab a lock. The job keeps trying by releasing itself onto the queue until it gets the lock, and each try is counted as a run attempt.

I'm sure I remember reading somewhere of a hook for jobs where you can do this before the count is incremented. Can't remember if it was introduced v7 to v8 though.

Please or to participate in this conversation.