Ejemplo n.º 1
0
 def patch(self, request, **kwargs):
     decision = get_object_or_404(Decision, **kwargs)
     if 'status' not in request.data:
         raise exceptions.ValidationError('Status is required')
     if request.data['status'] == DECISION_STATUS[7][0]:
         if decision.status != DECISION_STATUS[6][0]:
             raise exceptions.ValidationError(
                 "The decision status is not cancelling")
         decision.status = DECISION_STATUS[7][0]
         decision.save()
         decision_status_changed(decision)
     elif request.data['status'] == DECISION_STATUS[3][0]:
         res = FinishDecision(decision, DECISION_STATUS[3][0])
         if res.error:
             raise exceptions.ValidationError({'job': res.error})
         return Response({})
     elif request.data['status'] == DECISION_STATUS[4][0]:
         FinishDecision(decision, DECISION_STATUS[4][0],
                        request.data.get('error'))
         return Response({})
     else:
         raise exceptions.APIException(
             'Unsupported decision status: {}'.format(
                 request.data['status']))
     return Response({})
Ejemplo n.º 2
0
 def post(self, request, **kwargs):
     job = self.get_job(**kwargs)
     configuration = self.get_configuration()
     decision = create_default_decision(request, job, configuration)
     decision_status_changed(decision)
     return Response({
         'id': decision.id,
         'identifier': str(decision.identifier),
         'url': reverse('jobs:decision', args=[decision.id])
     })
Ejemplo n.º 3
0
 def post(self, request, **kwargs):
     with transaction.atomic():
         decision = get_object_or_404(Decision.objects.select_for_update(),
                                      **kwargs)
         if not DecisionAccess(request.user, decision).can_stop:
             raise exceptions.PermissionDenied(
                 _("You don't have an access to stop this decision"))
         cancel_decision(decision)
     decision_status_changed(decision)
     # If there are a lot of tasks that are not still deleted it could be too long
     # as there is request to DB for each task here (pre_delete signal)
     decision.tasks.all().delete()
     return Response({})
Ejemplo n.º 4
0
    def retrieve(self, request, *args, **kwargs):
        with transaction.atomic():
            instance = self.get_object()
            if instance.status == DECISION_STATUS[1][0]:
                # Update pending decision
                instance.status = DECISION_STATUS[2][0]
                instance.save()
                decision_status_changed(instance)
            elif instance.status != DECISION_STATUS[2][0]:
                raise exceptions.APIException('The job is not solving')

        generator = KleverCoreArchiveGen(instance)
        mimetype = mimetypes.guess_type(generator.arcname)[0]
        response = StreamingHttpResponse(generator, content_type=mimetype)
        response[
            'Content-Disposition'] = 'attachment; filename="%s"' % generator.arcname
        return response
Ejemplo n.º 5
0
 def finish_tasks(self, scheduler: Scheduler):
     decisions_qs = scheduler.decision_set.filter(status__in=[
         DECISION_STATUS[1][0], DECISION_STATUS[2][0], DECISION_STATUS[6][0]
     ])
     if scheduler.type == SCHEDULER_TYPE[0][0]:
         decisions_qs = decisions_qs.select_related('job')
     for decision in decisions_qs:
         decision.tasks_pending = decision.tasks_processing = 0
         # Pending or processing tasks
         tasks_updated = Task.objects.filter(
             status__in=[TASK_STATUS[0][0], TASK_STATUS[1][0]],
             decision=decision).update(error=self.task_error)
         decision.tasks_error += tasks_updated
         if scheduler.type == SCHEDULER_TYPE[0][0]:
             if not decision.finish_date:
                 decision.finish_date = now()
             if not decision.error:
                 decision.error = self.decision_error
             decision.status = DECISION_STATUS[8][0]
             decision_status_changed(decision)
         decision.save()
Ejemplo n.º 6
0
    def post(self, request, **kwargs):
        try:
            job = self.get_job(**kwargs)

            if 'file_conf' in self.request.FILES:
                configuration = GetConfiguration(
                    file_conf=self.request.FILES['file_conf']).for_json()
            else:
                configuration = get_default_configuration(
                    self.request.user).for_json()

            decision = create_default_decision(request, job, configuration)
            decision_status_changed(decision)
        except Exception as e:
            logger.exception(e)
            raise exceptions.APIException(
                'Error occured for starting default decision')
        return Response({
            'id': decision.id,
            'identifier': str(decision.identifier),
            'url': reverse('jobs:decision', args=[decision.id])
        })
Ejemplo n.º 7
0
    def __init__(self, decision, status, error=None):
        self.decision = decision
        self.error = error
        self.status = self.__get_status(status)
        try:
            self.__remove_tasks()
        except ServiceError as e:
            logger.exception(e)
            self.decision.error = str(e)
            self.status = DECISION_STATUS[5][0]
        if self.error is not None:
            if len(self.error) > 1024:
                logger.error(
                    "The decision '{}' finished with large error: {}".format(
                        self.decision.identifier, self.error))
                self.error = "Length of error for decision '{}' is large (1024 characters is maximum)"\
                    .format(self.decision.identifier)
                self.status = DECISION_STATUS[8][0]
            self.decision.error = self.error

        self.decision.status = self.status
        self.decision.finish_date = now()
        self.decision.save()
        decision_status_changed(self.decision)