Beispiel #1
0
 def __init__(self, config_path, tests=False):
     self.config = Config()
     if config_path:
         self.config.read(config_path)
     self.config.read_env(tests=tests)
     if tests:
         self.engine = sqlalchemy.create_engine(
             self.config.database.create_url(),
             poolclass=sqlalchemy.pool.AssertionPool)
     else:
         self.engine = sqlalchemy.create_engine(
             self.config.database.create_url())
     if not self.config.index.host:
         self.index = None
     else:
         self.index = IndexClientPool(host=self.config.index.host,
                                      port=self.config.index.port,
                                      recycle=60)
     if not self.config.redis.host:
         self.redis = None
     else:
         self.redis = Redis(host=self.config.redis.host,
                            port=self.config.redis.port)
     self._console_logging_configured = False
     self.setup_logging()
Beispiel #2
0
class Script(object):

    def __init__(self, config_path, tests=False):
        # type: (str, bool) -> None
        self.config = Config()
        if config_path:
            self.config.read(config_path)
        self.config.read_env(tests=tests)

        create_engine_kwargs = {'poolclass': sqlalchemy.pool.AssertionPool} if tests else {}
        self.db_engines = self.config.databases.create_engines(**create_engine_kwargs)

        self.index = IndexClientPool(host=self.config.index.host,
                                     port=self.config.index.port,
                                     recycle=60)

        if self.config.redis.sentinel:
            self.redis_sentinel = RedisSentinel([(self.config.redis.host, self.config.redis.port)])
            self.redis = self.redis_sentinel.master_for(self.config.redis.cluster)  # type: Redis
        else:
            self.redis = Redis(host=self.config.redis.host,
                               port=self.config.redis.port)

        self._console_logging_configured = False
        if not tests:
            self.setup_logging()

    def setup_logging(self):
        # type: () -> None
        for logger_name, level in sorted(self.config.logging.levels.items()):
            logging.getLogger(logger_name).setLevel(level)
        if self.config.logging.syslog:
            handler = LocalSysLogHandler(ident='acoustid',
                facility=self.config.logging.syslog_facility, log_pid=True)
            handler.setFormatter(logging.Formatter('%(name)s: %(message)s'))
            logging.getLogger().addHandler(handler)
        else:
            self.setup_console_logging()

    def setup_console_logging(self, quiet=False):
        # type: (bool) -> None
        if self._console_logging_configured:
            return
        handler = logging.StreamHandler()
        handler.setFormatter(logging.Formatter('[%(asctime)s] [%(process)s] [%(levelname)s] %(message)s', '%Y-%m-%d %H:%M:%S %z'))
        if quiet:
            handler.setLevel(logging.ERROR)
        logging.getLogger().addHandler(handler)
        self._console_logging_configured = True

    def setup_sentry(self):
        # type: () -> None
        sentry_sdk.init(self.config.sentry.script_dsn, release=GIT_RELEASE)

    def context(self, use_two_phase_commit=None):
        # type: (Optional[bool]) -> ScriptContext
        db = DatabaseContext(self, use_two_phase_commit=use_two_phase_commit)
        return ScriptContext(config=self.config, db=db, redis=self.redis, index=self.index)
Beispiel #3
0
class Script(object):
    def __init__(self, config_path, tests=False):
        self.config = Config()
        if config_path:
            self.config.read(config_path)
        self.config.read_env(tests=tests)
        if tests:
            self.engine = sqlalchemy.create_engine(
                self.config.database.create_url(),
                poolclass=sqlalchemy.pool.AssertionPool)
        else:
            self.engine = sqlalchemy.create_engine(
                self.config.database.create_url())
        if not self.config.index.host:
            self.index = None
        else:
            self.index = IndexClientPool(host=self.config.index.host,
                                         port=self.config.index.port,
                                         recycle=60)
        if not self.config.redis.host:
            self.redis = None
        else:
            self.redis = Redis(host=self.config.redis.host,
                               port=self.config.redis.port)
        self._console_logging_configured = False
        self.setup_logging()

    def setup_logging(self):
        for logger_name, level in sorted(self.config.logging.levels.items()):
            logging.getLogger(logger_name).setLevel(level)
        if self.config.logging.syslog:
            handler = LocalSysLogHandler(
                ident='acoustid',
                facility=self.config.logging.syslog_facility,
                log_pid=True)
            handler.setFormatter(logging.Formatter('%(name)s: %(message)s'))
            logging.getLogger().addHandler(handler)
        else:
            self.setup_console_logging()

    def setup_console_logging(self, quiet=False):
        if self._console_logging_configured:
            return
        handler = logging.StreamHandler()
        handler.setFormatter(
            logging.Formatter(
                '%(asctime)s [%(levelname)s] %(name)s - %(message)s',
                '%H:%M:%S'))
        if quiet:
            handler.setLevel(logging.ERROR)
        logging.getLogger().addHandler(handler)
        self._console_logging_configured = True

    def setup_sentry(self):
        sentry_sdk.init(self.config.sentry.script_dsn, release=GIT_RELEASE)
Beispiel #4
0
def setup():
    global config, engine
    config_path = os.path.dirname(
        os.path.abspath(__file__)) + '/../acoustid-test.conf'
    config = Config(config_path)
    engine = sqlalchemy.create_engine(config.database.create_url(),
                                      poolclass=sqlalchemy.pool.AssertionPool)
    if not os.environ.get('SKIP_DB_SETUP'):
        with closing(engine.connect()) as conn:
            for table in TABLES:
                conn.execute("TRUNCATE TABLE %s CASCADE" % (table, ))
            prepare_database(conn, BASE_SQL)
Beispiel #5
0
    def __init__(self, config_path, tests=False):
        # type: (str, bool) -> None
        self.config = Config()
        if config_path:
            self.config.read(config_path)
        self.config.read_env(tests=tests)

        self.db_engines = self.config.databases.create_engines()

        if self.config.statsd.enabled:
            self.statsd = StatsClient(host=self.config.statsd.host,
                                      port=self.config.statsd.port,
                                      prefix=self.config.statsd.prefix)
        else:
            self.statsd = None

        self.index = IndexClientPool(host=self.config.index.host,
                                     port=self.config.index.port,
                                     recycle=60)

        self.redis = None
        self.redis_sentinel = None

        if self.config.redis.sentinel:
            self.redis_sentinel = RedisSentinel(
                [(self.config.redis.host, self.config.redis.port)],
                password=self.config.redis.password,
            )
        else:
            self.redis = Redis(
                host=self.config.redis.host,
                port=self.config.redis.port,
                password=self.config.redis.password,
            )

        self._console_logging_configured = False
        if not tests:
            self.setup_logging()
Beispiel #6
0
    def __init__(self, config_path, tests=False):
        # type: (str, bool) -> None
        self.config = Config()
        if config_path:
            self.config.read(config_path)
        self.config.read_env(tests=tests)

        create_engine_kwargs = {'poolclass': sqlalchemy.pool.AssertionPool} if tests else {}
        self.db_engines = self.config.databases.create_engines(**create_engine_kwargs)

        self.index = IndexClientPool(host=self.config.index.host,
                                     port=self.config.index.port,
                                     recycle=60)

        if self.config.redis.sentinel:
            self.redis_sentinel = RedisSentinel([(self.config.redis.host, self.config.redis.port)])
            self.redis = self.redis_sentinel.master_for(self.config.redis.cluster)  # type: Redis
        else:
            self.redis = Redis(host=self.config.redis.host,
                               port=self.config.redis.port)

        self._console_logging_configured = False
        if not tests:
            self.setup_logging()
Beispiel #7
0
 def __init__(self, config_path):
     self.config = Config(config_path)
     #self.engine = sqlalchemy.create_engine(self.config.database.create_url(),
     #    poolclass=sqlalchemy.pool.AssertionPool)
     self.engine = sqlalchemy.create_engine(
         self.config.database.create_url())
     if not self.config.index.host:
         self.index = None
     else:
         self.index = IndexClientPool(host=self.config.index.host,
                                      port=self.config.index.port,
                                      recycle=60)
     if not self.config.redis.host:
         self.redis = None
     else:
         self.redis = Redis(host=self.config.redis.host,
                            port=self.config.redis.port)
     self.setup_logging()
Beispiel #8
0
 def __init__(self, config_path):
     self.config = Config(config_path)
     self.engine = sqlalchemy.create_engine(
         self.config.database.create_url())
     self.setup_logging()