예제 #1
0
    def __init__(self, task, user_id, cache_time=3000):
        self.task_id = task.request.id
        self.user = User.objects.get(
            id=user_id
        )  # user is used by other code that deal with celery progress bar
        self.task_stat_id = "celery-stat-%s" % self.task_id
        self.task_kill_id = "celery-kill-%s" % self.task_id
        self.task_msg_all_id = "celery-%s-msg-all" % self.task_id
        self.cache_time = cache_time
        self.result = {
            'msg': "IN PROGRESS",
            'sticky_msg': '',
            'progress_percent': 0,
            'is_killed': False,
            'user_id': user_id,
            'msg_index': 0,
        }
        self.last_err = ""
        self.last_err_type = None
        self.fatal = False

        self.msg = ""
        cache.set(self.task_msg_all_id, "", self.cache_time)

        # Normally we want to have created the CeleryTasks object before even the task is run. Reason: if the task is in the queue, we want to know that.
        # In that case these lines are not even run yet! However, we need some delay here before the task is created and the CeleryTasks object created
        # so we can get the object. Otherwise it can't "get" the object and it thinks it doesn't exist

        for j in range(1, 10):
            sleep(.3)
            try:
                self.celery_task_history_obj = CeleryTasks.objects.get(
                    task_id=self.task_id)
                break
            except CeleryTasks.DoesNotExist:
                pass

        if j >= 9:
            raise Exception("Could not get the celery_task_history_obj")

        self.celery_task_history_obj.status = "active"
        self.celery_task_history_obj.start_date = timezone.now()
        self.celery_task_history_obj.save(
            update_fields=["status", "start_date"])
        now = timezone.now()
        try:
            CeleryTasks.objects.filter(
                creation_date__lte=now - timezone.timedelta(hours=24),
                status__in=["active",
                            "waiting"]).update(status="must have failed")
            CeleryTasks.objects.filter(creation_date__lte=now -
                                       timezone.timedelta(hours=600)).delete()
        except:
            logger.error("Error in cleaning up CeleryTasks History",
                         exc_info=True)
예제 #2
0
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')
예제 #3
0
    def __init__(self, task, user_id, cache_time=3000):
        self.task_id = task.request.id
        self.user = User.objects.get(id=user_id)  # user is used by other code that deal with celery progress bar
        self.task_stat_id = "celery-stat-%s" % self.task_id
        self.task_kill_id = "celery-kill-%s" % self.task_id
        self.task_msg_all_id = "celery-%s-msg-all" % self.task_id
        self.cache_time = cache_time
        self.result = {'msg': "IN PROGRESS", 'sticky_msg': '', 'progress_percent': 0, 'is_killed': False,
                       'user_id': user_id, 'msg_index': 0, }
        self.last_err = ""
        self.last_err_type = None
        self.fatal = False

        self.msg = ""
        cache.set(self.task_msg_all_id, "", self.cache_time)

        # Normally we want to have created the CeleryTasks object before even the task is run. Reason: if the task is in the queue, we want to know that.
        # In that case these lines are not even run yet! However, we need some delay here before the task is created and the CeleryTasks object created
        # so we can get the object. Otherwise it can't "get" the object and it thinks it doesn't exist

        for j in range(1, 10):
            sleep(.3)
            try:
                self.celery_task_history_obj = CeleryTasks.objects.get(task_id=self.task_id)
                break
            except CeleryTasks.DoesNotExist:
                pass

        if j >= 9:
            raise Exception("Could not get the celery_task_history_obj")

        self.celery_task_history_obj.status = "active"
        self.celery_task_history_obj.start_date = timezone.now()
        self.celery_task_history_obj.save(update_fields=["status", "start_date"])
        now = timezone.now()
        try:
            CeleryTasks.objects.filter(creation_date__lte=now - timezone.timedelta(hours=24),
                                       status__in=["active", "waiting"]).update(status="must have failed")
            CeleryTasks.objects.filter(creation_date__lte=now - timezone.timedelta(hours=600)).delete()
        except:
            logger.error("Error in cleaning up CeleryTasks History", exc_info=True)
예제 #4
0
 def set_cache(self):
     cache.set(self.task_stat_id, self.result, time=self.cache_time)
예제 #5
0
 def set_cache(self):
     cache.set(self.task_stat_id, self.result, time=self.cache_time)