From 26daddf909044d55cc1ff47ed040579d01c49787 Mon Sep 17 00:00:00 2001 From: host Date: Mon, 26 Oct 2026 07:02:31 +0800 Subject: [PATCH] hotfix: autoscaler main() setup_logging arg setup_logging() takes a log LEVEL, not a logger name; passing 'cloud.autoscaler' raised ValueError at startup, so the scaler crash-looped under systemd and started zero workers (the host had no worker at all until this fix). The '/opt/.../-m' FileNotFoundError in the same journal lines is only Ubuntu's apport hook misfiring on 'python3 -m', not a second bug. Regression test: main() with mocked run_loop/signal must reach the loop (this would have caught the crash before deploy). --- cloud/autoscaler.py | 2 +- cloud/tests/test_autoscaler.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cloud/autoscaler.py b/cloud/autoscaler.py index 20e19f2..fdd72e7 100644 --- a/cloud/autoscaler.py +++ b/cloud/autoscaler.py @@ -384,7 +384,7 @@ def main(argv=None): print("autoscaler 需要 --db(或 worker 参数里的 --db)", file=sys.stderr) raise SystemExit(2) - setup_logging("cloud.autoscaler") + setup_logging() scaler = Autoscaler( db_path, worker_args, min_workers=args.min_workers, max_workers=args.max_workers, diff --git a/cloud/tests/test_autoscaler.py b/cloud/tests/test_autoscaler.py index 24b072c..d69834a 100644 --- a/cloud/tests/test_autoscaler.py +++ b/cloud/tests/test_autoscaler.py @@ -160,6 +160,22 @@ class AutoscalerReconcileTestCase(unittest.TestCase): self.assertIn("--db", cmd) self.assertEqual(cmd[cmd.index("--db") + 1], self.db_path) + def test_main_parses_args_and_starts_loop(self): + # 回归:main() 里 setup_logging 曾错传 logger 名当 level, + # 直接 ValueError,scaler 在主机上 crash-loop、一个 worker + # 都没拉起来。本测试 mock 掉 run_loop 与 signal,只验证 + # main() 能走到启动循环(即参数解析与日志初始化没炸)。 + from unittest import mock + + import cloud.autoscaler as autoscaler_mod + with mock.patch.object( + autoscaler_mod.Autoscaler, "run_loop") as mock_loop, \ + mock.patch("signal.signal"): + autoscaler_mod.main([ + "--db", self.db_path, "--interval", "30", + "--", "--interval", "10"]) + mock_loop.assert_called_once() + if __name__ == "__main__": unittest.main()