def check_tasks(self, queue_tasks_done): """Loads task results from runners and save to local queue. Manages also tasks in timeout. """ logger = self.logger idx_runners_to_terminate = [] for ii, runner_data in enumerate(self.execution_data): # check task execution if not runner_data['queue'].empty(): # pick the task executed = runner_data['queue'].get() # active_task task_name = runner_data['active_task'].task.classname uid = runner_data['active_task'].uid logger.info( f'Task {task_name} (UID {uid}) has terminated with result {executed.result.status}.' ) logger.info(f'Task {uid} says: {str(executed.result.data)}.') # update executed queue queue_tasks_done.put(executed) # prepare for cleanup idx_runners_to_terminate.append(ii) # check for task timeout else: total_seconds = (datetime.now() - runner_data['timestamp']).total_seconds() if (total_seconds > runner_data['active_task'].task.timeout): classname = runner_data['active_task'].task.classname uid = runner_data['active_task'].uid logger.info( f'Timeout exceeded, task {classname} (UID: {uid}) will be terminated.' ) # timeout gives error result response = Response() response.set_status(StatusCode.STATUS_ERROR) response.set_data({'result': 'Task went in timeout.'}) # update executed queue executed = runner_data['active_task'] executed.result = response queue_tasks_done.put(executed) # prepare for cleanup idx_runners_to_terminate.append(ii) # clean-up finished runners if idx_runners_to_terminate: self._cleanup_runners(idx_runners_to_terminate) return queue_tasks_done
def execute(self, request): config = self.config # get schedulation id args = request.data job_id = args['job_id'] follow_up = FollowUp('EXECUTE', job_id) response = Response() response.set_status(StatusCode.STATUS_OK) response.set_data({'msg': 'Job has been queued for execution.'}) return response, follow_up
def execute(self, request): """Performs a socket connection test.""" config = self.config response = Response() follow_up = FollowUp(None) try: response.set_status(StatusCode.STATUS_OK) except Exception: response.set_status(StatusCode.STATUS_ERROR) return response, follow_up
def execute(self, request): config = self.config response = Response() follow_up = FollowUp(None) data = {} try: # get schedulation id args = request.data job_id = args['job_id'] # get all schedules session = SqliteSession.create(config) job = session.query(Schedule).filter(Schedule.id == job_id).one() session.close() job_dict = { 'id': job.id, 'name': job.name, 'description': job.description, 'cronexp': job.cronexp, 'enabled': job.enabled, 'parameters': job.parameters, } data['Result'] = job_dict except Exception: response.set_status(StatusCode.STATUS_ERROR) else: response.set_status(StatusCode.STATUS_OK) finally: response.set_data(data) return response, follow_up
def execute(self, request): config = self.config response = Response() follow_up = FollowUp(None) data = {} try: # get all schedules session = SqliteSession.create(config) schedules = session.query(Schedule).group_by(Schedule.name).all() session.close() job_list = [] for s in schedules: d = {} d['id'] = s.id d['name'] = s.name d['description'] = s.description d['cronexp'] = s.cronexp d['enabled'] = s.enabled job_list.append(d) data['Result'] = job_list except Exception: response.set_status(StatusCode.STATUS_ERROR) else: response.set_status(StatusCode.STATUS_OK) finally: response.set_data(data) return response, follow_up
def execute(self, request): config = self.config response = Response() follow_up = FollowUp('RELOAD') # create db session session = SqliteSession.create(config) try: # get schedulation data from the user job_data = request.data # create and add job object job = Schedule(name=job_data['name'], description=job_data['description'], cronexp=job_data['cronexp'], parameters=job_data['parameters'], enabled=job_data['enabled']) # save session.add(job) session.commit() except Exception as err: response.set_status(StatusCode.STATUS_ERROR) msg = f'Impossible to add the job: {str(err)}.' response.set_data({'msg': msg}) else: response.set_status(StatusCode.STATUS_OK) response.set_data({'msg': 'Job has been added.'}) finally: session.close() return response, follow_up
def work(self): config = self.config logger = self.logger # get shared queues queue_w2m = self.queue_w2m # get the task to exec active_task = self.active_task # load class to exec classname = active_task.task.classname filepath = active_task.task.filepath timeout = active_task.task.timeout args = active_task.task.args try: # loading task class RunningTask = load_class(filepath, classname) # task execution running_task = RunningTask(config, logger) response = running_task.run(args) except Exception as err: response = Response() response.set_status(StatusCode.STATUS_ERROR) response.set_data({'result': str(err)}) finally: # queue_done active_task.result = response queue_w2m.put(active_task)
def execute(self, request): config = self.config # get schedulation id args = request.data job_id = args['job_id'] response = Response() follow_up = FollowUp('RELOAD') # create db session session = SqliteSession.create(config) try: # enable sched = session.query(Schedule).filter(Schedule.id == job_id).one() sched.enabled = True # save session.add(sched) session.commit() except Exception: response.set_status(StatusCode.STATUS_ERROR) response.set_data({'msg': 'Impossible to enable the job.'}) else: response.set_status(StatusCode.STATUS_OK) response.set_data({'msg': 'Job has been enabled.'}) finally: session.close() return response, follow_up