async def get_tasks(self, limit=100, offset=0, where=None, and_where=None, or_where=None, sort=None) -> List[Task]: """Retrieves tasks The result set is ordered by start_time descending Args: offset: Ignore this number of rows at the beginning of the result set. Results are unpredictable unless order_by is used. limit: Return at most this number of rows where: A query sort: A tuple of Task attributes to sort by. Defaults to ("start_time", "desc") """ chain_payload = PayloadBuilder().LIMIT(limit).chain_payload() if offset: chain_payload = PayloadBuilder(chain_payload).OFFSET( offset).chain_payload() if where: chain_payload = PayloadBuilder(chain_payload).WHERE( where).chain_payload() if and_where: chain_payload = PayloadBuilder(chain_payload).AND_WHERE( and_where).chain_payload() if or_where: chain_payload = PayloadBuilder(chain_payload).OR_WHERE( or_where).chain_payload() if sort: chain_payload = PayloadBuilder(chain_payload).ORDER_BY( sort).chain_payload() query_payload = PayloadBuilder(chain_payload).payload() tasks = [] try: self._logger.debug('Database command: %s', query_payload) res = self._storage.query_tbl_with_payload("tasks", query_payload) for row in res['rows']: task = Task() task.task_id = row.get('id') task.state = Task.State(int(row.get('state'))) task.start_time = row.get('start_time') task.process_name = row.get('process_name') task.end_time = row.get('end_time') task.exit_code = row.get('exit_code') task.reason = row.get('reason') tasks.append(task) except Exception: self._logger.exception('Query failed: %s', query_payload) raise return tasks
async def mock_coro(): task = Task() task.task_id = self._random_uuid task.state = Task.State.RUNNING task.start_time = None task.schedule_name = "bar" task.process_name = "bar" task.end_time = None task.exit_code = 0 task.reason = None return task
async def patch_get_tasks(): tasks = [] task = Task() task.task_id = self._random_uuid task.state = Task.State.RUNNING task.start_time = None task.schedule_name = "bla" task.process_name = "bla" task.end_time = None task.exit_code = 0 task.reason = None tasks.append(task) return tasks
async def get_task(request): """ Returns: a task list :Example: curl -X GET http://localhost:8081/foglamp/task/{task_id} """ try: task_id = request.match_info.get('task_id', None) try: assert uuid.UUID(task_id) except ValueError as ex: raise web.HTTPNotFound(reason="Invalid Task ID {}".format(task_id)) tsk = await server.Server.scheduler.get_task(task_id) task = { 'id': str(tsk.task_id), 'name': tsk.process_name, 'state': Task.State(int(tsk.state)).name.capitalize(), 'startTime': str(tsk.start_time), 'endTime': str(tsk.end_time), 'exitCode': tsk.exit_code, 'reason': tsk.reason } return web.json_response(task) except (ValueError, TaskNotFoundError) as ex: raise web.HTTPNotFound(reason=str(ex))
async def get_running_tasks(self) -> List[Task]: """Retrieves a list of all tasks that are currently running Returns: An empty list if no tasks are running A list of Task objects """ if not self._ready: raise NotReadyError() tasks = [] for (task_id, task_process) in self._task_processes.items(): task = Task() task.task_id = task_id task.process_name = task_process.schedule.process_name task.state = Task.State.RUNNING if task_process.cancel_requested is not None: task.cancel_requested = (datetime.datetime.fromtimestamp( task_process.cancel_requested)) task.start_time = datetime.datetime.fromtimestamp( task_process.start_time) tasks.append(task) return tasks
async def get_task(self, task_id: uuid.UUID) -> Task: """Retrieves a task given its id""" query_payload = PayloadBuilder().WHERE(["id", "=", task_id]).payload() try: self._logger.debug('Database command: %s', query_payload) res = self._storage.query_tbl_with_payload("tasks", query_payload) for row in res['rows']: task = Task() task.task_id = row.get('id') task.state = Task.State(int(row.get('state'))) task.start_time = row.get('start_time') task.process_name = row.get('process_name') task.end_time = row.get('end_time') task.exit_code = row.get('exit_code') task.reason = row.get('reason') return task except Exception: self._logger.exception('Query failed: %s', query_payload) raise raise TaskNotFoundError(task_id)
async def get_tasks(request): """ Returns: the list of tasks :Example: curl -X GET http://localhost:8081/foglamp/task curl -X GET http://localhost:8081/foglamp/task?limit=2 curl -X GET http://localhost:8081/foglamp/task?name=xxx curl -X GET http://localhost:8081/foglamp/task?state=xxx curl -X GET http://localhost:8081/foglamp/task?name=xxx&state=xxx """ try: limit = __DEFAULT_LIMIT if 'limit' in request.query and request.query['limit'] != '': try: limit = int(request.query['limit']) if limit < 0: raise ValueError except ValueError: raise web.HTTPBadRequest( reason="Limit must be a positive integer") name = None if 'name' in request.query and request.query['name'] != '': name = request.query['name'] state = None if 'state' in request.query and request.query['state'] != '': try: state = Task.State[request.query['state'].upper()].value except KeyError as ex: raise web.HTTPBadRequest( reason="This state value {} not permitted.".format(ex)) where_clause = None if name and state: where_clause = (["process_name", "=", name], ["state", "=", state]) elif name: where_clause = ["process_name", "=", name] elif state: where_clause = ["state", "=", state] tasks = await server.Server.scheduler.get_tasks(where=where_clause, limit=limit) if len(tasks) == 0: raise web.HTTPNotFound(reason="No Tasks found") new_tasks = [] for task in tasks: new_tasks.append({ 'id': str(task.task_id), 'name': task.process_name, 'state': Task.State(int(task.state)).name.capitalize(), 'startTime': str(task.start_time), 'endTime': str(task.end_time), 'exitCode': task.exit_code, 'reason': task.reason }) return web.json_response({'tasks': new_tasks}) except (ValueError, TaskNotFoundError) as ex: raise web.HTTPNotFound(reason=str(ex))
async def get_tasks(request): """ Returns the list of tasks :Example: curl -X GET http://localhost:8082/foglamp/task :Example: curl -X GET http://localhost:8082/foglamp/task?name=xxx :Example: curl -X GET http://localhost:8082/foglamp/task?state=xxx :Example: curl -X GET http://localhost:8082/foglamp/task?name=xxx&state=xxx """ try: limit = request.query.get( 'limit') if 'limit' in request.query else '100' if limit: if not re.match("(^[0-9]*$)", limit): raise web.HTTPBadRequest( reason='This limit {} not permitted.'.format(limit)) elif int(limit) > 100: limit = 100 else: limit = int(limit) state = request.query.get( 'state') if 'state' in request.query else None if state: if state.upper() not in [t.name for t in list(Task.State)]: raise web.HTTPBadRequest( reason='This state value {} not permitted.'.format(state)) else: z = dict() for i in list(Task.State): z.update({i.name: i.value}) state = z[state.upper()] name = request.query.get('name') if 'name' in request.query else None where_clause = None if name and state: where_clause = (["process_name", "=", name], ["state", "=", state]) elif name: where_clause = ["process_name", "=", name] elif state: where_clause = ["state", "=", state] tasks = await server.Server.scheduler.get_tasks(where=where_clause, limit=limit) if len(tasks) == 0: raise TaskNotFoundError("No Tasks") new_tasks = [] for task in tasks: new_tasks.append({ 'id': str(task.task_id), 'process_name': task.process_name, 'state': Task.State(int(task.state)).name, 'start_time': str(task.start_time), 'end_time': str(task.end_time), 'exit_code': task.exit_code, 'reason': task.reason }) return web.json_response({'tasks': new_tasks}) except (ValueError, TaskNotFoundError) as ex: raise web.HTTPNotFound(reason=str(ex))