def load_app_and_run_server() -> None: """Parse arguments, read configuration, and start the server.""" sys.path.append(os.getcwd()) shutdown_event = register_signal_handlers() args = parse_args(sys.argv[1:]) with args.config_file: config = read_config(args.config_file, args.server_name, args.app_name) assert config.server configure_logging(config, args.debug) app = make_app(config.app) listener = make_listener(args.bind) server = make_server(config.server, listener, app) if einhorn.is_worker(): einhorn.ack_startup() if args.reload: reloader.start_reload_watcher(extra_files=[args.config_file.name]) # clean up leftovers from initialization before we get into requests gc.collect() logger.info("Listening on %s, PID:%s", listener.getsockname(), os.getpid()) server.start() try: shutdown_event.wait() logger.info("Finally stopping server, PID:%s", os.getpid()) finally: server.stop()
def run_gunicorn_worker(): if not einhorn.is_worker(): print >> sys.stderr, "This process does not appear to be running under Einhorn." sys.exit(1) app = PasterApplication() util._setproctitle("worker [%s]" % app.cfg.proc_name) worker = EinhornSyncWorker(app.cfg, app) worker.init_process()
def setup_complete(self): self.startup_timer.stop() self.stats.flush() if self.log_start: self.log.error("%s:%s started %s at %s (took %.02fs)", self.reddit_host, self.reddit_pid, self.short_version, datetime.now().strftime("%H:%M:%S"), self.startup_timer.elapsed_seconds()) if einhorn.is_worker(): einhorn.ack_startup()
def load_app_and_run_server() -> None: """Parse arguments, read configuration, and start the server.""" sys.path.append(os.getcwd()) shutdown_event = register_signal_handlers() args = parse_args(sys.argv[1:]) with args.config_file: config = read_config(args.config_file, args.server_name, args.app_name) assert config.server configure_logging(config, args.debug) app = make_app(config.app) listener = make_listener(args.bind) server = make_server(config.server, listener, app) cfg = parse_config(config.server, {"drain_time": OptionalConfig(Timespan)}) if einhorn.is_worker(): einhorn.ack_startup() try: # pylint: disable=cyclic-import from baseplate.server.prometheus import start_prometheus_exporter except ImportError: logger.debug("Prometheus exporter not available. pip install prometheus-client to enable.") else: start_prometheus_exporter() if args.reload: reloader.start_reload_watcher(extra_files=[args.config_file.name]) # clean up leftovers from initialization before we get into requests gc.collect() logger.info("Listening on %s", listener.getsockname()) server.start() try: shutdown_event.wait() SERVER_STATE.state = ServerLifecycle.SHUTTING_DOWN if cfg.drain_time: logger.debug("Draining inbound requests...") time.sleep(cfg.drain_time.total_seconds()) finally: logger.debug("Gracefully shutting down...") server.stop() logger.info("Exiting")
def setup_complete(self): self.startup_timer.stop() self.stats.flush() if self.log_start: self.log.error( "%s:%s started %s at %s (took %.02fs)", self.reddit_host, self.reddit_pid, self.short_version, datetime.now().strftime("%H:%M:%S"), self.startup_timer.elapsed_seconds() ) if einhorn.is_worker(): einhorn.ack_startup()
def load_app_and_run_server(): """Parse arguments, read configuration, and start the server.""" register_signal_handlers() args = parse_args(sys.argv[1:]) config = read_config(args.config_file, args.server_name, args.app_name) configure_logging(config, args.debug) app = make_app(config.app) listener = make_listener(args.bind) server = make_server(config.server, listener, app) if einhorn.is_worker(): einhorn.ack_startup() if args.reload: reloader.start_reload_watcher(extra_files=[args.config_file.name]) logger.info("Listening on %s", listener.getsockname()) server.serve_forever()
def test_is_worker(self): self.assertTrue(einhorn.is_worker())
def test_is_not_worker(self): self.assertFalse(einhorn.is_worker())