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

Stopping celery task #4

Merged
merged 2 commits into from
Sep 10, 2019
Merged

Stopping celery task #4

merged 2 commits into from
Sep 10, 2019

Conversation

snakesonabrain
Copy link

Hi, first of all congratulations on your great effort to write this.

I updated the code to reflect a use case where an exception is raised during task execution but I want to handle it and then show a failure in the progress bar.

To do this, I added a method stop_task to ProgressRecorder which is then called as follows:

from celery.exceptions import Ignore

@shared_task(bind=True)
def test_task(self, delay=5, totaltime=10):
    progress_recorder = ProgressRecorder(self)
    try:
        for i in range(totaltime):
            time.sleep(delay)
            if i > 7:
                raise ValueError("Provoked an error")
            progress_recorder.set_progress(i + 1, totaltime)
        return 'done'
    except Exception as err:
        progress_recorder.stop_task(i + 1, 1, err)
        raise Ignore

By doing this, I fill up the progress bar to the maximum but then have a failed state for the task so that the error behaviour is triggered.

Let me know what you think about this tweak.

@CoryADavis
Copy link

I was wondering about this exact feature. Thanks for writing it, works great for what I needed.

Copy link
Owner

@czue czue left a comment

Choose a reason for hiding this comment

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

This is great, thanks. Sorry for my exceptional slowness! My notification settings prevented me from seeing it. 🙈

@czue czue merged commit 9c60b3e into czue:master Sep 10, 2019
@czue
Copy link
Owner

czue commented Sep 10, 2019

Just released 0.0.5 which adds this. Thanks!

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.

None yet

3 participants