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

Fetch the task state only once at the beginning and store it in local variable #92

Merged
merged 2 commits into from
Nov 3, 2021
Merged
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
35 changes: 19 additions & 16 deletions celery_progress/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,56 +60,59 @@ def __init__(self, result):
self.result = result

def get_info(self):
response = {'state': self.result.state}
if self.result.state in ['SUCCESS', 'FAILURE']:
task_meta = self.result._get_task_meta()
state = task_meta["status"]
info = task_meta["result"]
response = {'state': state}
if state in ['SUCCESS', 'FAILURE']:
success = self.result.successful()
with allow_join_result():
response.update({
'complete': True,
'success': success,
'progress': _get_completed_progress(),
'result': self.result.get(self.result.id) if success else str(self.result.info),
'result': self.result.get(self.result.id) if success else str(info),
})
elif self.result.state in ['RETRY', 'REVOKED']:
if self.result.state == 'RETRY':
retry = self.result.info
elif state in ['RETRY', 'REVOKED']:
if state == 'RETRY':
retry = info
when = str(retry.when) if isinstance(retry.when, datetime.datetime) else str(
datetime.datetime.now() + datetime.timedelta(seconds=retry.when))
result = {'when': when, 'message': retry.message or str(retry.exc)}
else:
result = 'Task ' + str(self.result.info)
result = 'Task ' + str(info)
response.update({
'complete': True,
'success': False,
'progress': _get_completed_progress(),
'result': result,
})
elif self.result.state == 'IGNORED':
elif state == 'IGNORED':
response.update({
'complete': True,
'success': None,
'progress': _get_completed_progress(),
'result': str(self.result.info)
'result': str(info)
})
elif self.result.state == PROGRESS_STATE:
elif state == PROGRESS_STATE:
response.update({
'complete': False,
'success': None,
'progress': self.result.info,
'progress': info,
})
elif self.result.state in ['PENDING', 'STARTED']:
elif state in ['PENDING', 'STARTED']:
response.update({
'complete': False,
'success': None,
'progress': _get_unknown_progress(self.result.state),
'progress': _get_unknown_progress(state),
})
else:
logger.error('Task %s has unknown state %s with metadata %s', self.result.id, self.result.state, self.result.info)
logger.error('Task %s has unknown state %s with metadata %s', self.result.id, state, info)
response.update({
'complete': True,
'success': False,
'progress': _get_unknown_progress(self.result.state),
'result': 'Unknown state {}'.format(self.result.state),
'progress': _get_unknown_progress(state),
'result': 'Unknown state {}'.format(state),
})
return response

Expand Down