def __init__(self): """Initialise a redis client connection to the state database.""" self.rc = redis.Redis( host=config.REDIS_HOST_STATE, port=config.REDIS_PORT, db=config.REDIS_DB_STATE) # get the correct subgrid id subgrid_id = utils.fetch_subgrid_id() while not subgrid_id: logger.info( '[StateReporter] waiting for a subgrid id from redis...') time.sleep(1) subgrid_id = utils.fetch_subgrid_id() self.redis_key = subgrid_id
def build_app(sub_port=5558, **kwargs): """Build the flask app.""" global app global message_data global cache logger.info("Starting threedi-wms...") logger.info("Subscription port: %d (server should publish on this port)." % sub_port) # use the correct subgrid_id subgrid_id = utils.fetch_subgrid_id() # add a ":" to the subgrid_id for uniformity cache_key_prefix = "{0}:".format(subgrid_id) logger.info( "Got subgrid_id: %s." % subgrid_id, extra={'subgrid_id': subgrid_id}) app = flask.Flask(__name__) if config.USE_CACHE: cache_config = { 'CACHE_TYPE': 'redis', 'CACHE_KEY_PREFIX': cache_key_prefix, 'CACHE_REDIS_HOST': config.REDIS_HOST_CACHE, 'CACHE_REDIS_PORT': config.REDIS_PORT, 'CACHE_REDIS_DB': config.REDIS_DB_THREEDI_WMS_CACHE, } else: cache_config = {'CACHE_TYPE': 'null', } cache = Cache(app, config=cache_config) # reset state variables logger.info("Reset wms state variables in redis.") reporter.reset_all() # setup sentry if hasattr(config, 'SENTRY_DSN'): app.config['SENTRY_DSN'] = config.SENTRY_DSN Sentry(app, dsn=config.SENTRY_DSN, logging=True, level=logging.ERROR) # this one is global because we only have one event loop that receives # messages message_data = MessageData(sub_port=sub_port) # register the blueprints for blueprint in blueprints.get_blueprints(): url_prefix = '/' + blueprint.name app.register_blueprint(blueprint, url_prefix=url_prefix) logger.info("Ready to rock and roll!", extra={'subgrid_id': subgrid_id}) return app
def get_loaded_model(): """Return the loaded_model (slug) from redis).""" subgrid_id = server_utils.fetch_subgrid_id() return rc.get('%s:loaded_model' % subgrid_id)