Exemplo n.º 1
0
    def start_watch_config(self):
        if self._config_observer:
            self.stop_watch_config()
        self._config_observer = Observer()
        config_paths = [
            get_config_file(),
            get_secrets_file(),
        ]
        for filepath in config_paths:
            if not os.path.exists(filepath):
                log.warning(
                    "File %s not found, skip changes watch" %
                    filepath
                )
                continue
            log.info(
                    "Watching %s for changes" % filepath
                )
            self._config_observer.schedule(
                self,
                filepath,
                recursive=False
            )

        self._config_observer.start()
Exemplo n.º 2
0
    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_event.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