Skip to content
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

handler after the function fails more than max_attempts #1257

Open
saro2-a opened this issue Dec 18, 2024 · 4 comments
Open

handler after the function fails more than max_attempts #1257

saro2-a opened this issue Dec 18, 2024 · 4 comments

Comments

@saro2-a
Copy link

saro2-a commented Dec 18, 2024

I would like to mark the task as failed in my database and alert the user if the task cannot run after max_attempts.

Is there a way to pass the max_attempts configured in the context?

@ewjoachim
Copy link
Member

I'm not 100% sure I understand your usecase, but it looks like the retry feature. Can you have a look at the doc and tell whether it's relevant, and if not, what you'd need that wouldn't be in there ?

@saro2-a
Copy link
Author

saro2-a commented Dec 18, 2024 via email

@ewjoachim
Copy link
Member

ewjoachim commented Dec 18, 2024

Still not sure it's what you want, but:

@app.task(retry=10, pass_context=True)
def my_task(job_context: procrastinate.JobContext) -> None:
    if job_context.job.attempt == job_context.task.retry.max_attempts:
        print("Warning: last attempt!")
        
    if random.random() < 0.9:
        raise Exception

@ewjoachim
Copy link
Member

ewjoachim commented Dec 18, 2024

Alternatively, something that would work better with custom retry strategies:

@app.task(retry=10, pass_context=True)
def my_task(job_context: procrastinate.JobContext) -> None:
    if job_context.task.retry.get_retry_decision(exception=Exception(), job=job_context.job) is None:
        print("Warning: last attempt!")
        
    if random.random() < 0.9:
        raise Exception

(of course, it may depend on the exception you set, as you may retry differently on different exceptions)
(note: untested code!)

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

No branches or pull requests

2 participants