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

Stop Tasks and include exception message #31

Merged
merged 2 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ def my_task(self, seconds):
return result
```

You can add an optional progress description like this:

```python
progress_recorder.set_progress(i + 1, seconds, description='my progress description')
```

You can stop your task with an exception message like this:

```python
progress_recorder.stop_task(i + 1, seconds, 'my exception message')
```

### Displaying progress

In the view where you call the task you need to get the task ID like so:
Expand Down
2 changes: 1 addition & 1 deletion celery_progress/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def get_info(self):
'complete': True,
'success': success,
'progress': _get_completed_progress(),
'result': self.result.get(self.task_id) if success else None,
'result': self.result.get(self.task_id) if success else str(self.result.info),
Copy link
Owner

Choose a reason for hiding this comment

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

is self.result.info guaranteed to exist? mostly just wondering if we need to worry about this raising an exception

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for considering my feature.

Here's the relevant section in the documentation (https://docs.celeryproject.org/en/stable/reference/celery.result.html):
image

If there's nothing set, self.result.info returns a None value, so str(None) will just return 'None'.
If it does throw an exception, the str() function should handle the string conversion also.

Copy link
Owner

Choose a reason for hiding this comment

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

perfect, thanks.

}
elif self.result.state == PROGRESS_STATE:
return {
Expand Down
6 changes: 3 additions & 3 deletions celery_progress/static/celery_progress/celery_progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ var CeleryProgressBar = (function () {
}
}

function onErrorDefault(progressBarElement, progressBarMessageElement) {
function onErrorDefault(progressBarElement, progressBarMessageElement, excMessage) {
progressBarElement.style.backgroundColor = '#dc4f63';
progressBarMessageElement.innerHTML = "Uh-Oh, something went wrong!";
progressBarMessageElement.innerHTML = "Uh-Oh, something went wrong! " + excMessage;
}

function onProgressDefault(progressBarElement, progressBarMessageElement, progress) {
Expand Down Expand Up @@ -48,7 +48,7 @@ var CeleryProgressBar = (function () {
if (data.success) {
onSuccess(progressBarElement, progressBarMessageElement);
} else {
onError(progressBarElement, progressBarMessageElement);
onError(progressBarElement, progressBarMessageElement, data.result);
}
if (data.result) {
onResult(resultElement, data.result);
Expand Down
6 changes: 3 additions & 3 deletions celery_progress/static/celery_progress/websockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ var CeleryWebSocketProgressBar = (function () {
CeleryProgressBar.onResultDefault(resultElement, result);
}

function onErrorDefault(progressBarElement, progressBarMessageElement) {
CeleryProgressBar.onErrorDefault(progressBarElement, progressBarMessageElement);
function onErrorDefault(progressBarElement, progressBarMessageElement, excMessage) {
CeleryProgressBar.onErrorDefault(progressBarElement, progressBarMessageElement, excMessage);
}

function onProgressDefault(progressBarElement, progressBarMessageElement, progress) {
Expand Down Expand Up @@ -45,7 +45,7 @@ var CeleryWebSocketProgressBar = (function () {
if (data.success) {
onSuccess(progressBarElement, progressBarMessageElement);
} else {
onError(progressBarElement, progressBarMessageElement);
onError(progressBarElement, progressBarMessageElement, data.result);
}
if (data.result) {
onResult(resultElement, data.result);
Expand Down