def task_api(request): """ A view to report the progress to the user """ if not request.user.is_active: raise PermissionDenied if request.method == "GET": task_id = request.GET.get('id', False) terminate = request.GET.get('terminate', False) msg_index_client = request.GET.get('msg_index_client', False) elif request.method == "POST": task_id = request.POST.get('id', False) terminate = request.POST.get('terminate', False) msg_index_client = request.POST.get('msg_index_client', False) else: task_id = False terminate = False msg_index_client = False if task_id: task_key = "celery-stat-%s" % task_id task_stat = cache.get(task_key) try: if task_stat['user_id'] != request.user.id: return HttpResponse('Unauthorized', status=401) except TypeError: return HttpResponse('Unauthorized', status=401) else: # logger.info("msg_index_client: %s task_stat['msg_index']: %s" % (msg_index_client, task_stat['msg_index'])) try: msg_index_client = int(msg_index_client) except: task_stat['msg_chunk'] = "Error in pointer index server call" else: if msg_index_client is not False and msg_index_client < task_stat['msg_index']: whole_msg = cache.get("celery-%s-msg-all" % task_id) task_stat['msg_chunk'] = whole_msg[msg_index_client:] else: task_stat = None if task_stat and terminate == "1": cache.set("celery-kill-%s" % task_id, True, 60 * 5) return HttpResponse(json.dumps(task_stat), content_type='application/json')
def get_kill(self): return cache.get(self.task_kill_id)