Refs #150 -- Trying to reduce latency

This commit is contained in:
Raphael Michel
2016-07-07 19:44:19 +02:00
parent 8aaf334096
commit 5b9d19b463
2 changed files with 41 additions and 39 deletions

View File

@@ -39,43 +39,46 @@ class AsyncAction:
return self.get_result(request) return self.get_result(request)
return self.http_method_not_allowed(request) return self.http_method_not_allowed(request)
def _return_celery_result(self, res, timeout=.5):
import celery.exceptions
if not res.ready():
try:
res.get(timeout=timeout)
except celery.exceptions.TimeoutError:
pass
ready = res.ready()
data = {
'async_id': res.id,
'ready': ready
}
if ready:
if res.successful() and not isinstance(res.info, Exception):
smes = self.get_success_message(res.info)
if smes:
messages.success(self.request, smes)
# TODO: Do not store message if the ajax client stats that it will not redirect
# but handle the mssage itself
data.update({
'redirect': self.get_success_url(res.info),
'message': str(self.get_success_message(res.info))
})
else:
messages.error(self.request, self.get_error_message(res.info))
# TODO: Do not store message if the ajax client stats that it will not redirect
# but handle the mssage itself
data.update({
'redirect': self.get_error_url(),
'message': str(self.get_error_message(res.info))
})
return data
def get_result(self, request): def get_result(self, request):
from celery.result import AsyncResult from celery.result import AsyncResult
import celery.exceptions
res = AsyncResult(request.GET.get('async_id')) res = AsyncResult(request.GET.get('async_id'))
if 'ajax' in self.request.GET: if 'ajax' in self.request.GET:
if not res.ready(): return JsonResponse(self._return_celery_result(res, timeout=0.25))
try:
res.get(timeout=0.5)
except celery.exceptions.TimeoutError:
pass
ready = res.ready()
data = {
'async_id': res.id,
'ready': ready
}
if ready:
if res.successful() and not isinstance(res.info, Exception):
smes = self.get_success_message(res.info)
if smes:
messages.success(self.request, smes)
# TODO: Do not store message if the ajax client stats that it will not redirect
# but handle the mssage itself
data.update({
'redirect': self.get_success_url(res.info),
'message': str(self.get_success_message(res.info))
})
else:
messages.error(self.request, self.get_error_message(res.info))
# TODO: Do not store message if the ajax client stats that it will not redirect
# but handle the mssage itself
data.update({
'redirect': self.get_error_url(),
'message': str(self.get_error_message(res.info))
})
return JsonResponse(data)
else: else:
if res.ready(): if res.ready():
if res.successful(): if res.successful():
@@ -85,14 +88,13 @@ class AsyncAction:
return render(request, 'pretixpresale/waiting.html') return render(request, 'pretixpresale/waiting.html')
def _do_celery(self, args): def _do_celery(self, args):
rs = self.task.task.apply_async(args=args) res = self.task.task.apply_async(args=args)
if 'ajax' in self.request.GET or 'ajax' in self.request.POST: if 'ajax' in self.request.GET or 'ajax' in self.request.POST:
return JsonResponse({ data = self._return_celery_result(res)
'async_id': rs.id, data['check_url'] = self.get_check_url(res.id, True)
'check_url': self.get_check_url(rs.id, True) return JsonResponse(data)
})
else: else:
return redirect(self.get_check_url(rs.id, False)) return redirect(self.get_check_url(res.id, False))
def _do_sync(self, args): def _do_sync(self, args):
try: try:

View File

@@ -35,7 +35,7 @@ function async_task_callback(data, jqXHR, status) {
} }
async_task_id = data.async_id; async_task_id = data.async_id;
async_task_check_url = data.check_url; async_task_check_url = data.check_url;
async_task_timeout = window.setTimeout(async_task_check, 250); async_task_timeout = window.setTimeout(async_task_check, 100);
} }
function async_task_error(jqXHR, textStatus, errorThrown) { function async_task_error(jqXHR, textStatus, errorThrown) {