def retrieve(client_id, client_tag='0', service_call=None): global db_config logging.debug('retrieving logged service call ' + str(client_id) + ',' + str(client_tag) + ',' + str(service_call)) try: if service_call == None: sqlStr=Dialect.buildQuery("ProdCom.Query2",{'client_id':client_id,\ 'client_tag':client_tag}) Session.execute(sqlStr) rows = Session.fetchall() if len(rows) != 1: raise ProdException( "No entries found for client ID: " + str(client_id) + " and tag " + str(client_tag), 1002) service_results = cPickle.loads(base64.decodestring(rows[0][0])) service_parameters = cPickle.loads(base64.decodestring(rows[0][2])) return [str(rows[0][1]), service_parameters, service_results] sqlStr=Dialect.buildQuery("ProdCom.Query3",{'client_id':client_id,\ 'client_tag':client_tag,'service_call':service_call}) Session.execute(sqlStr) rows = Session.fetchall() if len(rows) != 1: raise ProdException( "No entries found for client ID: " + str(client_id) + " and tag " + str(client_tag), 1002) service_results = cPickle.loads(base64.decodestring(rows[0][0])) service_parameters = cPickle.loads(base64.decodestring(rows[0][1])) return [service_results, service_parameters] except Exception, ex: raise ProdException(exceptions[4001] + str(ex), 4001)
def registerHandler(objectRef, objectName, registryName): """ _registerHandler_ Register a new Handler with the name provided """ if not GlobalRegistry.registries.has_key(registryName): logging.debug("Creating registry: " + registryName) GlobalRegistry.registries[registryName] = {} if objectName in GlobalRegistry.registries[registryName].keys(): msg = "Duplicate Name used to registerHandler object:\n" msg += "%s already exists\n" % objectName raise ProdException(msg, 1004) if not callable(objectRef): msg = "Object registered as a Handler is not callable:\n" msg += "Object registered as %s\n" % objectName msg += "The object must be a callable object, either\n" msg += "a function or class instance with a __call__ method\n" raise ProdException(msg, 1005) GlobalRegistry.registries[registryName][objectName] = objectRef logging.debug("Registered " + objectName + " in registry " + registryName) return
def rollback(sessionID=None): global session global current_session if sessionID == None: sessionID = current_session if not session.has_key(sessionID): raise ProdException(exceptions[4002], 4002) if not session[sessionID]['state'] == 'start_transaction': raise ProdException(exceptions[4003], 4003) execute("ROLLBACK", sessionID) session[sessionID]['cursor'].close() session[sessionID]['state'] = 'commit' session[sessionID]['queries'] = [] logging.debug("Transaction rolled back")
def retrieve(serverURL=None, method_name=None, componentID=None): try: if serverURL == None and method_name == None and componentID == None: sqlStr = """SELECT server_url,service_call,component_id, max(log_time) FROM ws_last_call WHERE call_state="call_placed" GROUP BY server_url; """ elif serverURL == None and method_name == None and componentID != None: sqlStr = """SELECT server_url,service_call,component_id, max(log_time) FROM ws_last_call WHERE component_id="%s" AND call_state="call_placed" GROUP BY server_url; """ % (componentID) elif serverURL == None and method_name != None and componentID != None: sqlStr = """SELECT server_url,service_call,component_id, max(log_time) FROM ws_last_call WHERE component_id="%s" AND service_call="%s" AND call_state="call_placed" GROUP BY server_url; """ % (componentID, method_name) elif serverURL != None and method_name == None and componentID != None: sqlStr = """SELECT server_url,service_call,component_id, max(log_time) FROM ws_last_call WHERE component_id="%s" AND server_url="%s" AND call_state="call_placed" GROUP BY server_url; """ % (componentID, serverURL) Session.execute(sqlStr) rows = Session.fetchall() if len(rows) == 0: raise ProdException("No result in local last service call table with componentID :"+\ str(componentID),1000) server_url = rows[0][0] service_call = rows[0][1] component_id = rows[0][2] return [server_url, service_call, component_id] except Exception, ex: raise ProdAgentException("Service commit Error: " + str(ex))
def commit(sessionID=None): global session global current_session if sessionID == None: sessionID = current_session if not session.has_key(sessionID): raise ProdException(exceptions[4002], 4002) if session[sessionID]['state'] == 'connect': raise ProdException(exceptions[4005], 4005) if session[sessionID]['state'] != 'commit': execute("COMMIT", sessionID) session[sessionID]['cursor'].close() session[sessionID]['state'] = 'commit' session[sessionID]['queries'] = [] logging.debug("Transaction committed")
def set_session(sessionID="default"): global session global current_session if not session.has_key(sessionID): raise ProdException(exceptions[4002], 4002) current_session = sessionID
def close(sessionID=None): global session global current_session if sessionID == None: sessionID = current_session if not session.has_key(sessionID): raise ProdException(exceptions[4002], 4002) session[sessionID]['connection'].close() del session[sessionID]
def get_cursor(sessionID=None): global session global current_db if sessionID == None: sessionID = current_session if not session.has_key(sessionID): raise ProdException(exceptions[4002], 4002) return session[sessionID]['cursor']
def get_cursor(sessionID=None): global session if sessionID == None: sessionID = current_session if not session.has_key(sessionID): raise ProdException(exceptions[4002], 4002) if not session[sessionID]['state'] == 'start_transaction': start_transaction(sessionID) return session[sessionID]['cursor']
def fetchone(sessionID=None): global current_session global session if sessionID == None: sessionID = current_session if not session.has_key(sessionID): raise ProdException(exceptions[4002], 4002) cursor = get_cursor(sessionID) return cursor.fetchone()
def __init__(self, dbs_url, dbs_address): self.conf = {} self.conf['url'] = dbs_url self.conf['address'] = dbs_address try: self.api= dbsCgiApi.DbsCgiApi(dbs_url,\ {'instance': dbs_address}) except StandardError, ex: raise ProdException(exceptions[4009] + str(ex), 4009)
def remove(client_id, service_call): global db_config logging.debug('removing logged service call') try: sqlStr=Dialect.buildQuery("ProdCom.Query4",{'client_id':client_id,\ 'service_call':service_call}) Session.execute(sqlStr) except Exception, ex: raise ProdException(str(ex), 1001)
def __init__(self, dls_type, dls_address): self.conf = {} self.conf['type'] = dls_type self.conf['address'] = dls_address try: logging.debug('Instantiating DLS client interface') self.api= dlsClient.getDlsApi(dls_type = dls_type,\ dls_endpoint = dls_address) logging.debug('DLS client interface instantiated') except dlsApi.DlsApiError, inst: raise ProdException(exceptions[4010] + str(inst), 4010)
def seconds2H_M_S(remainingSeconds): try: if remainingSeconds < 0: raise ProdException( "seconds for conversion should be larger than 1", 1007) secondsInHours = 3600 secondsInMinutes = 60 hours = int(math.floor( float(remainingSeconds) / float(secondsInHours))) remainingSeconds = remainingSeconds - secondsInHours * hours minutes = int( math.floor(float(remainingSeconds) / float(secondsInMinutes))) seconds = remainingSeconds - secondsInMinutes * minutes timeFormat = str(hours) + ":" + str(minutes) + ":" + str(seconds) return timeFormat except Exception, ex: raise ProdException( "Error converting seconds to H:M:S format: " + str(ex), 1008)
def retrieveHandler(objectName, registryName): """ _retrieveHandler_ Get the Handler object mapped to the name provided """ if not GlobalRegistry.registries.has_key(registryName): msg = "Registry %s is not registered in the GlobalRegistry\n" % registryName raise ProdException(msg, 1006) if objectName not in GlobalRegistry.registries[registryName].keys(): msg = "Name: %s not a registered Handler\n" % objectName msg += "No object registered with that name in GlobalRegistry" raise RuntimeError, msg logging.debug("Retrieving " + objectName + " from registry " + registryName) return GlobalRegistry.registries[registryName][objectName]
def log(client_id, service_call, service_parameters, service_result, client_tag='0'): global db_config logging.debug('logging service call') try: sqlStr=Dialect.buildQuery("ProdCom.Query1",{'client_id':client_id,\ 'service_call':service_call,'service_parameters':service_parameters,\ 'service_result':service_result,'client_tag':client_tag,}) # NOTE: this has to be done different, we do this to keep the log time unique Session.execute(sqlStr) logging.debug('service call logged') except Exception, ex: logging.debug('ERROR in logging call: ' + str(ex)) raise ProdException(str(ex), 1001)
def start_transaction(sessionID=None): global session global current_db global current_session if sessionID == None: sessionID = current_session if not session.has_key(sessionID): raise ProdException(exceptions[4002], 4002) if not session[sessionID]['state'] == 'start_transaction': logging.debug("Creating cursor object for session: " + str(sessionID)) session[sessionID]['cursor'] = session[sessionID]['connection'].cursor( ) if current_db['dbType'] == 'mysql': startTransaction = "START TRANSACTION" session[sessionID]['cursor'].execute(startTransaction) session[sessionID]['state'] = 'start_transaction' current_session = sessionID
def __init__(self, message, **data): ProdException.__init__(self, message, 11000, **data)
def __init__(self, message, errorNo=3000, **data): ProdException.__init__(self, message, errorNo, **data)
import time import logging # Try to connect a maximum of 5 times. __maxConnectionAttempts = int(defaultConfig['maxConnectionAttempts']) # Time to wait to reconnect __dbWaitingTime = int(defaultConfig['dbWaitingTime']) def connect(dbName, dbHost, dbUser, dbPasswd, socketLocation, portNr=""): """ _connect_ Generic connect method that returns a connection opbject. """ for attempt in range(__maxConnectionAttempts): try: conn_str = dbUser + '/' + dbPasswd + '@' + dbHost if (portNr): conn_str = conn_str + ":" + portNr print conn_str conn = cx_Oracle.connect(conn_str) return conn except Exception, v: print v logging.debug("Error connecting to the database: " + str(v)) # wait and try again. time.sleep(__dbWaitingTime) raise ProdException(exceptions[4007], 4007)
def __init__(self, msg, **data): ProdException.__init__(self, msg, 7000, **data)
def __init__(self): msg = "ProdCommon.Core.GlobalRegistry should not be initialised" raise ProdException(msg, 1003)
def __init__(self, message, **data): ProdException.__init__(self, message, 10000, **data)
def __init__(self, msg, **data): ProdException.__init__(self, msg, errorNo=3000, **data)
def __init__(self, msg, **data): # need to find right error code ProdException.__init__(self, msg, 9000, **data)
def __init__(self, message, errorNo = 1000 , **data): ProdException.__init__(self, message, errorNo, **data)