diff --git a/judoscale/core/config.py b/judoscale/core/config.py index 7476a6f..e9a2234 100644 --- a/judoscale/core/config.py +++ b/judoscale/core/config.py @@ -14,6 +14,13 @@ def is_redundant_instance(self): instance_number = self.split(".")[-1] return instance_number.isdigit() and int(instance_number) > 1 + @property + def is_release_instance(self): + # NOTE: this is currently Heroku-specific. We may need to update this + # for other platforms in the future and possibly move it to the Config + # module. + return self.lower().startswith("release") + class Config(UserDict): def __init__( diff --git a/judoscale/core/reporter.py b/judoscale/core/reporter.py index 038bbd7..fe7794b 100644 --- a/judoscale/core/reporter.py +++ b/judoscale/core/reporter.py @@ -52,6 +52,10 @@ def start(self): logger.info("Reporter not started: API_BASE_URL not set") return + if self.config["RUNTIME_CONTAINER"].is_release_instance: + logger.info("Reporter not started: in a build process") + return + logger.info(f"Starting reporter for process {self.pid}") self._thread = threading.Thread(target=self._run_loop, daemon=True) self._thread.start() diff --git a/tests/conftest.py b/tests/conftest.py index e615e60..29bdb31 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,6 +3,13 @@ from judoscale.core.config import Config +@fixture +def heroku_release_1(monkeypatch): + monkeypatch.setenv("JUDOSCALE_URL", "https://api.example.com") + monkeypatch.setenv("DYNO", "release.1") + return Config.initialize() + + @fixture def heroku_web_1(monkeypatch): monkeypatch.setenv("JUDOSCALE_URL", "https://api.example.com") diff --git a/tests/test_config.py b/tests/test_config.py index 446c111..6f32bfe 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -138,6 +138,14 @@ def test_is_redundant_instance(self): container = RuntimeContainer("web.2") assert container.is_redundant_instance + def test_is_release_instance(self): + container = RuntimeContainer("release.1") + assert container.is_release_instance + + def test_is_release_instance_2(self): + container = RuntimeContainer("release.2") + assert container.is_release_instance + def test_is_not_redundant_instance(self): container = RuntimeContainer("web.1") assert not container.is_redundant_instance diff --git a/tests/test_reporter.py b/tests/test_reporter.py index 748644b..1885891 100644 --- a/tests/test_reporter.py +++ b/tests/test_reporter.py @@ -1,3 +1,4 @@ +import logging from datetime import datetime from pytest import fixture @@ -14,6 +15,11 @@ def reporter(heroku_web_1): return Reporter(heroku_web_1) +@fixture +def reporter_in_release(heroku_release_1): + return Reporter(heroku_release_1) + + class TestReporter: def test_build_report(self, reporter): dt = datetime.fromisoformat("2012-12-12T12:12:00+00:00") @@ -81,3 +87,14 @@ def test_start(self, reporter): assert reporter.config.is_enabled reporter.start() assert reporter._running + + def test_start_in_release(self, reporter_in_release, caplog): + caplog.set_level(logging.INFO, logger="judoscale") + + assert reporter_in_release.config.is_enabled + reporter_in_release.ensure_running() + assert not reporter_in_release._running + + for record in caplog.records: + assert record.levelname == "INFO" + assert "Reporter not started: in a build process" in record.message