def __init__(self, connection, config, handler=None): # Grab a copy of our config section self.config = config.section('DHCPAgent') # grab relevant settings queue_expire = int(self.config.get('queue_expire', 60)) # Initialize logger self.logger = Logger(name='dhcpagent', level=self.config['loglevel'], handler=handler) # Parse agent conf self.qconfig = Config(self.config['conffile'], 'AGENT') # Initialize super NeutronAgent.__init__(self, self.qconfig, self.config, 'DHCP agent') # Initialize RPC bits RPC.__init__(self, connection, exopts={ 'name': self.event_queue(), 'durable': False, 'type': 'topic' }, qopts={ 'name': 'rpcdaemon-dhcp_%s' % uuid4(), 'auto_delete': True, 'durable': False, 'routing_key': 'q-plugin', 'queue_arguments': { 'x-expires': queue_expire * 1000, } })
def __init__(self, args=None): # Parse args if args is None: args = {} options, _ = getopt.getopt(sys.argv[1:], 'c:d') options = dict(options) config_file = options.get('-c', '/usr/local/etc/rpcdaemon.conf') daemonize = '-d' not in options # Parse config self.config = Config(config_file, 'Daemon') # Initialize logger self.logger = Logger( name='rpcdaemon', level = self.config['loglevel'], path = self.config['logfile'] if daemonize else None, handler = None if daemonize else logging.StreamHandler() ) self.pidfile = PIDFile(self.config['pidfile']) if daemonize else None; # TOOD: plugin.check thread pool? self.timeout = int(self.config.get('check_interval', 1)) # Clamp in case we exit before worker exists self.worker = None # Initialize daemon DaemonContext.__init__( self, detach_process=daemonize, files_preserve=[self.logger.handler.stream.fileno()], pidfile=self.pidfile, stdout=self.logger.handler.stream, stderr=self.logger.handler.stream )
class Monitor(DaemonContext): def __init__(self, args=None): # Parse args if args is None: args = {} options, _ = getopt.getopt(sys.argv[1:], 'c:d') options = dict(options) config_file = options.get('-c', '/usr/local/etc/rpcdaemon.conf') daemonize = '-d' not in options # Parse config self.config = Config(config_file, 'Daemon') # Initialize logger self.logger = Logger( name='rpcdaemon', level = self.config['loglevel'], path = self.config['logfile'] if daemonize else None, handler = None if daemonize else logging.StreamHandler() ) self.pidfile = PIDFile(self.config['pidfile']) if daemonize else None; # TOOD: plugin.check thread pool? self.timeout = int(self.config.get('check_interval', 1)) # Clamp in case we exit before worker exists self.worker = None # Initialize daemon DaemonContext.__init__( self, detach_process=daemonize, files_preserve=[self.logger.handler.stream.fileno()], pidfile=self.pidfile, stdout=self.logger.handler.stream, stderr=self.logger.handler.stream ) def open(self): # Call super DaemonContext.open(self) # Needfuls.doit() self.logger.info('Initializing...') # RPC connection self.connection = Connection(self.config['rpchost']) self.logger.info('Loading plugins...') # Import and create plugin objects self.plugins = [ plugin(self.connection, self.config, self.logger.handler) for plugin in [ getattr( __import__( 'rpcdaemon.plugins.' + module.lower(), fromlist=[module] ), module) for module in self.config['plugins'].split() ] ] # Setup worker with plugins and crank it up self.logger.info('Starting worker...') self.worker = Worker(self.connection, self.plugins, handler=self.logger.handler) self.worker.daemon = True # Daemon thread self.worker.start() self.logger.info('Started.') def check(self): if self.worker.is_connected: self.logger.debug('Dispatching plugin checks...') for plugin in self.plugins: plugin.check() def close(self): # We might get called more than once, or before worker exists if self.is_open and self.worker and self.worker.is_alive(): self.logger.info('Stopping worker...') self.worker.should_stop = True self.worker.join(5) # Wait up to 5 seconds if self.worker.is_alive(): self.logger.warn( 'Error stopping worker. Shutting down uncleanly.' ) self.logger.info('Stopped.') DaemonContext.close(self)