fix: ensure scheduler starts only once during Flask requests
Some checks failed
CI/CD Pipeline / test (push) Successful in 1m34s
CI/CD Pipeline / build-image (push) Failing after 1m47s

This commit is contained in:
2026-01-22 16:55:18 +01:00
parent d84b8f128b
commit 446c432c18
2 changed files with 40 additions and 3 deletions

View File

@@ -153,3 +153,23 @@ class TestScheduler:
assert search_data.get(("losangeles", "python")) == 4
assert search_data.get(("losangeles", "java")) == 1
assert result.get("discovered") == 10 # Total unique jobs
def test_app_scheduler_starts_once(monkeypatch):
"""Ensure the Flask before_request hook starts scheduler only once."""
import web.app as app_module
monkeypatch.setenv("SCRAPE_SCHEDULER_ENABLED", "true")
monkeypatch.delenv("SERVER_SOFTWARE", raising=False)
monkeypatch.delenv("FLASK_RUN_FROM_CLI", raising=False)
monkeypatch.delenv("WERKZEUG_RUN_MAIN", raising=False)
app_module._scheduler_started = False
with patch("web.app.start_scheduler_in_background") as mock_start:
app_module.app.config.update(TESTING=True, WTF_CSRF_ENABLED=False)
with app_module.app.test_client() as client:
client.get("/health")
client.get("/health")
assert mock_start.call_count == 1