Spaces:
Running
Running
Commit ·
f9b1ae4
1
Parent(s): dfde933
Fix: run startup refresh in WSGI mode
Browse files
webapp.py
CHANGED
|
@@ -237,7 +237,7 @@ def health():
|
|
| 237 |
return jsonify({"status": "unhealthy", "message": cached_status}), 503
|
| 238 |
|
| 239 |
|
| 240 |
-
# ----
|
| 241 |
|
| 242 |
_cache_data = _load_cache()
|
| 243 |
if _cache_data:
|
|
@@ -252,31 +252,29 @@ if _cache_data:
|
|
| 252 |
pass
|
| 253 |
logger.info("Loaded cached data: %s", cached_status)
|
| 254 |
|
| 255 |
-
|
|
|
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
# ---- Entry points ----
|
| 278 |
|
| 279 |
if __name__ == "__main__":
|
| 280 |
-
_start_scheduler()
|
| 281 |
-
threading.Thread(target=refresh_data, daemon=True).start()
|
| 282 |
app.run(host=cfg.host, port=cfg.port, debug=(cfg.flask_env != "production"))
|
|
|
|
| 237 |
return jsonify({"status": "unhealthy", "message": cached_status}), 503
|
| 238 |
|
| 239 |
|
| 240 |
+
# ---- Startup (runs in both WSGI and dev modes) ----
|
| 241 |
|
| 242 |
_cache_data = _load_cache()
|
| 243 |
if _cache_data:
|
|
|
|
| 252 |
pass
|
| 253 |
logger.info("Loaded cached data: %s", cached_status)
|
| 254 |
|
| 255 |
+
if not cached_results:
|
| 256 |
+
threading.Thread(target=refresh_data, daemon=True).start()
|
| 257 |
|
| 258 |
+
try:
|
| 259 |
+
from apscheduler.schedulers.background import BackgroundScheduler
|
| 260 |
+
_scheduler = BackgroundScheduler(daemon=True)
|
| 261 |
+
_scheduler.add_job(
|
| 262 |
+
refresh_data,
|
| 263 |
+
trigger="cron",
|
| 264 |
+
hour=cfg.refresh_hour,
|
| 265 |
+
minute=cfg.refresh_minute,
|
| 266 |
+
timezone=cfg.refresh_timezone,
|
| 267 |
+
id="daily_refresh",
|
| 268 |
+
name="Daily news refresh",
|
| 269 |
+
replace_existing=True,
|
| 270 |
+
)
|
| 271 |
+
_scheduler.start()
|
| 272 |
+
logger.info("Scheduler started — daily refresh at %02d:%02d %s", cfg.refresh_hour, cfg.refresh_minute, cfg.refresh_timezone)
|
| 273 |
+
except ImportError:
|
| 274 |
+
logger.warning("APScheduler not available — scheduled refresh disabled")
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
# ---- Dev server (not used in WSGI/gunicorn) ----
|
|
|
|
| 278 |
|
| 279 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 280 |
app.run(host=cfg.host, port=cfg.port, debug=(cfg.flask_env != "production"))
|