class BasicModule(IModule): # lock InstanceLock = Lock() """ -------------------------------------------------------------------- """ """ __init__ """ """ -------------------------------------------------------------------- """ def __init__(self, name, *p, **k): # set name self.name = name # logger self.logger = Activator().getInstance('LoggerFactory').getLogger(self.name) # request-handler self.requestHandler = None # onStop-delegates self.onStopDelegates = [] # running-flag self.running = False # listener-thread self.thread = Thread(target = self.run) self.thread.setName(self.name) self.thread.setDaemon(True) """ -------------------------------------------------------------------- """ """ run """ """ -------------------------------------------------------------------- """ def run(self): # log self.logger.info("up and running") # running-flag self.running = True # module-main try: self.main() except Exception, e: if self.running: self.logger.error("Exception in Module-Thread (%s)" % (e)) # shutdown self.shutdown()
class DataAdapterADOdb(IDataAdapter): """ -------------------------------------------------------------------- """ """ __init__ """ """ -------------------------------------------------------------------- """ def __init__(self): # logger self.logger = Activator().getInstance('LoggerFactory').getLogger('DataAdapterADOdb') # db-config self.dbConfig = None # connection self.connection = None """ -------------------------------------------------------------------- """ """ loadSettings """ """ -------------------------------------------------------------------- """ def loadSettings(self): # info self.logger.info('loading settings...') # try/finally (separate this for python < 2.5) try: # try try: # open connection self.openConnection() # execute select and get cursor cursor = self.connection.Execute('SELECT tf_key, tf_value FROM tf_settings') # retval retVal = {} # process rows while not cursor.EOF: # get row-dict rowDict = cursor.GetRowAssoc(0) # add setting retVal[rowDict['tf_key']] = rowDict['tf_value'] # next cursor.MoveNext() # close cursor cursor.Close() # check if len(retVal) < 1: raise Exception, 'settings-validation failed' # return return retVal # catch, log and rethrow except Exception, e: self.logger.error('failed to load Settings (%s)' % (e)) raise e # finally close the con finally: self.closeConnection() """ -------------------------------------------------------------------- """ """ saveSettings """ """ -------------------------------------------------------------------- """ def saveSettings(self, settings = {}): # info self.logger.info('saving settings...') # try/finally (separate this for python < 2.5) try: # try try: # open connection self.openConnection() # update settings try: # begin transaction self.connection.BeginTrans() # update for key, val in settings.iteritems(): # sql sql = 'UPDATE tf_settings SET tf_value = %s WHERE tf_key = %s' % \ (self.connection.qstr(val), self.connection.qstr(key)) # DEBUG #self.logger.debug(sql) # execute update and get cursor cursor = self.connection.Execute(sql) # close cursor cursor.Close() # commit transaction self.connection.CommitTrans() # catch, rollback and rethrow except Exception, e: self.connection.RollbackTrans() raise e # catch, log and rethrow except Exception, e: self.logger.error('failed to save Settings (%s)' % (e)) raise e # finally close the con finally: self.closeConnection() """ -------------------------------------------------------------------- """ """ openConnection """ """ -------------------------------------------------------------------- """ def openConnection(self): # debug self.logger.debug('open connection...') # get config try: self.dbConfig = getDatabaseConfig() # catch, log and rethrow except Exception, e: self.logger.error('failed to get database-config (%s)' % (e)) raise e # mysql if self.dbConfig['db_type'].lower().startswith('mysql'): # get ado-connection try: self.connection = adodb.NewADOConnection('mysql') if self.connection is None: raise Exception, 'connection is None' # catch, log and rethrow except Exception, e: self.logger.error('failed to get ADOConnection (%s)' % (e)) raise e # connect try: self.connection.Connect(self.dbConfig['db_host'], self.dbConfig['db_user'], self.dbConfig['db_pass'], self.dbConfig['db_name']) # catch, log and rethrow except Exception, e: self.logger.error('failed to connect to database (%s)' % (e)) raise e
class Dispatcher(IActivator): # instance Instance = None # lock InstanceLock = Lock() """ -------------------------------------------------------------------- """ """ __new__ """ """ -------------------------------------------------------------------- """ def __new__(cls, *p, **k): if Dispatcher.Instance is None: Dispatcher.Instance = object.__new__(cls, *p, **k) return Dispatcher.Instance """ -------------------------------------------------------------------- """ """ __init__ """ """ -------------------------------------------------------------------- """ def __init__(self, name): # set name self.__name = name # logger self.logger = Activator().getInstance('LoggerFactory').getLogger('Dispatcher') # onStop-delegates self.__onStopDelegates = [] # request-queue self.__queueRequest = Queue() # request-handler self.__requestHandler = RequestHandler(self.logger) # running-flag self.__running = False # request-count self.requestCount = 0 # thread self.__thread = Thread(target = self.run) self.__thread.setName(self.__name) self.__thread.setDaemon(True) """ -------------------------------------------------------------------- """ """ getName """ """ -------------------------------------------------------------------- """ def getName(self): return self.__name """ -------------------------------------------------------------------- """ """ start """ """ -------------------------------------------------------------------- """ def start(self): # log self.logger.info('Starting...') # start thread self.__thread.start() """ -------------------------------------------------------------------- """ """ stop """ """ -------------------------------------------------------------------- """ def stop(self): # running-flag self.__running = False # join self.__thread.join(2.0) # shutdown if still alive if self.__thread.isAlive(): self.shutdown() """ -------------------------------------------------------------------- """ """ run """ """ -------------------------------------------------------------------- """ def run(self): # running-flag self.__running = True # debug self.logger.info("up and running") # main-loop while self.__running: # process queue try: # get and process request request = self.__queueRequest.get() # handle request self.__running = self.__requestHandler.handleRequest(request) except Empty, emp: self.logger.error("request-queue is empty (%s)" % emp) except Exception, e: self.logger.error("failed to process request (%s)" % e)
class ModuleManager(IActivator): # instance Instance = None # lock InstanceLock = Lock() # modules Modules = {} """ -------------------------------------------------------------------- """ """ __new__ """ """ -------------------------------------------------------------------- """ def __new__(cls, *p, **k): if ModuleManager.Instance is None: ModuleManager.Instance = object.__new__(cls, *p, **k) return ModuleManager.Instance """ -------------------------------------------------------------------- """ """ __init__ """ """ -------------------------------------------------------------------- """ def __init__(self, name): # set name self.__name = name # logger self.logger = Activator().getInstance('LoggerFactory').getLogger('ModuleManager') """ -------------------------------------------------------------------- """ """ getName """ """ -------------------------------------------------------------------- """ def getName(self): return self.__name """ -------------------------------------------------------------------- """ """ startModule """ """ -------------------------------------------------------------------- """ @synchronized(InstanceLock) def startModule(self, requestHandler, name = None): try: # start all modules if name is None: self.logger.info('Starting all Modules...') names = Config().get('modules', 'Modules').strip().split(',') for name in names: name = name.strip() if isTrue(Config().getExt(name, 'enabled').strip()): try: # check if exists if ModuleManager.Modules.has_key(name): raise Exception, "Module does already exist: %s" % (name) # start ModuleManager.Modules[name] = getClassByName(Config().get(name, 'module').strip(), Config().get(name, 'class').strip())(name) ModuleManager.Modules[name].start(requestHandler, self.onModuleStop) except Exception, e: self.logger.error("failed to start Module %s (%s)" % (name, e)) # start single module else:
class Fluxcli(IActivator): # instance Instance = None # lock InstanceLock = Lock() # path (relative to docroot) Path = 'bin/fluxcli.php' """ -------------------------------------------------------------------- """ """ __new__ """ """ -------------------------------------------------------------------- """ def __new__(cls, *p, **k): if Fluxcli.Instance is None: Fluxcli.Instance = object.__new__(cls, *p, **k) return Fluxcli.Instance """ -------------------------------------------------------------------- """ """ __init__ """ """ -------------------------------------------------------------------- """ def __init__(self, name): # set name self.__name = name # logger self.logger = Activator().getInstance('LoggerFactory').getLogger('Fluxcli') # invocation-Count self.invocationCount = 0 """ -------------------------------------------------------------------- """ """ getName """ """ -------------------------------------------------------------------- """ def getName(self): return self.__name """ -------------------------------------------------------------------- """ """ getPath """ """ -------------------------------------------------------------------- """ def getPath(self): return '%s%s' % (Config().get('dir', 'docroot').strip(), Fluxcli.Path) """ -------------------------------------------------------------------- """ """ invoke """ """ -------------------------------------------------------------------- """ @synchronized(InstanceLock) def invoke(self, args = [], readResult = True): # increment counter self.invocationCount += 1 # log self.logger.info('invoking fluxcli...') try: # unshift fluxcli-arg args.insert(0, self.getPath()) # unshift php-arg (command is invoked thru an args # list, not by building a string command-line given # to a shell -- this avoids any quoting troubles) php = Config().get('file', 'php').strip() args.insert(0, php) # log pseudo-cmdline (see above, php is not actually invoked that way) self.logger.debug(' '.join([("'%s'" % arg) for arg in args])) # open if readResult: # invoke (use popen2.Popen3 directly to be able to reap # child correctly -- using os.popen2 leaves zombies) p = popen2.Popen3(args) p.tochild.close() result = p.fromchild.read() p.fromchild.close() p.wait() # return result return result # spawn else: # invoke and return bool return (os.spawnv(os.P_WAIT, php, args) == 0) except Exception, e: self.logger.error("Exception in invoke: %s" % (e)) raise e