def process_view(self, request, view_func, view_args, view_kwargs):
"""
Check if response has information about regular pending task.
Loop and check for task result if task_id of a PENDING task is found.
"""
# Ignore socket.io requests
if not request.path.startswith('/api/'):
return None
response = view_func(request, *view_args, **view_kwargs)
# Valid task response is always a TaskResponse objects
if not isinstance(response, TaskResponse):
return response
# api.task.views.task_status should immediately show task status
if response.task_status:
return response
# Only if task/status is PENDING
if response.status_code != HTTP_201_CREATED:
return response
# We need the task_id
# noinspection PyBroadException
try:
task_id = response.data['task_id']
except:
return response
# This should never happen (Dummy task has it's own Response class)
if is_dummy_task(task_id):
return response
# Use streaming only if client is es or es compatible
stream = request.META.get('HTTP_ES_STREAM', None)
if stream:
# Let's render the pending response as it sets some headers (Content-type)
pending_response = response.rendered_content
# Switch to streaming response
stream_res = StreamingHttpResponse(task_status_loop(request, pending_response, task_id, stream=stream),
status=HTTP_201_CREATED)
# Copy headers
# noinspection PyProtectedMember
stream_res._headers = response._headers
# Set custom es_stream header => es will process the stream correctly
stream_res['es_stream'] = bool(stream)
stream_res['es_task_id'] = task_id
return stream_res
else:
return response
评论列表
文章目录