Skip to content

Commit

Permalink
Prevent reporter from starting in a build process
Browse files Browse the repository at this point in the history
  • Loading branch information
karls committed Jul 1, 2024
1 parent 16e9814 commit 7a80ff3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions judoscale/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand Down
4 changes: 4 additions & 0 deletions judoscale/core/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 8 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions tests/test_reporter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from datetime import datetime

from pytest import fixture
Expand All @@ -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")
Expand Down Expand Up @@ -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

0 comments on commit 7a80ff3

Please sign in to comment.