Skip to content

Commit

Permalink
Address another possible race condition where the task status might c…
Browse files Browse the repository at this point in the history
…hange between the time when we fetch "state" and the time we fetch "info"
  • Loading branch information
mobiware committed Nov 1, 2021
1 parent 5c03d01 commit 7db1763
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions celery_progress/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ def __init__(self, result):
self.result = result

def get_info(self):
state = self.result.state
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()
Expand All @@ -69,16 +71,16 @@ def get_info(self):
'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 state in ['RETRY', 'REVOKED']:
if state == 'RETRY':
retry = self.result.info
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,
Expand All @@ -90,13 +92,13 @@ def get_info(self):
'complete': True,
'success': None,
'progress': _get_completed_progress(),
'result': str(self.result.info)
'result': str(info)
})
elif state == PROGRESS_STATE:
response.update({
'complete': False,
'success': None,
'progress': self.result.info,
'progress': info,
})
elif state in ['PENDING', 'STARTED']:
response.update({
Expand All @@ -105,7 +107,7 @@ def get_info(self):
'progress': _get_unknown_progress(state),
})
else:
logger.error('Task %s has unknown state %s with metadata %s', self.result.id, 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,
Expand Down

0 comments on commit 7db1763

Please sign in to comment.