From 68bd31bc11f044940af34804d1d3dd57caed9a1c Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Mon, 2 Feb 2026 15:32:34 -0300 Subject: [PATCH 1/3] Refactor init logic for JUDOSCALE_URL / API_BASE_URL Let Config initialize itself with JUDOSCALE_URL from all containers / platforms, and only on Render where we may need the legacy fallback we can check whether API_BASE_URL was set, and if not set it with the legacty URL based on the service/instance. This better matches how the other packages initializes the config, and simplifies this flow a bit, since the platforms all respect JUDOSCALE_URL when set. --- judoscale/core/config.py | 29 ++++++++++++----------------- tests/test_config.py | 7 ++----- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/judoscale/core/config.py b/judoscale/core/config.py index 40913c3..5a54f9c 100644 --- a/judoscale/core/config.py +++ b/judoscale/core/config.py @@ -23,13 +23,11 @@ def is_release_instance(self): class Config(UserDict): - def __init__( - self, runtime_container: RuntimeContainer, api_base_url: str, env: Mapping - ): + def __init__(self, runtime_container: RuntimeContainer, env: Mapping): initialdata = dict( DEFAULTS, RUNTIME_CONTAINER=runtime_container, - API_BASE_URL=api_base_url, + API_BASE_URL=env.get("JUDOSCALE_URL"), ) for key in {"RQ", "CELERY"}: @@ -63,45 +61,42 @@ def initialize(cls, env: Mapping = os.environ): @classmethod def for_heroku(cls, env: Mapping): runtime_container = RuntimeContainer(env["DYNO"]) - api_base_url = env.get("JUDOSCALE_URL") - return cls(runtime_container, api_base_url, env) + return cls(runtime_container, env) @classmethod def for_render(cls, env: Mapping): service_id = env.get("RENDER_SERVICE_ID") instance = env.get("RENDER_INSTANCE_ID").replace(f"{service_id}-", "") runtime_container = RuntimeContainer(instance) - api_base_url = env.get("JUDOSCALE_URL") or f"https://adapter.judoscale.com/api/{service_id}" - return cls(runtime_container, api_base_url, env) + config = cls(runtime_container, env) + if not config["API_BASE_URL"]: + config["API_BASE_URL"] = f"https://adapter.judoscale.com/api/{service_id}" + return config @classmethod def for_ecs(cls, env: Mapping): instance = env["ECS_CONTAINER_METADATA_URI"].split("/")[-1] runtime_container = RuntimeContainer(instance) - api_base_url = env.get("JUDOSCALE_URL") - return cls(runtime_container, api_base_url, env) + return cls(runtime_container, env) @classmethod def for_fly(cls, env: Mapping): runtime_container = RuntimeContainer(env["FLY_MACHINE_ID"]) - api_base_url = env.get("JUDOSCALE_URL") - return cls(runtime_container, api_base_url, env) + return cls(runtime_container, env) @classmethod def for_railway(cls, env: Mapping): runtime_container = RuntimeContainer(env["RAILWAY_REPLICA_ID"]) - api_base_url = env.get("JUDOSCALE_URL") - return cls(runtime_container, api_base_url, env) + return cls(runtime_container, env) @classmethod def for_custom(cls, env: Mapping): runtime_container = RuntimeContainer(env["JUDOSCALE_CONTAINER"]) - api_base_url = env.get("JUDOSCALE_URL") - return cls(runtime_container, api_base_url, env) + return cls(runtime_container, env) @classmethod def for_unknown(cls, env: Mapping): - return cls(RuntimeContainer(""), env.get("JUDOSCALE_URL"), env) + return cls(RuntimeContainer(""), env) @property def is_enabled(self) -> bool: diff --git a/tests/test_config.py b/tests/test_config.py index da6b5a1..69cd763 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -125,13 +125,10 @@ def test_judoscale_log_level_env(self): assert config["API_BASE_URL"] == "https://api.example.com" def test_is_enabled(self): - config = Config(None, "", {}) + config = Config(RuntimeContainer(""), {}) assert not config.is_enabled - config = Config(None, None, {}) - assert not config.is_enabled - - config = Config(None, "https://some-url.com", {}) + config = Config(RuntimeContainer(""), {"JUDOSCALE_URL": "https://some-url.com"}) assert config.is_enabled def test_for_report(self): From d11233cc32abd119cc4f42b21defe07a3b4f96ac Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Mon, 2 Feb 2026 15:40:38 -0300 Subject: [PATCH 2/3] Inline all Config per-platform initialization Very little change between these, and it feels more work to keep separate methods than it is for them to share a single init method that we can refactor further. --- judoscale/core/config.py | 57 ++++++++++------------------------------ tests/test_config.py | 8 +++--- 2 files changed, 18 insertions(+), 47 deletions(-) diff --git a/judoscale/core/config.py b/judoscale/core/config.py index 5a54f9c..49ab65c 100644 --- a/judoscale/core/config.py +++ b/judoscale/core/config.py @@ -44,59 +44,30 @@ def __init__(self, runtime_container: RuntimeContainer, env: Mapping): @classmethod def initialize(cls, env: Mapping = os.environ): if env.get("JUDOSCALE_CONTAINER"): - return cls.for_custom(env) + runtime_container = RuntimeContainer(env["JUDOSCALE_CONTAINER"]) elif env.get("DYNO"): - return cls.for_heroku(env) + runtime_container = RuntimeContainer(env["DYNO"]) elif env.get("RENDER_INSTANCE_ID"): - return cls.for_render(env) + service_id = env.get("RENDER_SERVICE_ID") + instance = env["RENDER_INSTANCE_ID"].replace(f"{service_id}-", "") + runtime_container = RuntimeContainer(instance) elif env.get("ECS_CONTAINER_METADATA_URI"): - return cls.for_ecs(env) + instance = env["ECS_CONTAINER_METADATA_URI"].split("/")[-1] + runtime_container = RuntimeContainer(instance) elif env.get("FLY_MACHINE_ID"): - return cls.for_fly(env) + runtime_container = RuntimeContainer(env["FLY_MACHINE_ID"]) elif env.get("RAILWAY_REPLICA_ID"): - return cls.for_railway(env) + runtime_container = RuntimeContainer(env["RAILWAY_REPLICA_ID"]) else: - return cls.for_unknown(env) + runtime_container = RuntimeContainer("") - @classmethod - def for_heroku(cls, env: Mapping): - runtime_container = RuntimeContainer(env["DYNO"]) - return cls(runtime_container, env) - - @classmethod - def for_render(cls, env: Mapping): - service_id = env.get("RENDER_SERVICE_ID") - instance = env.get("RENDER_INSTANCE_ID").replace(f"{service_id}-", "") - runtime_container = RuntimeContainer(instance) config = cls(runtime_container, env) - if not config["API_BASE_URL"]: - config["API_BASE_URL"] = f"https://adapter.judoscale.com/api/{service_id}" - return config - @classmethod - def for_ecs(cls, env: Mapping): - instance = env["ECS_CONTAINER_METADATA_URI"].split("/")[-1] - runtime_container = RuntimeContainer(instance) - return cls(runtime_container, env) - - @classmethod - def for_fly(cls, env: Mapping): - runtime_container = RuntimeContainer(env["FLY_MACHINE_ID"]) - return cls(runtime_container, env) + # Render legacy support: fall back to constructing URL from service ID + if not config["API_BASE_URL"] and env.get("RENDER_SERVICE_ID"): + config["API_BASE_URL"] = f"https://adapter.judoscale.com/api/{env['RENDER_SERVICE_ID']}" - @classmethod - def for_railway(cls, env: Mapping): - runtime_container = RuntimeContainer(env["RAILWAY_REPLICA_ID"]) - return cls(runtime_container, env) - - @classmethod - def for_custom(cls, env: Mapping): - runtime_container = RuntimeContainer(env["JUDOSCALE_CONTAINER"]) - return cls(runtime_container, env) - - @classmethod - def for_unknown(cls, env: Mapping): - return cls(RuntimeContainer(""), env) + return config @property def is_enabled(self) -> bool: diff --git a/tests/test_config.py b/tests/test_config.py index 69cd763..9842ff8 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -118,7 +118,7 @@ def test_judoscale_log_level_env(self): "JUDOSCALE_LOG_LEVEL": "WARN", "JUDOSCALE_URL": "https://api.example.com", } - config = Config.for_heroku(fake_env) + config = Config.initialize(fake_env) assert config["RUNTIME_CONTAINER"] == "web.1" assert config["LOG_LEVEL"] == "WARN" @@ -137,7 +137,7 @@ def test_for_report(self): "LOG_LEVEL": "WARN", "JUDOSCALE_URL": "https://api.example.com", } - config = Config.for_heroku(fake_env) + config = Config.initialize(fake_env) assert config.for_report == {"log_level": "WARN", "report_interval_seconds": 10} config.update({"LOG_LEVEL": "ERROR", "REPORT_INTERVAL_SECONDS": 20}) @@ -157,7 +157,7 @@ def test_update(self): "QUEUES": ["default", "high"], }, } - config = Config.for_heroku(fake_env) + config = Config.initialize(fake_env) assert config["API_BASE_URL"] == "https://api.example.com" assert config["RUNTIME_CONTAINER"] == "worker.1" assert config["LOG_LEVEL"] == "WARN" @@ -188,7 +188,7 @@ def test_update_lowercase_keys(self): "QUEUES": ["default", "high"], }, } - config = Config.for_heroku(fake_env) + config = Config.initialize(fake_env) assert config["LOG_LEVEL"] == "WARN" assert config["REPORT_INTERVAL_SECONDS"] == 10 From 3c49150fb9ae694388a43aee16a83cfd3811d6c1 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Mon, 2 Feb 2026 15:43:13 -0300 Subject: [PATCH 3/3] Move RuntimeContainer init to single place --- judoscale/core/config.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/judoscale/core/config.py b/judoscale/core/config.py index 49ab65c..8cc1253 100644 --- a/judoscale/core/config.py +++ b/judoscale/core/config.py @@ -44,24 +44,22 @@ def __init__(self, runtime_container: RuntimeContainer, env: Mapping): @classmethod def initialize(cls, env: Mapping = os.environ): if env.get("JUDOSCALE_CONTAINER"): - runtime_container = RuntimeContainer(env["JUDOSCALE_CONTAINER"]) + container = env["JUDOSCALE_CONTAINER"] elif env.get("DYNO"): - runtime_container = RuntimeContainer(env["DYNO"]) + container = env["DYNO"] elif env.get("RENDER_INSTANCE_ID"): service_id = env.get("RENDER_SERVICE_ID") - instance = env["RENDER_INSTANCE_ID"].replace(f"{service_id}-", "") - runtime_container = RuntimeContainer(instance) + container = env["RENDER_INSTANCE_ID"].replace(f"{service_id}-", "") elif env.get("ECS_CONTAINER_METADATA_URI"): - instance = env["ECS_CONTAINER_METADATA_URI"].split("/")[-1] - runtime_container = RuntimeContainer(instance) + container = env["ECS_CONTAINER_METADATA_URI"].split("/")[-1] elif env.get("FLY_MACHINE_ID"): - runtime_container = RuntimeContainer(env["FLY_MACHINE_ID"]) + container = env["FLY_MACHINE_ID"] elif env.get("RAILWAY_REPLICA_ID"): - runtime_container = RuntimeContainer(env["RAILWAY_REPLICA_ID"]) + container = env["RAILWAY_REPLICA_ID"] else: - runtime_container = RuntimeContainer("") + container = "" - config = cls(runtime_container, env) + config = cls(RuntimeContainer(container), env) # Render legacy support: fall back to constructing URL from service ID if not config["API_BASE_URL"] and env.get("RENDER_SERVICE_ID"):