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)
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)