def test_ResponseError(self): r = Response.Error() r_d = r.toDict() r_json = r.toJSON() logging.info("serialized response error: %s" % (r_json)) r_dict = json.loads(r_json) self.assertTrue(len(r_dict) == 1) self.assertTrue(r_dict['code'] == ResponseCode.ERROR) r = Response.Error('internal error') r_json = r.toJSON() logging.info("serialized response error: %s" % (r_json)) r_dict = json.loads(r_json) self.assertTrue(len(r_dict) == 2) self.assertTrue(r_dict['code'] == ResponseCode.ERROR) self.assertTrue(r_dict['message'] == 'internal error') r = Response.Error('job can\'t be canceled', {'jobId': 101}) r_json = r.toJSON() logging.info("serialized response error: %s" % (r_json)) r_dict = json.loads(r_json) self.assertTrue(r_dict['code'] == ResponseCode.ERROR) self.assertTrue(r_dict['message'] == 'job can\'t be canceled') self.assertTrue(r_dict.get('data') is not None) self.assertTrue(r_dict['data'].get('jobId') is not None) self.assertTrue(r_dict['data']['jobId'] == 101)
async def __handleListJobsReq(self, iface, request): logging.info("Handling list jobs info from %s iface" % (iface.__class__.__name__)) jobNames = self.__manager.jobList.jobs() logging.info("got %s jobs from list" % (str(len(jobNames)))) jobs = {} for jobName in jobNames: job = self.__manager.jobList.get(jobName) if job is None: return Response.Error( 'One of the job %s doesn\'t exist in registry' % (jobName)) jobData = {'status': str(job.strState())} if job.messages is not None: jobData['messages'] = job.messages if job.getQueuePos() is not None: jobData['inQueue'] = job.getQueuePos() jobs[jobName] = jobData return Response.Ok(data={ 'length': len(jobNames), 'jobs': jobs, })
async def __handleJobStatusReq(self, iface, request): logging.info("Handling job status request from %s iface" % (iface.__class__.__name__)) result = {} for jobName in request.jobNames: try: job = self.__manager.jobList.get(jobName) if job is None: return Response.Error('Job %s doesn\'t exist' % (request.jobName)) result[jobName] = { 'status': int(ResponseCode.OK), 'data': { 'jobName': jobName, 'status': str(job.strState()) } } except Exception as e: result[jobName] = { 'status': int(ResponseCode.ERROR), 'message': e.args[0] } return Response.Ok(data={'jobs': result})
async def __handleCancelJobReq(self, iface, request): logging.info("Handling cancel job from %s iface" % (iface.__class__.__name__)) job = self.__manager.jobList.get(request.jobName) if job is None: return Response.Error('Job %s doesn\'t exist' % (request.jobName)) return Response.Error('Cancel job is not supported')
async def __handleControlReq(self, iface, request): logging.info("Handling control request from %s iface" % (iface.__class__.__name__)) if request.command == ControlReq.REQ_CONTROL_CMD_FINISHAFTERALLTASKSDONE: if self.__finishTask is not None: return Response.Error('Finish request already requested') self.__finishTask = asyncio.ensure_future(self.__waitForAllJobs()) return Response.Ok('%s command accepted' % (request.command))
async def __handleRequest(self, iface, request): if request.__class__ not in self.__handlers: logging.error( "Failed to handle request: unknown request class '%s'" % (request.__class__.__name__)) return Response.Error('Unknown request type') else: try: return await self.__handlers[request.__class__](iface, request) except: logging.exception('Failed to process request') return Response.Error('Failed to process request')
async def __handleFinishReq(self, iface, request): logging.info("Handling finish service from %s iface" % (iface.__class__.__name__)) delay = 2 resources = self.__manager.resources if self.__finishTask is not None: return Response.Error('Finish request already requested') self.__finishTask = asyncio.ensure_future(self.__delayedFinish(delay)) return Response.Ok(data={ 'when': '%ds' % delay, })
async def __handleRemoveJobReq(self, iface, request): logging.info("Handling remove jobs from %s iface" % (iface.__class__.__name__)) removed = 0 errors = {} for jobName in request.jobNames: try: job = self.__manager.jobList.get(jobName) if job is None: raise InvalidRequest('Job %s doesn\'t exist' % (jobName)) if not job.state.isFinished(): raise InvalidRequest( 'Job %s not finished - can not be removed' % (jobName)) self.__manager.jobList.remove(jobName) removed += 1 except Exception as e: errors[jobName] = e.args[0] data = { 'removed': removed, } if len(errors) > 0: data['errors'] = errors return Response.Ok(data=data)
async def __handleSubmitReq(self, iface, request): logging.info("Handling submit request from %s iface" % (iface.__class__.__name__)) for job in request.jobs: # verify job name uniqness if self.__manager.jobList.exist(job.name): return Response.Error('Job %s already exist' % (job.name)) # enqueue job in the manager self.__manager.enqueue(request.jobs) jobs = len(request.jobs) data = {'submitted': jobs, 'jobs': [job.name for job in request.jobs]} return Response.Ok('%d jobs submitted' % (len(request.jobs)), data=data)
def test_ResponseOk(self): r = Response.Ok() r_d = r.toDict() r_json = r.toJSON() logging.info("serialized response ok: %s" % (r_json)) r_dict = json.loads(r_json) self.assertTrue(len(r_dict) == 1) self.assertTrue(r_dict['code'] == ResponseCode.OK) r = Response.Ok('job canceled') r_json = r.toJSON() logging.info("serialized response ok: %s" % (r_json)) r_dict = json.loads(r_json) self.assertTrue(len(r_dict) == 2) self.assertTrue(r_dict['code'] == ResponseCode.OK) self.assertTrue(r_dict['message'] == 'job canceled')
async def __handleJobInfoReq(self, iface, request): logging.info("Handling job info request from %s iface" % (iface.__class__.__name__)) result = {} for jobName in request.jobNames: try: job = self.__manager.jobList.get(jobName) if job is None: return Response.Error('Job %s doesn\'t exist' % (request.jobName)) jobData = {'jobName': jobName, 'status': str(job.strState())} if job.messages is not None: jobData['messages'] = job.messages if job.runtime is not None and len(job.runtime) > 0: jobData['runtime'] = job.runtime if job.history is not None and len(job.history) > 0: history_str = '' for entry in job.history: history_str = '\n'.join([ history_str, "%s: %s" % (str(entry[1]), entry[0].name) ]) jobData['history'] = history_str result[jobName] = { 'status': int(ResponseCode.OK), 'data': jobData } except Exception as e: result[jobName] = { 'status': int(ResponseCode.ERROR), 'message': e.args[0] } return Response.Ok(data={'jobs': result})
def test_ResponseData(self): r = Response.Ok(data={'jobId': 101}) r_json = r.toJSON() logging.info("serialized response with data: %s" % (r_json)) r_dict = json.loads(r_json) self.assertTrue(r_dict['code'] == ResponseCode.OK) self.assertTrue(r_dict.get('message') is None) self.assertTrue(r_dict.get('data') is not None) self.assertTrue(r_dict['data'].get('jobId') is not None) self.assertTrue(r_dict['data']['jobId'] == 101)
async def __handleResourcesInfoReq(self, iface, request): logging.info("Handling resources info from %s iface" % (iface.__class__.__name__)) resources = self.__manager.resources return Response.Ok( data={ 'totalNodes': len(resources.nodes), 'totalCores': resources.totalCores, 'usedCores': resources.usedCores, 'freeCores': resources.freeCores })
async def __listen(self, iface): logging.info("Listener on interface %s started" % (iface.__class__.__name__)) while True: try: reqData = await iface.receive() if reqData is None: # finishing listening - nothing more will come logging.info( "Finishing listening on interface %s due to EOD" % (iface.__class__.__name__)) return logging.info("Interface %s received request: %s" % ((iface.__class__.__name__), str(reqData))) # validate request validResp = self.__validate(reqData) logging.info( "validate result: result(%s), errorMessage(%s), request(%s)" % (str(validResp.result), str( validResp.errorMessage), str(validResp.request))) logging.info("validate error ? %s" % (str(validResp.isError()))) if validResp.isError(): response = Response.Error(validResp.errorMessage) else: response = await self.__handleRequest( iface, validResp.request) logging.info("got response: %s" % (str(response.toDict()))) await iface.reply(response.toJSON()) except CancelledError: # listener was canceled - finished gracefully logging.info( "Finishing listening on interface %s due to interrupt" % (iface.__class__.__name__)) return except Exception as e: logging.exception( "Failed to process request from interface %s" % (iface.__class__.__name__))