fix: ensure scheduler starts only once during Flask requests
This commit is contained in:
23
web/app.py
23
web/app.py
@@ -1,4 +1,5 @@
|
||||
import os
|
||||
import threading
|
||||
from flask import Flask, request, jsonify, render_template, redirect, url_for, session, flash, Response
|
||||
from flask_wtf import CSRFProtect
|
||||
from typing import Dict, List
|
||||
@@ -60,6 +61,10 @@ app.static_folder = "static"
|
||||
csrf = CSRFProtect(app)
|
||||
|
||||
|
||||
_scheduler_started = False
|
||||
_scheduler_lock = threading.Lock()
|
||||
|
||||
|
||||
def _scheduler_enabled() -> bool:
|
||||
flag = (os.environ.get("SCRAPE_SCHEDULER_ENABLED") or "").strip().lower()
|
||||
if flag not in {"1", "true", "yes", "on"}:
|
||||
@@ -74,10 +79,22 @@ def _scheduler_enabled() -> bool:
|
||||
return True
|
||||
|
||||
|
||||
@app.before_first_request
|
||||
def _start_scheduler_if_enabled():
|
||||
if _scheduler_enabled():
|
||||
def _maybe_start_scheduler() -> None:
|
||||
global _scheduler_started
|
||||
if _scheduler_started:
|
||||
return
|
||||
if not _scheduler_enabled():
|
||||
return
|
||||
with _scheduler_lock:
|
||||
if _scheduler_started:
|
||||
return
|
||||
start_scheduler_in_background()
|
||||
_scheduler_started = True
|
||||
|
||||
|
||||
@app.before_request
|
||||
def _start_scheduler_if_enabled():
|
||||
_maybe_start_scheduler()
|
||||
|
||||
|
||||
def require_admin():
|
||||
|
||||
Reference in New Issue
Block a user