Example #1
0
    def update(self, status=None):
        self.status = status
        if status == Job.STATUS_DONE:
            self.data_model.status = Job.STATUS_DONE
            self.data_model.save()
            return True

        if status == Job.STATUS_RUNNING:
            self.data_model.status = Job.STATUS_RUNNING
            self.data_model.save()
            return True

        if status == Job.STATUS_PENDING:
            self.data_model.status = Job.STATUS_PENDING
            self.data_model.save()
            return True

        if status == Job.STATUS_CANCELED:
            self.data_model.status = Job.STATUS_CANCELED
            jobs = Job.select().where(Job.task == self.id)

            for i in jobs:
                if i.status == Job.STATUS_CANCELED:
                    continue
                # we do not interrupt the running job
                # if i.status == Job.STATUS_RUNNING:
                #     i.status = Job.STATUS_CANCELED
                if i.status == Job.STATUS_EXCEPTION:
                    i.status = Job.STATUS_FAILED
                if i.status == Job.STATUS_PENDING:
                    i.status = Job.STATUS_CANCELED
                i.save()

            self.data_model.save()
            return True

        if status == Job.STATUS_FAILED:
            self.data_model.status = Job.STATUS_FAILED
            jobs = Job.select().where(Job.task == self.id)

            for i in jobs:
                if i.status == Job.STATUS_CANCELED:
                    continue
                # cause we do not call this method if job is running
                # TODO think about of checking process pid
                if i.status == Job.STATUS_RUNNING:
                    i.status = Job.STATUS_FAILED
                if i.status == Job.STATUS_EXCEPTION:
                    i.status = Job.STATUS_FAILED
                if i.status == Job.STATUS_PENDING:
                    i.status = Job.STATUS_CANCELED

                i.save()

            self.data_model.save()
            return True

        return False
Example #2
0
def task_paging(request):
    """
    Получить список заданий

    :param request:
    :return:
    """
    # task_paging/list/?sort=created&order=desc&limit=100&offset=0"
    # ?limit=10&offset=10&order=asc
    sorting = JobDataModel.id.desc()




    if 'sort' in request.GET:
        sorting_value = request.GET['sort']
        if sorting_value == 'created':
                if 'order' in request.GET and request.GET['order'] == 'asc':
                    sorting = JobDataModel.created.asc()
                else:
                    sorting = JobDataModel.created.desc()
        if sorting_value == 'title':
                if 'order' in request.GET and request.GET['order'] == 'asc':
                    sorting = JobDataModel.title.asc()
                else:
                    sorting = JobDataModel.title.desc()
        if sorting_value == 'command':
                if 'order' in request.GET and request.GET['order'] == 'asc':
                    sorting = JobDataModel.command.asc()
                else:
                    sorting = JobDataModel.command.desc()
        if sorting_value == 'status':
                if 'order' in request.GET and request.GET['order'] == 'asc':
                    sorting = JobDataModel.status.asc()
                else:
                    sorting = JobDataModel.status.desc()

    limit = int(request.GET['limit'])
    offset = int(request.GET['offset'])
    rows = None

    if 'search' in request.GET and len(request.GET['search'])>3:
        search_string = request.GET['search']
        rows = [t.to_dict() for t in JobDataModel.select().order_by(sorting).where(JobDataModel.title.contains(search_string)).offset(offset).limit(limit) ]
        count = JobDataModel.select().where(JobDataModel.title.contains(search_string)).count()
        return {"total": count, "rows": rows}

    else:
        rows = [t.to_dict() for t in JobDataModel.select().order_by(sorting).offset(offset).limit(limit) ]
        count = JobDataModel.select().count()
        return {"total": count, "rows": rows}
Example #3
0
def task_list(request):
    """
    Получить список заданий

    :param request:
    :return:
    """
    return [t.to_dict() for t in JobDataModel.select().order_by(JobDataModel.id.desc())]
Example #4
0
    def setup_job(self, job_id: int):
        # method that clean some garbage
        if self.current_job is not None:
            self.current_job.status = JobPeewee.STATUS_DONE
            self.current_job.save()

        self.current_job = JobPeewee.get(JobPeewee.id == job_id)
        self.current_job.status = JobPeewee.STATUS_RUNNING
        self.current_job.save()
Example #5
0
    def get_running_job():

        ret = Core.get_lock(JobDataModel.LOCK)
        if not ret:
            return None
        else:
            job_id = int(ret)
            try:
                job = JobDataModel.get(JobDataModel.id == job_id)
                return job
            except JobDataModel.DoesNotExist:
                return None
Example #6
0
    def get_running_job():

        ret = Core.get_lock(JobDataModel.LOCK)
        if not ret:
            return None
        else:
            job_id = int(ret)
            try:
                job = JobDataModel.get(JobDataModel.id == job_id)
                return job
            except JobDataModel.DoesNotExist:
                return None
Example #7
0
    def __init__(self, *agrs, **kwargs):
        self.jobs = kwargs.get("jobs", None)
        if self.jobs is None:
            jobs = Job.select().where(Job.task == self.id,
                                      Job.status == Job.STATUS_PENDING)
            self.jobs = []

            for i in jobs:
                self.jobs.append(i)

        self.data_model = kwargs["task_model"]
        self.id = kwargs["task_model"].id
        self.status = Job.STATUS_PENDING
        self.settings = self.data_model.settings
Example #8
0
    def is_job_alive(core, job_id):

        try:
            obj = JobDataModel.get(JobDataModel.id == job_id)
            # may be wrong
            logging.debug("find possible alive task %i pid is %i %s" % (job_id, obj.pid, obj.status))

            if obj.status == JobDataModel.STATUS_EXCEPTION:
                return False
            if obj.status == JobDataModel.STATUS_FAILED:
                return False
            if obj.status == JobDataModel.STATUS_DONE:
                return False

            pid = obj.pid

            return core.api.os.shell.is_alive(pid)
        except JobDataModel.DoesNotExist:
            return False
Example #9
0
    def is_job_alive(core, job_id):

        try:
            obj = JobDataModel.get(JobDataModel.id == job_id)
            # may be wrong
            logging.debug("find possible alive task %i pid is %i %s" %
                          (job_id, obj.pid, obj.status))

            if obj.status == JobDataModel.STATUS_EXCEPTION:
                return False
            if obj.status == JobDataModel.STATUS_FAILED:
                return False
            if obj.status == JobDataModel.STATUS_DONE:
                return False

            pid = obj.pid

            return core.api.os.shell.is_alive(pid)
        except JobDataModel.DoesNotExist:
            return False
Example #10
0
    def get_task(task_id: int):
        """
        get task from database by task_id

        :param task_id:
        :return: Task
        """

        global_task = JobDataModel.select().where(JobDataModel.task == task_id,
                                                  JobDataModel.status == JobDataModel.STATUS_PENDING)
        atom_jobs = []
        for i in global_task:
            atom_jobs.append(i)

        try:
            task_object = TaskDataModel.get(TaskDataModel.id == task_id)
            task = Task(task_model=task_object, jobs=atom_jobs)
            return task
        except TaskDataModel.DoesNotExist:
            return None
Example #11
0
    def get_task(task_id: int):
        """
        get task from database by task_id

        :param task_id:
        :return: Task
        """

        global_task = JobDataModel.select().where(
            JobDataModel.task == task_id,
            JobDataModel.status == JobDataModel.STATUS_PENDING)
        atom_jobs = []
        for i in global_task:
            atom_jobs.append(i)

        try:
            task_object = TaskDataModel.get(TaskDataModel.id == task_id)
            task = Task(task_model=task_object, jobs=atom_jobs)
            return task
        except TaskDataModel.DoesNotExist:
            return None