def query_task(cls, only_latest=True, reverse=None, order_by=None, **kwargs): filters = [] for f_n, f_v in kwargs.items(): attr_name = 'f_%s' % f_n if hasattr(Task, attr_name): filters.append(operator.attrgetter('f_%s' % f_n)(Task) == f_v) if filters: tasks = Task.select().where(*filters) else: tasks = Task.select() if reverse is not None: if not order_by or not hasattr(Task, f"f_{order_by}"): order_by = "create_time" if reverse is True: tasks = tasks.order_by(getattr(Task, f"f_{order_by}").desc()) elif reverse is False: tasks = tasks.order_by(getattr(Task, f"f_{order_by}").asc()) if only_latest: tasks_group = cls.get_latest_tasks(tasks=tasks) return list(tasks_group.values()) else: return [task for task in tasks]
def query_task(**kwargs): with DB.connection_context(): filters = [] for f_n, f_v in kwargs.items(): attr_name = 'f_%s' % f_n if hasattr(Task, attr_name): filters.append(operator.attrgetter('f_%s' % f_n)(Task) == f_v) if filters: tasks = Task.select().where(*filters) else: tasks = Task.select() return [task for task in tasks]
def check_task(cls, job_id, role, party_id, components: list): filters = [ Task.f_job_id == job_id, Task.f_role == role, Task.f_party_id == party_id, Task.f_component_name << components ] tasks = Task.select().where(*filters) if tasks and len(tasks) == len(components): return True else: return False
def save_task(self, role, party_id, task_info): with DB.connection_context(): tasks = Task.select().where( Task.f_job_id == self.job_id, Task.f_component_name == self.component_name, Task.f_task_id == self.task_id, Task.f_role == role, Task.f_party_id == party_id) is_insert = True if tasks: task = tasks[0] is_insert = False else: task = Task() task.f_create_time = current_timestamp() task.f_job_id = self.job_id task.f_component_name = self.component_name task.f_task_id = self.task_id task.f_role = role task.f_party_id = party_id if 'f_status' in task_info: if task.f_status in [TaskStatus.COMPLETE, TaskStatus.FAILED]: # Termination status cannot be updated # TODO: pass for k, v in task_info.items(): try: if k in [ 'f_job_id', 'f_component_name', 'f_task_id', 'f_role', 'f_party_id' ] or v == getattr(Task, k).default: continue except: pass setattr(task, k, v) if is_insert: task.save(force_insert=True) else: task.save() return task
def get_tasks_asc(cls, job_id, role, party_id): tasks = Task.select().where( Task.f_job_id == job_id, Task.f_role == role, Task.f_party_id == party_id).order_by(Task.f_create_time.asc()) tasks_group = cls.get_latest_tasks(tasks=tasks) return tasks_group
def list_task(limit): if limit > 0: tasks = Task.select().order_by(Task.f_create_time.desc()).limit(limit) else: tasks = Task.select().order_by(Task.f_create_time.desc()) return [task for task in tasks]