def process_run( self, request, json_str ): """ ! @brief process 'addjobs' command @param json_str (json) json representation of a dictinary dictinary has the following format: { <HOST.id>: { 'host_id': <int>}, 'host_full_name': <string>, 'jobs': [<JOB.id>, ...], ... } """ jobs = json.loads( json_str ) failed = self._send_jobs( jobs ) if failed: # there are still some jobs which could not be executed. # send them back failed_jobs = [] [ failed_jobs.append( jobs[ h ]['jobs'] ) for h in failed ] cmd = "failedjobs:{j}".format(j=json.dumps( failed ) ) clientSock = hQSocket( catchErrors = False ) clientSock.initSocket( HQ_SERVER_HOST, HQ_SERVER_PORT ) clientSock.send( cmd )
def shutdown( self ): """! brief shutdown server """ command = "shutdown" self.clientSock = hQSocket( host=self.host, port=self.port, catchErrors=False ) self.clientSock.send(command) self.clientSock = hQSocket( host=self.host, port=self.port, catchErrors=False ) self.clientSock.send("ping") return "done"
def handle(self): """request handler""" if self.srv.shutdown_server_event.is_set(): # do not process event while server ist shutting down return # since after a predefined number of request the status is printed on console # number of handled requests is counted here self.srv.print_status_counter += 1 # create a hQSocket-instance requestSocket = hQSocket( sock = self.request, serverSideSSLConn = True, catchErrors = False, timeout = 10 ) try: receivedStr = requestSocket.recv() except socket.timeout: self.writeLog( "Timeout while reading from socket {h}:{p}. Skip".format( h = self.requestHost, p = self.requestPort), logCategory='warning' ) return requestStrShort = "{r1}{dots}".format( r1=receivedStr[:30] if len(receivedStr)>30 else receivedStr, dots="..." if len(receivedStr)>30 else "" ) # add new attribute to Thread object self.currThread.command = receivedStr self.currThread.command_short = requestStrShort self.writeLog( "NEW REQUEST FROM {h}:{p}: {s}".format( s = requestStrShort, h = self.requestHost, p = self.requestPort ), logCategory='request_processing' ) t1 = datetime.now() # process request try: self.srv.processor.process(receivedStr, requestSocket, self.writeLog, self.srv) except: # processing failed tb = sys.exc_info() self.writeLog('Error while processing request from {h}:{p}!\n'.format(h=self.requestHost, p=self.requestPort), logCategory='error' ) traceback.print_exception(*tb,file=sys.stderr) requestSocket.send("Error while processing request!\n%s" % tb[1]) t2 = datetime.now() self.writeLog( "REQUEST PROCESSED IN {dt}s.".format(dt=str(t2-t1) ), logCategory='request_processing')
def connect( self ): """! @brief just create new socket """ if not self.clientSock: try: self.clientSock = hQSocket( host=self.host, port=self.port, catchErrors=False ) except: pass return self
def process_addjobs( self, request, json_str ): """ ! @brief process 'addjobs' command @parama json_str (json) json representation of a list of dictinaries format of each dictinary { 'command': <string>, 'estimatedMemory': <float>, 'estimatedTime': <float>, 'excludedHosts': <comma separated list of host names>, 'group': <string>, 'infoText': <string>, 'logfile': <string>, 'priority': <int>, 'shell': <bash|csh>, 'slots': <int>, 'stderr': <string>, 'stdout': <string> } """ jobs = json.loads( json_str ) # register job at TaskDispatcher jsonOutObj = { 'user_id': self.server.user_id, 'host': self.server.host, 'port': self.server.port, 'id': self.server.server_id, 'jobs': jobs } #self.server.logger.info('[%s] ... submit jobs to cluster' % threadName) jsonOutObj = json.dumps(jsonOutObj) com = "add:%s" % jsonOutObj try: # instantiate new socket clientSock = hQSocket( catchErrors = False ) clientSock.initSocket( HQ_SERVER_HOST, HQ_SERVER_PORT ) clientSock.send( com ) jobID = clientSock.recv() if jobID=="What do you want?": response = "Could not submit job to hq-server." else: response = "Job {ids} has been submitted to the cluster.\nSo long, and thanks for all the fish.".format(ids=jobID) request.send( response ) except: traceback.print_exc(file=sys.stderr) request.send("Could not connect to hq-server.")
def send(self, command): logger.info( "send request: {c}".format(c=command) ) try: self.clientSock.send(command) except: self.clientSock = hQSocket( host=self.host, port=self.port, catchErrors=False ) self.clientSock.send(command) logger.info( "... done" ) return self
def register_server( self ): """! @brief register server at hq-server if hq-server does not approve it, this server exits """ jsonObj = json.dumps( { 'user': USER, 'host': self.host, 'port': self.port, 'id': self.server_id } ) cmd = 'register:{j}'.format(j = jsonObj ) clientSock = hQSocket( catchErrors = False ) clientSock.initSocket( HQ_SERVER_HOST, HQ_SERVER_PORT ) clientSock.send( cmd ) response = clientSock.recv() response = json.loads( response ) if response['status']=='approved': self.user_id = response['user_id'] return True else: return False
) args = parser.parse_args() # here the the certificates should be read keyfile = None certfile = None ca_certs = None # set server host and port to which we try to connect host = args.serverSettings.host port = args.serverSettings.port try: # create socket client = hQSocket( catchErrors = False ) client.initSocket( host, port ) logger.info( "Connection to {host}:{port}".format( host=host, port=port ) ) command = ' '.join( [args.command] + args.commandArgs ) client.send(command) logger.info( "Command: {com}".format(com=command )) receivedStr = client.recv() logger.info( "Received string:") sys.stdout.write(receivedStr)