def config(project_id): return Config( { "APP_NAME": "rele", "SUB_PREFIX": "rele", "GC_CREDENTIALS_PATH": "tests/dummy-pub-sub-credentials.json", "MIDDLEWARE": ["rele.contrib.LoggingMiddleware"], } )
def config(credentials, project_id): return Config( { "APP_NAME": "rele", "SUB_PREFIX": "rele", "GC_PROJECT_ID": project_id, "GC_CREDENTIALS": credentials, "MIDDLEWARE": ["rele.contrib.LoggingMiddleware"], } )
def test_sets_defaults(self): settings = {} config = Config(settings) assert config.app_name is None assert config.sub_prefix is None assert config.gc_project_id is None assert config.credentials is None assert config.middleware == ["rele.contrib.LoggingMiddleware"] assert config.encoder == json.JSONEncoder
def test_uses_project_id_from_creds_when_no_project_id_given(self): settings = { "GC_CREDENTIALS_PATH": "tests/dummy-pub-sub-credentials.json", } config = Config(settings) assert isinstance(config.credentials, google.oauth2.service_account.Credentials) assert config.credentials.project_id == "rele-test" assert config.gc_project_id == "rele-test"
def test_inits_service_account_creds_when_credential_path_given( self, project_id): settings = { "GC_CREDENTIALS_PATH": "tests/dummy-pub-sub-credentials.json", } config = Config(settings) assert config.gc_project_id == project_id assert isinstance(config.credentials, google.oauth2.service_account.Credentials) assert config.credentials.project_id == "rele-test"
def test_sets_defaults_pulled_from_env(self, monkeypatch, project_id): settings = {} config = Config(settings) assert config.app_name is None assert config.sub_prefix is None assert config.gc_project_id == "rele-test" assert isinstance(config.credentials, google.oauth2.service_account.Credentials) assert config.middleware == ["rele.contrib.LoggingMiddleware"] assert config.encoder == json.JSONEncoder
def test_parses_all_keys(self, project_id, custom_encoder): settings = { "APP_NAME": "rele", "SUB_PREFIX": "rele", "GC_CREDENTIALS_PATH": "tests/dummy-pub-sub-credentials.json", "MIDDLEWARE": ["rele.contrib.DjangoDBMiddleware"], "ENCODER": custom_encoder, } config = Config(settings) assert config.app_name == "rele" assert config.sub_prefix == "rele" assert config.gc_project_id == project_id assert isinstance(config.credentials, service_account.Credentials) assert config.middleware == ["rele.contrib.DjangoDBMiddleware"]
def test_parses_all_keys(self, project_id, credentials, custom_encoder): settings = { "APP_NAME": "rele", "SUB_PREFIX": "rele", "GC_PROJECT_ID": project_id, "GC_CREDENTIALS": credentials, "MIDDLEWARE": ["rele.contrib.DjangoDBMiddleware"], "ENCODER": custom_encoder, } config = Config(settings) assert config.app_name == "rele" assert config.sub_prefix == "rele" assert config.gc_project_id == project_id assert config.credentials == credentials assert config.middleware == ["rele.contrib.DjangoDBMiddleware"]
class Command(BaseCommand): help = "Start subscriber threads to consume messages from Relé topics." config = Config(settings.RELE) def handle(self, *args, **options): if all( map(lambda x: x.get("CONN_MAX_AGE"), settings.DATABASES.values())): self.stderr.write( self.style.WARNING( "WARNING: settings.CONN_MAX_AGE is not set to 0. " "This may result in slots for database connections to " "be exhausted.")) subs = self._autodiscover_subs() self.stdout.write(f"Configuring worker with {len(subs)} " f"subscription(s)...") for sub in subs: self.stdout.write(f" {sub}") worker = Worker( subs, self.config.gc_project_id, self.config.credentials, self.config.ack_deadline, ) worker.setup() worker.start() signal.signal(signal.SIGINT, signal.SIG_IGN) signal.signal(signal.SIGTERM, worker.stop) signal.signal(signal.SIGTSTP, worker.stop) self._wait_forever() def _autodiscover_subs(self): return rele.config.load_subscriptions_from_paths( discover_subs_modules(), self.config.sub_prefix, settings.RELE.get("FILTER_SUBS_BY"), ) def _wait_forever(self): self.stdout.write("Consuming subscriptions...") while True: time.sleep(1)