Skip to content

[13.x] Add Queue::whenJobRunsLongerThan - #62141

Merged
taylorotwell merged 1 commit into
laravel:13.xfrom
jackbayliss:whenJobRunsLongerThan
Oct 28, 2026
Merged

taylorotwell merged 1 commit into
laravel:13.xfrom
jackbayliss:whenJobRunsLongerThan

Conversation

@jackbayliss

@jackbayliss jackbayliss commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Laravel offers whenQueryingForLongerThan,whenRequestLifecycleIsLongerThan as well as whenCommandLifecycleIsLongerThan, but there's nothing equivalent for queued jobs.

This PR adds Queue::whenJobRunsLongerThan :

Queue::whenJobRunsLongerThan(CarbonInterval::seconds(90), function ($job, $duration) {
    Log::warning("{$job->resolveName()} took toooo long Jack");
});

My thought here is I wanna know when, which job, and how long it ran for - over a certain amount of time (90 seconds), so I know they should be moved to a Pro worker.

More generally, it's useful for spotting jobs that are creeping toward their timeout, a hosting platform's runtime limit etc.

Only downside I guess is we can just listen to JobAttempted manually / macro it, but you have to know about it, so by putting it on the manager its on the facade soooo the people can see it via intelephense and its similar to the other methods.

I also guess most people would try to use Queue::after() but that doesn't handle failed jobs.

So, givin this one a go feel free to throw away if you aint feelin the vibe

@github-actions

Copy link
Copy Markdown

Thanks for submitting a PR!

Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@jackbayliss
jackbayliss marked this pull request as ready for review October 27, 2026 14:50
@taylorotwell
taylorotwell merged commit 71df784 into laravel:13.x Oct 28, 2026
64 checks passed
@jackbayliss
jackbayliss deleted the whenJobRunsLongerThan branch October 28, 2026 23:05
albertcht pushed a commit to hypervel/docs that referenced this pull request Nov 3, 2026
Applications had no direct way to react when a queued job attempt takes
too long. QueueManager::whenJobRunsLongerThan() registers a JobAttempted
listener that calls the handler with the job and its duration in
milliseconds when the attempt exceeds the threshold. The threshold
accepts a date, CarbonInterval or number, like
Connection::whenQueryingForLongerThan().

Ports laravel/framework#62141 from 13.x at b20bf7f442. The method is
natively typed and carries a Boot-only warning like the other listener
registrars. Upstream has no docs, so the Job Events section of
queues.md now describes it.

Tests: ported QueueManagerTest::testWhenJobRunsLongerThan. tests/Queue
passes; composer analyse is clean.
albertcht pushed a commit to hypervel/queue that referenced this pull request Nov 3, 2026
Applications had no direct way to react when a queued job attempt takes
too long. QueueManager::whenJobRunsLongerThan() registers a JobAttempted
listener that calls the handler with the job and its duration in
milliseconds when the attempt exceeds the threshold. The threshold
accepts a date, CarbonInterval or number, like
Connection::whenQueryingForLongerThan().

Ports laravel/framework#62141 from 13.x at b20bf7f442. The method is
natively typed and carries a Boot-only warning like the other listener
registrars. Upstream has no docs, so the Job Events section of
queues.md now describes it.

Tests: ported QueueManagerTest::testWhenJobRunsLongerThan. tests/Queue
passes; composer analyse is clean.
binaryfire added a commit to hypervel/components-backup that referenced this pull request Nov 3, 2026
Applications had no direct way to react when a queued job attempt takes
too long. QueueManager::whenJobRunsLongerThan() registers a JobAttempted
listener that calls the handler with the job and its duration in
milliseconds when the attempt exceeds the threshold. The threshold
accepts a date, CarbonInterval or number, like
Connection::whenQueryingForLongerThan().

Ports laravel/framework#62141 from 13.x at b20bf7f442. The method is
natively typed and carries a Boot-only warning like the other listener
registrars. Upstream has no docs, so the Job Events section of
queues.md now describes it.

Tests: ported QueueManagerTest::testWhenJobRunsLongerThan. tests/Queue
passes; composer analyse is clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants