Пример #1
0
 def __init__(self, config: WorkerConfig) -> None:
     self.config = config
     self.executors = Executors(config.plugins.execution_data_root, config.plugins.executors_dir)
     self.resources = Resources(config.plugins.resources_dir)
     self.backend = WorkerBackends(config.plugins.backends_dir).construct_backend(config.backend,
                                                                                  config.backend_config)
     self.engine = Engine(self.backend, self.resources, self.executors)
Пример #2
0
class WorkerApp:
    def __init__(self, config: WorkerConfig) -> None:
        self.config = config
        self.executors = Executors(config.plugins.execution_data_root, config.plugins.executors_dir)
        self.resources = Resources(config.plugins.resources_dir)
        self.backend = WorkerBackends(config.plugins.backends_dir).construct_backend(config.backend,
                                                                                     config.backend_config)
        self.engine = Engine(self.backend, self.resources, self.executors)

    @staticmethod
    def ping(*args):
        return ResultOk('pong')

    def create_task(self, args: dict, request: Request):
        task_id = uuid4().hex
        self.engine.create_idle_task(task_id, task_struct=args)
        return ResultOk({'task_id': task_id})

    def get_task_info(self, args: dict, request: Request):
        task_id = request.match_info.get('task_id', None)
        if not task_id:
            return ResultError(error='task_id field should be set')
        return ResultOk(self.backend.read_task_info(task_id=task_id).to_json())

    def get_task_state(self, args: dict, request: Request):
        task_id = request.match_info.get('task_id', None)
        if not task_id:
            return ResultError(error='task_id field should be set')
        task_info = self.backend.read_task_info(task_id=task_id)
        return ResultOk({'state': task_info.exec_stats.state.name})

    def start_task(self, args: dict, request: Request):
        task_id = request.match_info.get('task_id', None)
        if not task_id:
            return ResultError(error='task_id field should be set')
        return self._set_task_state(task_id, TaskState.preparing)

    def stop_task(self, args: dict, request: Request):
        task_id = request.match_info.get('task_id', None)
        if not task_id:
            return ResultError(error='task_id field should be set')
        return self._set_task_state(task_id, TaskState.stopped)

    def _set_task_state(self, task_id: str, task_state_name: str):
        prev_state = self.engine.set_task_state(task_id, task_state_name)
        return ResultOk(prev_state=prev_state, new_state=task_state_name)

    def list_tasks(self, args: dict, request: Request):
        with_info = (args.get('with_info', '1') == '1')
        limit = int(args.get('limit', '-1'))
        offset = int(args.get('offset', '0'))
        it = islice(self.backend.list_tasks(with_info=with_info), offset, offset + limit if limit >= 0 else None)
        return ResultOk(
            [
                task_info.to_json() if task_info else {'task_id': task_id}
                for task_id, task_info in it
            ]
        )

    def task_log(self, args: dict, request: Request):
        task_id = request.match_info.get('task_id', None)
        log_type = request.match_info.get('log_type', None)
        if not task_id:
            return ResultError(error='task_id field should be set')
        if log_type not in ('err', 'out'):
            return ResultError(error='Log type should be one of (err, out)')
        # TODO: extract calculation of execution data root
        self.backend.read_task_info(task_id)  # just verify that this task exists
        log_path = os.path.join(self.config.plugins.execution_data_root, task_id, 'std{}.log'.format(log_type))
        try:
            with open(log_path, 'r') as f:
                return ResultOk(log_type=log_type, data=f.read())
        except:
            return ResultNotFound()