Jitendra12421 commited on
Commit
47c8bfc
·
verified ·
1 Parent(s): 973ade9

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -4
app.py CHANGED
@@ -17,6 +17,8 @@ PREDICTIONS_FILE_T5 = os.path.join(os.path.dirname(__file__), "predictions_t5.js
17
 
18
  app = FastAPI(title="HF NIFTY Forecaster Backend")
19
 
 
 
20
  app.add_middleware(
21
  CORSMiddleware,
22
  allow_origins=["*"],
@@ -24,6 +26,36 @@ app.add_middleware(
24
  allow_headers=["*"],
25
  )
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  def run_update_pipeline(is_t5=False):
28
  try:
29
  if is_t5:
@@ -80,10 +112,6 @@ def cron_trigger(background_tasks: BackgroundTasks):
80
  # Run T+5 pipeline at 09:21
81
  background_tasks.add_task(run_update_pipeline, is_t5=True)
82
  return {"status": "triggered", "message": "Morning T+5 forecast pipeline started in the background."}
83
- elif not os.path.exists(PREDICTIONS_FILE_T5):
84
- # Fallback: if file is missing (e.g. container restart), generate T+5 immediately
85
- background_tasks.add_task(run_update_pipeline, is_t5=True)
86
- return {"status": "triggered", "message": "Missing T+5 predictions. Generating them now in the background."}
87
  else:
88
  return {"status": "skipped", "reason": f"Current time {current_time} is not in the execution windows (09:21-10:00 for T+5, after 15:45 for T+1)."}
89
 
 
17
 
18
  app = FastAPI(title="HF NIFTY Forecaster Backend")
19
 
20
+ import threading
21
+
22
  app.add_middleware(
23
  CORSMiddleware,
24
  allow_origins=["*"],
 
26
  allow_headers=["*"],
27
  )
28
 
29
+ @app.on_event("startup")
30
+ def on_startup():
31
+ def startup_check():
32
+ now = datetime.now(IST)
33
+ today = now.date()
34
+ current_time = now.time()
35
+
36
+ if not is_trading_day(today):
37
+ return
38
+
39
+ # If past 09:21 and T+5 predictions are missing, generate them
40
+ if current_time >= T5_RUN_BUFFER and not os.path.exists(PREDICTIONS_FILE_T5):
41
+ print("Startup: T+5 predictions missing and it's past 09:21. Backfilling...")
42
+ try:
43
+ generate_t5_predictions()
44
+ except Exception as e:
45
+ print(f"Startup T+5 generation failed: {e}")
46
+
47
+ # If past 15:45 and EOD predictions are missing, generate them
48
+ if current_time >= MARKET_CLOSE_BUFFER and not os.path.exists(PREDICTIONS_FILE):
49
+ print("Startup: EOD predictions missing and it's past 15:45. Backfilling...")
50
+ try:
51
+ update_daily_data()
52
+ generate_predictions()
53
+ except Exception as e:
54
+ print(f"Startup EOD generation failed: {e}")
55
+
56
+ # Run in a background thread so it doesn't block Uvicorn startup
57
+ threading.Thread(target=startup_check, daemon=True).start()
58
+
59
  def run_update_pipeline(is_t5=False):
60
  try:
61
  if is_t5:
 
112
  # Run T+5 pipeline at 09:21
113
  background_tasks.add_task(run_update_pipeline, is_t5=True)
114
  return {"status": "triggered", "message": "Morning T+5 forecast pipeline started in the background."}
 
 
 
 
115
  else:
116
  return {"status": "skipped", "reason": f"Current time {current_time} is not in the execution windows (09:21-10:00 for T+5, after 15:45 for T+1)."}
117