Fred808 commited on
Commit
7725164
·
verified ·
1 Parent(s): e501fe8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -13
app.py CHANGED
@@ -509,33 +509,47 @@ async def startup_event():
509
 
510
  async def initialize_after_startup():
511
  """Initialize app components after server has started"""
 
 
512
  try:
 
 
513
  # Load state with timeout
514
  try:
515
- async with asyncio.timeout(30):
516
- await load_state_from_hf()
 
517
  except asyncio.TimeoutError:
518
  print("WARNING: Timeout loading state from HF, using default state")
 
 
519
 
520
  # Update file list with timeout
521
  try:
522
- async with asyncio.timeout(60):
523
- await update_file_list()
 
524
  except asyncio.TimeoutError:
525
  print("WARNING: Timeout updating file list, using existing state")
 
 
526
 
527
  # Start processing if needed
528
- if state.current_index < state.total_files:
529
- state.is_running = True
530
- asyncio.create_task(process_next_file_task())
531
- print(f"[{FLOW_ID}] Started processing from index {state.current_index}")
532
- else:
533
- state.is_running = False
534
- print(f"[{FLOW_ID}] All files processed. Starting in Idle mode.")
 
535
 
536
  except Exception as e:
537
- print(f"[{FLOW_ID}] Error during initialization: {e}")
538
- state.is_running = False
 
 
 
539
 
540
 
541
  @app.get("/", response_class=HTMLResponse)
 
509
 
510
  async def initialize_after_startup():
511
  """Initialize app components after server has started"""
512
+ global state
513
+
514
  try:
515
+ print(f"[{FLOW_ID}] Starting initialization...")
516
+
517
  # Load state with timeout
518
  try:
519
+ print(f"[{FLOW_ID}] Loading state from HF...")
520
+ await asyncio.wait_for(load_state_from_hf(), timeout=30.0)
521
+ print(f"[{FLOW_ID}] State loaded successfully")
522
  except asyncio.TimeoutError:
523
  print("WARNING: Timeout loading state from HF, using default state")
524
+ except Exception as e:
525
+ print(f"WARNING: Error loading state from HF: {e}, using default state")
526
 
527
  # Update file list with timeout
528
  try:
529
+ print(f"[{FLOW_ID}] Updating file list...")
530
+ await asyncio.wait_for(update_file_list(), timeout=60.0)
531
+ print(f"[{FLOW_ID}] File list updated successfully")
532
  except asyncio.TimeoutError:
533
  print("WARNING: Timeout updating file list, using existing state")
534
+ except Exception as e:
535
+ print(f"WARNING: Error updating file list: {e}, using existing state")
536
 
537
  # Start processing if needed
538
+ async with state_lock:
539
+ if state.current_index < state.total_files:
540
+ state.is_running = True
541
+ asyncio.create_task(process_next_file_task())
542
+ print(f"[{FLOW_ID}] Started processing from index {state.current_index}/{state.total_files}")
543
+ else:
544
+ state.is_running = False
545
+ print(f"[{FLOW_ID}] All files processed. Starting in Idle mode.")
546
 
547
  except Exception as e:
548
+ print(f"[{FLOW_ID}] Critical error during initialization: {e}")
549
+ import traceback
550
+ traceback.print_exc()
551
+ async with state_lock:
552
+ state.is_running = False
553
 
554
 
555
  @app.get("/", response_class=HTMLResponse)