def create_rabbitmq(app): from listenbrainz.webserver.rabbitmq_connection import init_rabbitmq_connection try: init_rabbitmq_connection(app) except ConnectionError as e: app.logger.error('Could not connect to RabbitMQ: %s', str(e)) return
def _send_listens_to_queue(listen_type, listens): submit = [] for listen in listens: if listen_type == LISTEN_TYPE_PLAYING_NOW: try: expire_time = listen["track_metadata"]["additional_info"].get( "duration", current_app.config['PLAYING_NOW_MAX_DURATION']) redis_connection._redis.redis.setex( 'playing_now:{}'.format(listen['user_id']), ujson.dumps(listen).encode('utf-8'), expire_time) except Exception as e: current_app.logger.error( "Redis rpush playing_now write error: " + str(e)) raise ServiceUnavailable( "Cannot record playing_now at this time.") else: submit.append(listen) if submit: # check if rabbitmq connection exists or not # and if not then try to connect try: rabbitmq_connection.init_rabbitmq_connection(current_app) except ConnectionError as e: current_app.logger.error('Cannot connect to RabbitMQ: %s' % str(e)) raise ServiceUnavailable( 'Cannot submit listens to queue, please try again later.') publish_data_to_queue( data=submit, exchange=current_app.config['INCOMING_EXCHANGE'], queue=current_app.config['INCOMING_QUEUE'], error_msg='Cannot submit listens to queue, please try again later.', )
def test_connection_error_when_rabbitmq_down(self): # create an app with no RabbitMQ config # as will be the case when RabbitMQ is down in production app = Flask(__name__) with self.assertRaises(ConnectionError): rabbitmq_connection._rabbitmq = None init_rabbitmq_connection(app)
def test_connection_error_when_rabbitmq_down(self): # create an app with no RabbitMQ config # as will be the case when RabbitMQ is down in production app = Flask(__name__) with self.assertRaises(ConnectionError): rabbitmq_connection._rabbitmq = None init_rabbitmq_connection(app)
def _send_listens_to_queue(listen_type, listens): submit = [] for listen in listens: if listen_type == LISTEN_TYPE_PLAYING_NOW: try: listen = handle_playing_now(listen) if listen: submit.append(listen) except Exception: current_app.logger.error( "Redis rpush playing_now write error: ", exc_info=True) raise APIServiceUnavailable( "Cannot record playing_now at this time.") else: submit.append(listen) if submit: # check if rabbitmq connection exists or not # and if not then try to connect try: rabbitmq_connection.init_rabbitmq_connection(current_app) except ConnectionError as e: current_app.logger.error('Cannot connect to RabbitMQ: %s' % str(e)) raise APIServiceUnavailable( 'Cannot submit listens to queue, please try again later.') if listen_type == LISTEN_TYPE_PLAYING_NOW: exchange = current_app.config['PLAYING_NOW_EXCHANGE'] queue = current_app.config['PLAYING_NOW_QUEUE'] else: exchange = current_app.config['INCOMING_EXCHANGE'] queue = current_app.config['INCOMING_QUEUE'] publish_data_to_queue( data=submit, exchange=exchange, queue=queue, error_msg='Cannot submit listens to queue, please try again later.', )
def create_rabbitmq(app): from listenbrainz.webserver.rabbitmq_connection import init_rabbitmq_connection init_rabbitmq_connection(app)
def create_app(debug=None): """ Generate a Flask app for LB with all configurations done and connections established. In the Flask app returned, blueprints are not registered. """ app = CustomFlask( import_name=__name__, use_flask_uuid=True, ) load_config(app) if debug is not None: app.debug = debug # As early as possible, if debug is True, set the log level of our 'listenbrainz' logger to DEBUG # to prevent flask from creating a new log handler if app.debug: logger = logging.getLogger('listenbrainz') logger.setLevel(logging.DEBUG) # initialize Flask-DebugToolbar if the debug option is True if app.debug and app.config['SECRET_KEY']: app.init_debug_toolbar() sentry_config = app.config.get('LOG_SENTRY') if sentry_config: sentry.init_sentry(**sentry_config) # Initialize BU cache and metrics cache.init(host=app.config['REDIS_HOST'], port=app.config['REDIS_PORT'], namespace=app.config['REDIS_NAMESPACE']) metrics.init("listenbrainz") # Database connections from listenbrainz import db from listenbrainz.db import timescale as ts from listenbrainz import messybrainz as msb db.init_db_connection(app.config['SQLALCHEMY_DATABASE_URI']) ts.init_db_connection(app.config['SQLALCHEMY_TIMESCALE_URI']) msb.init_db_connection(app.config['MESSYBRAINZ_SQLALCHEMY_DATABASE_URI']) # Redis connection from listenbrainz.webserver.redis_connection import init_redis_connection init_redis_connection(app.logger) # Timescale connection from listenbrainz.webserver.timescale_connection import init_timescale_connection init_timescale_connection(app) # RabbitMQ connection from listenbrainz.webserver.rabbitmq_connection import init_rabbitmq_connection try: init_rabbitmq_connection(app) except ConnectionError: app.logger.critical("RabbitMQ service is not up!", exc_info=True) if app.config['MB_DATABASE_URI']: from brainzutils import musicbrainz_db musicbrainz_db.init_db_engine(app.config['MB_DATABASE_URI']) # OAuth from listenbrainz.webserver.login import login_manager, provider login_manager.init_app(app) provider.init(app.config['MUSICBRAINZ_CLIENT_ID'], app.config['MUSICBRAINZ_CLIENT_SECRET']) # Error handling from listenbrainz.webserver.errors import init_error_handlers init_error_handlers(app) from brainzutils.ratelimit import inject_x_rate_headers, set_user_validation_function set_user_validation_function(check_ratelimit_token_whitelist) @app.after_request def after_request_callbacks(response): return inject_x_rate_headers(response) # Template utilities app.jinja_env.add_extension('jinja2.ext.do') from listenbrainz.webserver import utils app.jinja_env.filters['date'] = utils.reformat_date app.jinja_env.filters['datetime'] = utils.reformat_datetime return app