def locateService(serviceName, servicePath, returnProfile=True, runconfig=None): """ Searches for the service named serviceName in the service path and loads the profile. Returns (serviceDirectory, profile) unless returnProfile is False in which case (serviceDirectory, None) returned. Raises ServiceNotFoundError if the service is not found (surprise).""" # search through service path logger = logging.getLogger() serviceDir = serviceName.lower() locations = ["%s/%s" % (i, serviceDir) for i in servicePath if os.path.exists("%s/%s" % (i, serviceDir)) and os.path.isdir("%s/%s" % (i, serviceDir))] # locations will hopefuly only be one item long; if not make # a note in the logs and take the first location if len(locations) > 1: logger.info("Found more than one location for service %s (using first)" % serviceName) if not locations: raise ServiceNotFoundError("Could not find service %s" % serviceName) servicePath = locations[0] if returnProfile: serviceProfile = loadServiceProfile(servicePath, runconfig) else: serviceProfile = None return (servicePath, serviceProfile)
def _pscOK(self, startupInfo): """ Now start the service. If OK, message the PSC accordingly; if not, let the PSC know we've failed and why, then initiate closedown. """ self.name = startupInfo['serviceName'] self.publishedName = startupInfo['publishedName'] self.servicepath = startupInfo['servicePath'] logging.closeHandlers() logToConsole = False if startupInfo['logdir'] == None: logToConsole = True logging.initLogging(rootLoggerName='WORKER: %s' % self.name, logLevel=getattr(logging, startupInfo['loglevel']), logdir=startupInfo['logdir'], logfile="worker_%s.log" % self.name, logToConsole=logToConsole) logging.setAdditionalLoggers(self) self.logger = logging.getLogger() # add any sevice directories to sys.path if not already there for sd in self.servicepath: if sd not in sys.path: sys.path.append(sd) self.loadService(startupInfo['runtimeConfig']) try: self.pscReference = startupInfo['pwa'] except Exception,ex: self.logger.exception('[1]')
def __init__(self, settings = {}): self.settings = settings if settings.has_key('profile'): self.profile = profile = settings.profile else: self.profile = PelotonSettings() self.logger = logging.getLogger() # hide sys.exit self._trapExit() # ensure that handlers only installed when things are OK reactor.callWhenRunning(self._setSignalHandlers)
def start(pc): """ Start a PSC. By default the first step is to daemonise, either by a double fork (on a POSIX system) or by re-spawning the process (on Windows) to detach from the console; this can be over-ridden by specifying --nodetach on the command line. The kernel is then started. This creates worker processes as required via the subprocess module. """ if not pc.nodetach: makeDaemon() logging.initLogging(rootLoggerName='PSC', logLevel=getattr(logging, pc.loglevel), logdir=pc.logdir, logfile='psc.log', logToConsole=pc.nodetach) logging.getLogger().info("Kernel starting; pid = %d" % os.getpid()) kernel = PelotonKernel(pc) logging.setAdditionalLoggers(kernel) ex = kernel.start() return ex
def loadService(self, runtimeConfig = None): """ Loading a service happens as follows: - Load service class - Validate its signature cookie ??? - Load configs: Here the configuration files are read and internals are organised. This is generally NOT overidden by the service writer who instead provides the startup() method to do logical business level initialisation. Raises ServiceConfigurationError if the name is invalid. """ try: pqcn = "%s.%s.%s" % (self.name.lower(), self.name.lower(), self.name) cls = getClassFromString(pqcn) self.__service = cls(self.name, self.dispatcher, logging.getLogger(self.name)) self.__service.initSupportServices() self.__service.loadConfig(self.servicepath, runtimeConfig) except Exception, ex: raise ServiceConfigurationError("Could not find class for service %s" % self.name, ex)
def main(): # Let's read the command line usage = "usage: %prog [options]" # add 'arg1 arg2' etc as required parser = FilteredOptionParser(usage=usage, version="Peloton version %s" % peloton.RELEASE_VERSION) # parser.set_defaults(nodetach=False) parser.add_option("--prefix", help="Prefix directory to help with setting paths") parser.add_option("--nodetach", action="store_true", default=False, help="Prevent PSC detaching from terminal [default: %default]") parser.add_option("-c", "--configfile", help="Path to configuration file for the PSC [default: %default].", dest="configfile", default='psc.pcfg') parser.add_option("-b", "--bind", help="""specify the host:port to which this instance should bind. Overides values set in configuration.""", dest='bindhost') parser.add_option("--anyport", action="store_true", default=False, help="""When set, this permits the PSC to seek a free port if the configured port is not available.""") parser.add_option("-s", "--servicepath", help="""Directory containing peloton services. You may specify several such directories with multiple instances of this option [default: %default]""", action="append", default=["$PREFIX/service"]) parser.add_option("--loglevel", help="""Set the logging level to one of critical, fatal, error(uat, prod), warning, info(test), debug(dev). (defaults for each run mode indicated in brackets).""", choices=['critical', 'fatal', 'error', 'warning', 'info', 'debug']) parser.add_option("--logdir", help="""Directory to which log files should be written. By setting this argument the logging-to-file system will be enabled.""") parser.add_option("--disable", help="""Comma delimited list of plugins to prevent starting even if configuration has them enabled""", action="append") parser.add_option("--enable", help="""Comma delimited list of plugins to start even if configuration has them disabled""", action="append") parser.add_option("--flags", help="""Comma delimited list of flags to add to this PSC.""", action="append") options, args = parser.parse_args() # Handling errors and pumping back through the system #if len(args) != 1: # parser.error("incorrect number of arguments") # add any sevice directories to sys.path if not already there for sd in options.servicepath: if sd not in sys.path: sys.path.append(sd) # enable, disable and flags are all 'append' types, but allow # comma delimited entries as well so we need to turn the list of # n potentially compound entries into a single list if options.enable: options.enable = deCompound(options.enable) else: options.enable=[] if options.disable: options.disable = deCompound(options.disable) else: options.disable=[] if options.flags: options.flags = deCompound(options.flags) else: options.flags=[] # determine the appropriate log-level for the root logger based # on supplied arguments. if options.loglevel: options.loglevel = options.loglevel.upper() else: options.loglevel = "ERROR" # Load configuration from file pc = PelotonSettings() try: pc.load(options.configfile) except IOError: sys.stderr.write("There is no profile for the PSC!\n") return 1 if pc.profile.has_key('flags'): pc.profile.flags.extend(options.flags) else: pc.profile.flags = options.flags # copy in the necessary from options to config keys =['configfile', 'nodetach', 'bindhost', 'anyport', 'servicepath', 'loglevel', 'logdir', 'enable', 'disable'] for k in keys: pc[k] = getattr(options, k) try: exitCode = start(pc) except: logging.getLogger().exception('Untrapped error in PSC: ') exitCode = 99 return exitCode
def _f(*args, **kargs): logging.getLogger().debug("%s called with args: %s, kargs:%s" % (f.func_name, str(args[1:]), str(kargs))) return f(*args, **kargs)
def initSupportServices(self): """ Start supporting services, such as the logger. Kept out of __init__ so that we can instantiate very lightly (as required by the launch sequencer.)""" self.logger = logging.getLogger(self.name)
def __init__(self, kernel): self.logger = logging.getLogger() self.profile = kernel.profile self.__kernel__ = kernel