Skip to content

Prevent multiple execution of aborted jobs #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 31, 2025

Conversation

luke-
Copy link
Contributor

@luke- luke- commented May 19, 2025

Q A
Is bugfix? ✔️
New feature?
Breaks BC?
Fixed issues

We have the situation that apparently in some environments, jobs canceled by max_execution_time are not correctly removed from the queue. As a result, they are executed again and again. Even if no RetryableJobInterface is implemented or canRetry() always returns false.

Unfortunately, I can't reproduce it myself, but some users can in connection with CPanel environments. Perhaps the worker is there not running in CLI but in CGI mode.

In our project, we have now successfully solved the problem using the ExecEvent. But maybe it makes sense to add this fix (even if I am not completely happy with it) to the upstream queue project.

Related issue: humhub/calendar#547

Copy link
Member

@samdark samdark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a valid case. Would you please add a line for CHANGELOG? Thanks.

@luke-
Copy link
Contributor Author

luke- commented May 21, 2025

@samdark Thanks for the feedback. I still need to investigate why the tests are failing. After that, I'll adapt the CHANGELOG.

src/Queue.php Outdated
if ($job instanceof RetryableJobInterface && !$job->canRetry($attempt - 1, $error)) {
return true;
} else {
// Non RetryableJobs can have a maximum of one attempt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumption is incorrect. RetryableJobInterface means that job can decide whether it should be retried. In other case it is controlled by attempts property at queue level - job can still have multiple retries even if it doesn't implement RetryableJobInterface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rob006 Thanks for pointing that out. I've adjusted the condition.
However, I still need to check the tests.

@luke-
Copy link
Contributor Author

luke- commented May 21, 2025

I was able to fix the tests. Unfortunately, PHP 5.4 and PHP 7 fail for other reasons. I don't have time to investigate this further at the moment.

I'm not entirely happy with my fix/PR, as these failed jobs are closed without any error message. Unfortunately, no specific error is available either. At least these jobs are not executed multiple times.

src/Queue.php Outdated
@@ -225,6 +225,16 @@ public function getWorkerPid()
protected function handleMessage($id, $message, $ttr, $attempt)
{
list($job, $error) = $this->unserializeMessage($message);

// Handle aborted jobs without thrown error
if ($attempt > 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that all this can be combined into one condition

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@s1lver Shall I summarize that? I'd be happy to. I personally prefer the breakdown for quick readability.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this solution? Will return true if any of the conditions evaluate to true

return
    ($job instanceof RetryableJobInterface && !$job->canRetry($attempt - 1, $error)) 
    || (!($job instanceof RetryableJobInterface) && $attempt > $this->attempts)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@s1lver Thanks for the example.

I've summarized the condition here:
99d9f89

I'm not sure about the return in your example, since we only return true if the condition matches.

@samdark samdark requested review from s1lver and rob006 May 29, 2025 21:15
luke- and others added 2 commits May 30, 2025 08:30
Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
@samdark samdark merged commit 26fb7fe into yiisoft:master May 31, 2025
4 of 7 checks passed
@samdark
Copy link
Member

samdark commented May 31, 2025

Thank you!

samdark pushed a commit that referenced this pull request Jun 3, 2025
Co-authored-by: Lucas Bartholemy <luke-@users.noreply.github.com>
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.

4 participants