Skip to content

Commit

Permalink
Merge pull request #92 from darwin-homes/fix-race-condition
Browse files Browse the repository at this point in the history
Fetch the task state only once at the beginning and store it in local variable
  • Loading branch information
czue authored Nov 3, 2021
2 parents c26e0d7 + 7db1763 commit 2e744df
Showing 1 changed file with 19 additions and 16 deletions.
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

0 comments on commit 2e744df

Please sign in to comment.