def configure_extensions(app): # I18N babel.init_app(app) # Database db.init_app(app) # CouchDB couchdbmanager.setup(app) couchdbmanager.sync(app) # Login login_manager.init_app(app) login_manager.login_view = "authentification.login" # Roles principal.init_app(app) # Mail mail.init_app(app) # Plugins list global plugin_manager.init_plugin_manager(app.root_path + "/plugins", app) plugin_manager.activate_plugins() plugin_manager.setup_plugins()
def start_worker_for_queue(flow='simple_queue_processor', queue='zmon:queue:default', **execution_context): """ Starting execution point to the workflows """ known_flows = {'simple_queue_processor': flow_simple_queue_processor} if flow not in known_flows: logger.exception("Bad role: %s" % flow) sys.exit(1) logger.info("Starting worker with pid=%s, flow type: %s, queue: %s, execution_context: %s", os.getpid(), flow, queue, execution_context) setproctitle.setproctitle('zmon-worker {} {}'.format(flow, queue)) # init the plugin manager plugin_manager.init_plugin_manager() # load external plugins (should be run only once) plugin_manager.collect_plugins(global_config=get_config(), load_builtins=True, load_env=True) # start Flow Reactor here FlowControlReactor.get_instance().start() exit_code = 0 try: known_flows[flow](queue=queue, **execution_context) except (KeyboardInterrupt, SystemExit): logger.warning("Caught user signal to stop consumer: finishing!") except Exception: logger.exception("Exception in start_worker(). Details: ") exit_code = 2 finally: FlowControlReactor.get_instance().stop() sys.exit(exit_code)
def main(args=None): args = parse_args(args) main_proc = rpc_server.MainProcess() config = {} # load default configuration from file for path in (args.config_file, 'config.yaml'): if path and os.path.exists(path): config = read_config(path) break process_config(config) # allow overwritting any configuration setting via env vars for k, v in os.environ.items(): if k.startswith('WORKER_'): config[k.replace("WORKER_", "").replace("_", ".").lower()] = v # make zmon worker compatible with old redis config vars if 'redis.host' in config: port = config.get('redis.port', 6379) config.update( {"redis.servers": '{}:{}'.format(config["redis.host"], port)}) # save config in our settings module settings.set_workers_log_level(config.get('loglevel', 'INFO')) settings.set_external_config(config) settings.set_rpc_server_port(config.get('server.port')) logging.config.dictConfig(settings.RPC_SERVER_CONF['LOGGING']) logger = logging.getLogger(__name__) # start the process controller main_proc.start_proc_control() # start web server process under supervision main_proc.proc_control.spawn_process( target=start_web, kwargs=dict( listen_on=config.get('webserver.listen_on', '0.0.0.0'), port=int(config.get('webserver.port', '8080')), log_conf=None, threaded=True, rpc_url='http://{host}:{port}{path}'.format( host='localhost', port=config.get('server.port'), path=settings.RPC_SERVER_CONF['RPC_PATH']), ), flags=MONITOR_RESTART, # web server will be restarted if dies ) # init the plugin manager plugin_manager.init_plugin_manager() # load external plugins (should be run only once) plugin_manager.collect_plugins(global_config=config, load_builtins=True, load_env=True) # start worker processes per queue according to the config queues = config['zmon.queues'] for qn in queues.split(','): queue, N = (qn.rsplit('/', 1) + [DEFAULT_NUM_PROC])[:2] main_proc.proc_control.spawn_many( int(N), kwargs={ 'queue': queue, 'flow': 'simple_queue_processor', 'tracer': config.get('opentracing.tracer'), 'tracer_tags': { 'team': config.get('team', 'UNKNOWN'), 'account': config.get('account', 'UNKNOWN'), 'region': config.get('region', 'UNKNOWN'), }, }, flags=MONITOR_RESTART | MONITOR_KILL_REQ | MONITOR_PING) if not args.no_rpc: try: main_proc.start_rpc_server() except (KeyboardInterrupt, SystemExit): logger.info('RPC server stopped. Exiting main') return main_proc