def __init__(self, server, services, adapters, **configs): """Create a new Runtime """ self.server = server self.services = services self.adapters = adapters self.configs = configs self.probe = ProbeFrame() # Runtimes self._serviceRuntimes = None # Create stateful parameters self._started = False self._closed = True self._closedEvent = Event()
class Runtime(object): """Represents a server runtime """ GLOCK = Lock() logger = logging.getLogger('unifiedrpc.protocol.runtime') def __init__(self, server, services, adapters, **configs): """Create a new Runtime """ self.server = server self.services = services self.adapters = adapters self.configs = configs self.probe = ProbeFrame() # Runtimes self._serviceRuntimes = None # Create stateful parameters self._started = False self._closed = True self._closedEvent = Event() @property def started(self): """Get if the runtime is started """ return self._started @property def closed(self): """Get if the runtime is closed """ return self._closed def start(self): """Start this runtime """ self.startAsync() # Wait forever self.wait() def startAsync(self): """Start this runtime asynchronously """ with self.GLOCK: # Check flag if self._started: raise ValueError('The runtime is already started') if not self.services: raise ValueError('Require services') if not self.adapters: raise ValueError('Require adapters') # Initialize the parameters self._started = True self._closed = False self._serviceRuntimes = {} self._closedEvent.clear() # Boot up services self.logger.info('Boot up services') for service in self.services.itervalues(): self.logger.info('Start service [%s]', service.name) serviceRuntime = service.bootup(self) if not serviceRuntime: raise ValueError('Require service runtime after boot up service [%s]' % service.name) self._serviceRuntimes[service.id] = serviceRuntime # Start all adapters self.logger.info('Boot up adapters') for adapter in self.adapters.itervalues(): self.logger.info('Start adapter [%s]', adapter.name) adapter.startAsync(self) # Done self.logger.info('Runtime completely started') def wait(self): """Wait for this runtime to complete """ self._closedEvent.wait() def shutdown(self): """Shutdown this runtime """ # Shutdown all services if self._serviceRuntimes: for serviceRuntime in self._serviceRuntimes.itervalues(): try: self.logger.info('Shutdown service [%s]', serviceRuntime.service.name) serviceRuntime.shutdown() except: self.logger.exception('Failed to shutdown service [%s], skip', serviceRuntime.service.name) # Shutdown all adapters for adapter in self.adapters.itervalues(): try: self.logger.info('Shutdown adapter [%s]', adapter.name) adapter.shutdown() except: self.logger.exception('Failed to shutdown adapter [%s], skip', adapter.name) # Done self._started = False self._closed = True self._serviceRuntimes = None self._closedEvent.set() # The helper methods def invokeBeforeRequest(self, method): """Add a probe before request """ self.probe.add(PROBE_LOCATION_BEFORE_REQUEST, method) def invokeAfterRequest(self, method): """Add a probe after request """ self.probe.add(PROBE_LOCATION_AFTER_REQUEST, method) def invokeAfterResponse(self, method): """Add a probe after response """ self.probe.add(PROBE_LOCATION_AFTER_RESPONSE, method)
class Runtime(object): """Represents a server runtime """ GLOCK = Lock() logger = logging.getLogger('unifiedrpc.protocol.runtime') def __init__(self, server, services, adapters, **configs): """Create a new Runtime """ self.server = server self.services = services self.adapters = adapters self.configs = configs self.probe = ProbeFrame() # Runtimes self._serviceRuntimes = None # Create stateful parameters self._started = False self._closed = True self._closedEvent = Event() @property def started(self): """Get if the runtime is started """ return self._started @property def closed(self): """Get if the runtime is closed """ return self._closed def start(self): """Start this runtime """ self.startAsync() # Wait forever self.wait() def startAsync(self): """Start this runtime asynchronously """ with self.GLOCK: # Check flag if self._started: raise ValueError('The runtime is already started') if not self.services: raise ValueError('Require services') if not self.adapters: raise ValueError('Require adapters') # Initialize the parameters self._started = True self._closed = False self._serviceRuntimes = {} self._closedEvent.clear() # Boot up services self.logger.info('Boot up services') for service in self.services.itervalues(): self.logger.info('Start service [%s]', service.name) serviceRuntime = service.bootup(self) if not serviceRuntime: raise ValueError( 'Require service runtime after boot up service [%s]' % service.name) self._serviceRuntimes[service.id] = serviceRuntime # Start all adapters self.logger.info('Boot up adapters') for adapter in self.adapters.itervalues(): self.logger.info('Start adapter [%s]', adapter.name) adapter.startAsync(self) # Done self.logger.info('Runtime completely started') def wait(self): """Wait for this runtime to complete """ self._closedEvent.wait() def shutdown(self): """Shutdown this runtime """ # Shutdown all services if self._serviceRuntimes: for serviceRuntime in self._serviceRuntimes.itervalues(): try: self.logger.info('Shutdown service [%s]', serviceRuntime.service.name) serviceRuntime.shutdown() except: self.logger.exception( 'Failed to shutdown service [%s], skip', serviceRuntime.service.name) # Shutdown all adapters for adapter in self.adapters.itervalues(): try: self.logger.info('Shutdown adapter [%s]', adapter.name) adapter.shutdown() except: self.logger.exception( 'Failed to shutdown adapter [%s], skip', adapter.name) # Done self._started = False self._closed = True self._serviceRuntimes = None self._closedEvent.set() # The helper methods def invokeBeforeRequest(self, method): """Add a probe before request """ self.probe.add(PROBE_LOCATION_BEFORE_REQUEST, method) def invokeAfterRequest(self, method): """Add a probe after request """ self.probe.add(PROBE_LOCATION_AFTER_REQUEST, method) def invokeAfterResponse(self, method): """Add a probe after response """ self.probe.add(PROBE_LOCATION_AFTER_RESPONSE, method)