bep40 commited on
Commit
5c8ae5f
·
verified ·
1 Parent(s): 1707f8b

Refactor: _run_scheduled_sync helper + background non-blocking trigger

Browse files
Files changed (1) hide show
  1. app_v2_entry.py +17 -20
app_v2_entry.py CHANGED
@@ -1470,13 +1470,8 @@ def _scheduler_loop():
1470
  should_run = True
1471
  if should_run and label not in today_log:
1472
  print(f"[auto] Detected missed slot {label} (h={h} < now={current_hour}:{current_minute}), running catch-up now")
1473
- loop = asyncio.new_event_loop()
1474
- asyncio.set_event_loop(loop)
1475
- try:
1476
- loop.run_until_complete(_do_scheduled_run(label))
1477
- _last_run_slots.add(label)
1478
- finally:
1479
- loop.close()
1480
  except Exception as e:
1481
  print(f"[auto] Catch-up check error: {e}")
1482
 
@@ -1499,12 +1494,7 @@ def _scheduler_loop():
1499
 
1500
  if slot and slot not in _last_run_slots:
1501
  _last_run_slots.add(slot)
1502
- loop = asyncio.new_event_loop()
1503
- asyncio.set_event_loop(loop)
1504
- try:
1505
- loop.run_until_complete(_do_scheduled_run(slot))
1506
- finally:
1507
- loop.close()
1508
  except Exception as e:
1509
  print(f"[auto] Loop error: {e}")
1510
 
@@ -1531,14 +1521,21 @@ async def debug_auto_schedule(slot: str = '07:00'):
1531
  except Exception as e:
1532
  return JSONResponse({"error": str(e)}, status_code=500)
1533
 
1534
- @app.get('/api/debug/trigger_auto')
1535
- async def debug_trigger_auto(slot: str = '19:00'):
1536
- """Trigger _do_scheduled_run and return result."""
 
1537
  try:
1538
- await _do_scheduled_run(slot)
1539
- return JSONResponse({"status": "ok", "slot": slot})
1540
  except Exception as e:
1541
- import traceback
1542
- return JSONResponse({"error": str(e), "traceback": traceback.format_exc()}, status_code=500)
 
 
 
 
 
 
 
1543
 
1544
  app.mount('/static',StaticFiles(directory=STATIC_DIR),name='vnews_static')
 
1470
  should_run = True
1471
  if should_run and label not in today_log:
1472
  print(f"[auto] Detected missed slot {label} (h={h} < now={current_hour}:{current_minute}), running catch-up now")
1473
+ _run_scheduled_sync(label)
1474
+ _last_run_slots.add(label)
 
 
 
 
 
1475
  except Exception as e:
1476
  print(f"[auto] Catch-up check error: {e}")
1477
 
 
1494
 
1495
  if slot and slot not in _last_run_slots:
1496
  _last_run_slots.add(slot)
1497
+ _run_scheduled_sync(slot)
 
 
 
 
 
1498
  except Exception as e:
1499
  print(f"[auto] Loop error: {e}")
1500
 
 
1521
  except Exception as e:
1522
  return JSONResponse({"error": str(e)}, status_code=500)
1523
 
1524
+ def _run_scheduled_sync(slot):
1525
+ """Run _do_scheduled_run in a separate event loop (for background thread)."""
1526
+ loop = asyncio.new_event_loop()
1527
+ asyncio.set_event_loop(loop)
1528
  try:
1529
+ loop.run_until_complete(_do_scheduled_run(slot))
 
1530
  except Exception as e:
1531
+ print(f"[auto] Background run error: {e}")
1532
+ finally:
1533
+ loop.close()
1534
+
1535
+ @app.get('/api/debug/trigger_auto')
1536
+ async def debug_trigger_auto(slot: str = '19:00'):
1537
+ """Trigger _do_scheduled_run in background thread (non-blocking)."""
1538
+ threading.Thread(target=_run_scheduled_sync, args=(slot,), daemon=True).start()
1539
+ return JSONResponse({"status": "started", "slot": slot})
1540
 
1541
  app.mount('/static',StaticFiles(directory=STATIC_DIR),name='vnews_static')