def start(self): """Programmatic start of the main service.""" assert os.path.exists(self._env_work_dir) # reload configuration load_config(get_config_file()) logger.configure(config.get("logging")) timeline.configure_timeline(config.get("timeline")) # watch configuration changes self.start_watch_config() log.info('Starting Ambianic server...') # Register the signal handlers servers = {} # Start the job threads try: for s_name, s_class in ROOT_SERVERS.items(): srv = s_class(config=config) srv.start() servers[s_name] = srv self._latest_heartbeat = time.monotonic() self._servers = servers # Keep the main thread running, otherwise signals are ignored. while True: time.sleep(0.5) self._healthcheck(servers) self._heartbeat() except ServiceExit: log.info('Service exit requested.') # stop servers and cleanup references self._stop_servers(servers) self._servers = {} self._service_exit_requested = False # stop watching config files self.stop_watch_config() if self._service_restart_requested: self._service_restart_requested = False log.info('Restarting Ambianic server.') return self.start() log.info('Exiting Ambianic server.') return True
def _configure(env_work_dir=None): """Load configuration settings. :returns config dict if configuration was loaded without issues. None or a specific exception otherwise. """ assert env_work_dir, 'Working directory required.' assert os.path.exists(env_work_dir), \ 'working directory invalid: {}'.format(env_work_dir) print("Configuring server...") secrets_file = os.path.join(env_work_dir, SECRETS_FILE) config_file = os.path.join(env_work_dir, CONFIG_FILE) try: if os.path.isfile(secrets_file): with open(secrets_file) as sf: secrets_config = sf.read() else: secrets_config = "" log.warning('Secrets file not found. ' 'Proceeding without it: %s', secrets_file) with open(config_file) as cf: base_config = cf.read() all_config = secrets_config + "\n" + base_config config = yaml.safe_load(all_config) log.debug('loaded config from %r: %r', CONFIG_FILE, config) # configure logging logging_config = None if config: logging_config = config.get('logging', None) _configure_logging(logging_config) # configure pipeline timeline event log timeline_config = None if config: timeline_config = config.get('timeline', None) timeline.configure_timeline(timeline_config) return config except FileNotFoundError: log.warning('Configuration file not found: %s', config_file) log.warning('Please provide a configuration file and restart.') except Exception as e: log.exception('Configuration Error!', e, exc_info=True) return None
def timeline_config_handler(event: ConfigChangedEvent): # configure pipeline timeline event log log.info("Reconfiguring pipeline timeline event log") timeline.configure_timeline(config.get("timeline"))
def timeline_config_handler(config): # configure pipeline timeline event log timeline_config = None if config: timeline_config = config.get('timeline', None) timeline.configure_timeline(timeline_config)