Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
assert self.app.AsyncResult('1') in x.results
x.discard(self.app.AsyncResult('1'))
x.discard(self.app.AsyncResult('1'))
x.discard('1')
assert self.app.AsyncResult('1') not in x.results
x.update([self.app.AsyncResult('2')])
def test_clear(self):
x = self.app.ResultSet([])
r = x.results
x.clear()
assert x.results is r
class MockAsyncResultFailure(AsyncResult):
@property
def result(self):
return KeyError('baz')
@property
def state(self):
return states.FAILURE
def get(self, propagate=True, **kwargs):
if propagate:
raise self.result
return self.result
class MockAsyncResultSuccess(AsyncResult):
def task_wait(self, request, app, uuid):
return {"result": AsyncResult(uuid).get()}
def download_pdf(request, task_id):
task_id = 0
try:
task_id = request.build_absolute_uri().split("task_id=", 1)[1]
except:
return HttpResponseRedirect('/')
work = AsyncResult(task_id)
if work.ready():
try:
result = work.get(timeout=1)
content_type_name = result.get('Content-Type')
if "zip" in content_type_name:
return HttpResponse(result, content_type='application/zip')
else:
return result
except:
return HttpResponseRedirect('/')
return render(request, 'app/error.html')
# If the AsyncResult status is PENDING it means there is no celery task object for this tracker, and we can
# mark it as "dead":
if result.status == 'PENDING':
logging.info("In progress tracker for %s is no longer enqueued, cancelling (task: %s).",
tracker.query_hash, tracker.task_id)
_unlock(tracker.query_hash, tracker.data_source_id)
tracker.update(state='cancelled')
if result.ready():
logging.info("in progress tracker %s finished", tracker.query_hash)
_unlock(tracker.query_hash, tracker.data_source_id)
tracker.update(state='finished')
waiting = QueryTaskTracker.all(QueryTaskTracker.WAITING_LIST)
for tracker in waiting:
result = AsyncResult(tracker.task_id)
if result.ready():
logging.info("waiting tracker %s finished", tracker.query_hash)
_unlock(tracker.query_hash, tracker.data_source_id)
tracker.update(state='finished')
# Maintain constant size of the finished tasks list:
QueryTaskTracker.prune(QueryTaskTracker.DONE_LIST, 1000)
def append_async_result(self):
async_result = AsyncResult(self._id)
if async_result:
try:
if self.status not in TaskModel.CANCELED_STATUS:
self.status = async_result.status
try:
self.statistic.update(async_result.result)
except:
self.statistic['error'] = to_unicode(async_result.result)
except NotImplementedError:
self.status = TaskModel.UNKNOWN
else:
self.status = TaskModel.UNKNOWN
def find(request, token):
try:
signer = TimestampSigner(key=request.session.session_key, sep=':')
id = b64_decode(signer.unsign(token, max_age=86400).encode('utf-8'))
return AsyncResult(id)
except:
return None
inner_task_id = str(uuid.uuid4())
task_name = self.name
tag_list = kwargs.get('tags', [])
group_id = kwargs.get('group_id', None)
# Create a new task status with the task id and tags.
task_status = TaskStatus(task_id=inner_task_id, task_type=task_name,
state=constants.CALL_WAITING_STATE, tags=tag_list,
group_id=group_id)
# To avoid the race condition where __call__ method below is called before
# this change is propagated to all db nodes, using an 'upsert' here and setting
# the task state to 'waiting' only on an insert.
task_status.save_with_set_on_insert(fields_to_set_on_insert=['state', 'start_time'])
_queue_reserved_task.apply_async(args=[task_name, inner_task_id, resource_id, args, kwargs],
queue=RESOURCE_MANAGER_QUEUE)
return AsyncResult(inner_task_id)
def failed(self):
return AsyncResult(str(self.id)).failed()
def get(self, request, *args, **kwargs):
task_id = self.kwargs.get('task_id', None)
res = AsyncResult(task_id) if task_id else None
status = res and res.ready()
if status:
return JsonResponse(
{
'task_ready': status,
'task_successful': res.successful(),
'task_result': res.result if res.successful() else None
}
)
return JsonResponse({'task_ready': status})
def get_async_status(self, task_id):
result = AsyncResult(task_id)
response = {
'status': result.state
}
if result.state in ('PROGRESS', 'SUCCESS'):
response.update(result.result)
elif result.state in ('FAILURE',):
response.update(self.format_exception(result.result))
return response