def dump_json(output_dict, file=sys.stdout): print(jsonDumps(output_dict, indent=4), file=file)
def dumps(*args, **kwds): return jsonDumps(*args, **kwds)
def json(self, isOutput=False, **kwargs): return jsonDumps(self.getFields(isOutput), **kwargs)
def ajaxMsg(msg, status): return jsonDumps({'message': msg}), status, { 'ContentType': 'application/json' }
def registerEPs(self, ce_proxy=None): """ Register EPs to Central Engines """ if ce_proxy: print('Starting Client Service register on `{}`...'.format(ce_proxy)) else: print('Starting Client Service register...') # List of Central Engine connections proxyEpsList = {} for currentEP in self.eps: _proxy = '{}:{}'.format(self.eps[currentEP]['ce_ip'], self.eps[currentEP]['ce_port']) # If Central Engine proxy filter is specified, use it if ce_proxy and ce_proxy != _proxy: continue if not proxyEpsList.has_key(_proxy): proxyEpsList[_proxy] = [ ep for ep in self.eps if self.eps[ep]['ce_ip'] == self.eps[currentEP]['ce_ip'] and self.eps[ep]['ce_port'] == self.eps[currentEP]['ce_port'] ] unregistered = True # Try to register to Central Engine, forever while unregistered: for currentCE in proxyEpsList: try: proxy = self.eps[proxyEpsList[currentCE][0]]['proxy'] __proxy = proxy._ServerProxy__host.split('@')[1].split(':') except Exception, e: print('CE proxy error: `{}` on `{}`.'.format(e, __proxy)) continue clientKey = ':{port}'.format(port=self.clientPort) try: userCeClientInfo = proxy.getUserVariable(self.username, 'clients') if not userCeClientInfo: userCeClientInfo = {} else: userCeClientInfo = jsonLoads(userCeClientInfo) while True: ceStatus = proxy.getExecStatusAll(self.username) if ceStatus.startswith('invalid'): break elif ceStatus.startswith('stopped'): # Reset user project proxy.resetProject(self.username) print('User project reset.') break else: print('CE on `{}` is running with status `{}`.'.format( proxy._ServerProxy__host.split('@')[1], ceStatus)) print('Waiting to stop ...') sleep(2) for (prxy, eps) in userCeClientInfo.items(): for ep in eps: if ep in proxyEpsList[currentCE]: print('Warning: epname {} already registered. Trying to stop..'.format(ep)) try: p = xmlrpclib.ServerProxy('http://{}:{}/twisterclient/'.format( prxy.split(':')[0], prxy.split(':')[1])) try: last_seen_alive = self.eps[ep]['proxy'].getEpVariable( self.username, ep, 'last_seen_alive') now_dtime = datetime.today() if last_seen_alive: diff = now_dtime - datetime.strptime(last_seen_alive, '%Y-%m-%d %H:%M:%S') if diff.seconds <= 2.4: proxyEpsList[currentCE].pop(proxyEpsList[currentCE].index(ep)) print('Warning: epname {} is running. Will not register.'.format(ep)) else: p.stopEP(ep) userCeClientInfo[prxy].pop(userCeClientInfo[prxy].index(ep)) if not userCeClientInfo[prxy]: userCeClientInfo.pop(prxy) print('Warning: epname {} stoped. Will register.'.format(ep)) except Exception as e: pass except Exception as e: pass if not proxyEpsList[currentCE]: continue userCeClientInfo.update([(clientKey, proxyEpsList[currentCE]), ]) userCeClientInfo = jsonDumps(userCeClientInfo) proxy.registerClient(self.username, userCeClientInfo) unregistered = False except Exception as e: self.proxyList.pop(currentCE) print('Error: {er}'.format(er=e)) if unregistered: print('Error: Central Engine is down... will retry...') sleep(2)
def registerEPs(self, ce_proxy=None): """ Register EPs to Central Engines """ if ce_proxy: print( 'Starting Client Service register on `{}`...'.format(ce_proxy)) else: print('Starting Client Service register...') # List of Central Engine connections proxyEpsList = {} for currentEP in self.eps: _proxy = '{}:{}'.format(self.eps[currentEP]['ce_ip'], self.eps[currentEP]['ce_port']) # If Central Engine proxy filter is specified, use it if ce_proxy and ce_proxy != _proxy: continue if not proxyEpsList.has_key(_proxy): proxyEpsList[_proxy] = [ ep for ep in self.eps if self.eps[ep]['ce_ip'] == self.eps[currentEP]['ce_ip'] and self.eps[ep]['ce_port'] == self.eps[currentEP]['ce_port'] ] unregistered = True # Try to register to Central Engine, forever while unregistered: for currentCE in proxyEpsList: try: proxy = self.eps[proxyEpsList[currentCE][0]]['proxy'] __proxy = proxy._ServerProxy__host.split('@')[1].split(':') create_connection((__proxy[0], __proxy[1]), 2) except Exception, e: print('CE proxy error: `{}` on `{}`.'.format(e, __proxy)) continue clientKey = ':{port}'.format(port=self.clientPort) try: userCeClientInfo = proxy.getUserVariable( self.username, 'clients') if not userCeClientInfo: userCeClientInfo = {} else: userCeClientInfo = jsonLoads(userCeClientInfo) while True: ceStatus = proxy.getExecStatusAll(self.username) if ceStatus.startswith('invalid'): break elif ceStatus.startswith('stopped'): # Reset user project proxy.resetProject(self.username) print('User project reset.') break else: print('CE on `{}` is running with status `{}`.'. format( proxy._ServerProxy__host.split('@')[1], ceStatus)) print('Waiting to stop ...') sleep(2) for (prxy, eps) in userCeClientInfo.items(): for ep in eps: if ep in proxyEpsList[currentCE]: print( 'Warning: epname {} already registered. Trying to stop..' .format(ep)) try: p = xmlrpclib.ServerProxy( 'http://{}:{}/twisterclient/'.format( prxy.split(':')[0], prxy.split(':')[1])) try: last_seen_alive = self.eps[ep][ 'proxy'].getEpVariable( self.username, ep, 'last_seen_alive') now_dtime = datetime.today() if last_seen_alive: diff = now_dtime - datetime.strptime( last_seen_alive, '%Y-%m-%d %H:%M:%S') if diff.seconds <= 2.4: proxyEpsList[currentCE].pop( proxyEpsList[currentCE]. index(ep)) print( 'Warning: epname {} is running. Will not register.' .format(ep)) else: p.stopEP(ep) userCeClientInfo[prxy].pop( userCeClientInfo[prxy].index( ep)) if not userCeClientInfo[prxy]: userCeClientInfo.pop(prxy) print( 'Warning: epname {} stoped. Will register.' .format(ep)) except Exception as e: pass except Exception as e: pass if not proxyEpsList[currentCE]: continue userCeClientInfo.update([ (clientKey, proxyEpsList[currentCE]), ]) userCeClientInfo = jsonDumps(userCeClientInfo) proxy.registerClient(self.username, userCeClientInfo) unregistered = False except Exception as e: self.proxyList.pop(currentCE) print('Error: {er}'.format(er=e)) if unregistered: print('Error: Central Engine is down... will retry...') sleep(2)