Skip to content

Commit 109fc74

Browse files
authored
Merge pull request #2 from Yanu403/fix/autostart-managed-bot
feat: support managed bot autostart
2 parents 131aa48 + d8a0d2c commit 109fc74

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ METRICS_PATH=data/runtime-metrics.json
3737
# === Dashboard (optional) ===
3838
# API key for dashboard endpoints. If empty, dashboard API routes are disabled.
3939
DASHBOARD_API_KEY=
40+
# Start the dashboard-managed bot during application startup (production opt-in).
41+
DIGITAL_MATE_AUTO_START_BOT=false

digital_mate/web/app.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ async def lifespan(_: FastAPI) -> AsyncIterator[None]:
179179
db = await init_db(str(abs_db_path))
180180
_brand_service = BrandService(db)
181181
_stats_service = StatsService(db)
182+
auto_start_bot = os.environ.get(
183+
"DIGITAL_MATE_AUTO_START_BOT", ""
184+
).strip().lower() in {"1", "true", "yes", "on"}
185+
if auto_start_bot:
186+
await bot_manager.start()
182187
try:
183188
yield
184189
finally:

tests/test_web.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ async def test_start_inherits_service_output(self):
4141
assert "stdout" not in await_args.kwargs
4242
assert "stderr" not in await_args.kwargs
4343

44+
def test_lifespan_autostarts_bot_when_enabled(self, tmp_path, monkeypatch):
45+
monkeypatch.setenv("DASHBOARD_API_KEY", DASHBOARD_TEST_KEY)
46+
monkeypatch.setenv("DIGITAL_MATE_AUTO_START_BOT", "true")
47+
48+
with (
49+
patch("digital_mate.web.app.PROJECT_ROOT", tmp_path),
50+
patch(
51+
"digital_mate.web.app.bot_manager.start", new=AsyncMock()
52+
) as start_bot,
53+
patch(
54+
"digital_mate.web.app.bot_manager.stop", new=AsyncMock()
55+
) as stop_bot,
56+
):
57+
from digital_mate.web.app import create_app
58+
59+
with TestClient(create_app()):
60+
start_bot.assert_awaited_once_with()
61+
62+
stop_bot.assert_awaited_once_with()
63+
4464

4565
# ---------------------------------------------------------------------------
4666
# Settings Service Tests

0 commit comments

Comments
 (0)