Mbonea commited on
Commit
0810746
·
1 Parent(s): 2e54aff

Sync all assets on startup if last market sync is older than 3 days

Browse files

Wires the existing startup_refresh() into startup (it was defined but
never called). Adds a STARTUP_FORCE_REFRESH_AFTER_DAYS=3 guard: when the
last DSE snapshot (StockProfile.updated_at) is older than 3 days, force a
full refresh of stocks + funds + bonds regardless of per-dataset
staleness or weekend. Enriched the startup-refresh log line with
last_sync_age and force_refresh_all.

Files changed (2) hide show
  1. App/scheduler.py +23 -5
  2. main.py +3 -0
App/scheduler.py CHANGED
@@ -32,6 +32,10 @@ STOCK_PRICE_REFRESH_END_HOUR = 17 # exclusive — last run starts at 16:00
32
  STOCK_STALE_AFTER_DAYS = 2
33
  FUND_STALE_AFTER_DAYS = 2
34
  BOND_STALE_AFTER_DAYS = 14
 
 
 
 
35
  NEWS_ARTICLE_STALE_HOURS = 12
36
  NEWS_ARTICLES_PER_QUERY = 4
37
  ECONOMY_RUN_GUARD_SECONDS = 60 * 60
@@ -666,15 +670,26 @@ async def startup_refresh() -> None:
666
  await _stock_history_backfill_needed()
667
  )
668
 
 
 
 
 
 
 
 
 
 
 
669
  needs_stocks = (
670
- stock_age is None
 
671
  or stock_age > STOCK_STALE_AFTER_DAYS
672
  or stock_history_backfill_needed
673
  )
674
- needs_funds = fund_age is None or fund_age > FUND_STALE_AFTER_DAYS
675
- needs_bonds = bond_age is None or bond_age > BOND_STALE_AFTER_DAYS
676
 
677
- if _is_weekend(now):
678
  weekend_needs_stocks = stock_age is None
679
  weekend_needs_funds = fund_age is None
680
  weekend_needs_bonds = bond_age is None
@@ -697,8 +712,11 @@ async def startup_refresh() -> None:
697
  needs_stocks = True
698
 
699
  logger.info(
700
- "[startup-refresh] dataset status: weekend=%s stocks_age_days=%s stale_after=%s history_backfill_needed=%s history_backfill_info=%s needs=%s, funds_age_days=%s stale_after=%s needs=%s, bonds_age_days=%s stale_after=%s needs=%s",
701
  _is_weekend(now),
 
 
 
702
  stock_age,
703
  STOCK_STALE_AFTER_DAYS,
704
  stock_history_backfill_needed,
 
32
  STOCK_STALE_AFTER_DAYS = 2
33
  FUND_STALE_AFTER_DAYS = 2
34
  BOND_STALE_AFTER_DAYS = 14
35
+ # If the last market sync (StockProfile.updated_at — the DSE snapshot scrape
36
+ # timestamp) is older than this on startup, force a full refresh of every
37
+ # asset class regardless of per-dataset staleness.
38
+ STARTUP_FORCE_REFRESH_AFTER_DAYS = 3
39
  NEWS_ARTICLE_STALE_HOURS = 12
40
  NEWS_ARTICLES_PER_QUERY = 4
41
  ECONOMY_RUN_GUARD_SECONDS = 60 * 60
 
670
  await _stock_history_backfill_needed()
671
  )
672
 
673
+ # Force a full refresh of every asset class if the last market sync
674
+ # (StockProfile.updated_at, the DSE snapshot timestamp) is older than
675
+ # STARTUP_FORCE_REFRESH_AFTER_DAYS — e.g. the server was down over a
676
+ # long weekend / multiple days.
677
+ last_sync_age = _age_days(
678
+ latest_stock_profile.updated_at if latest_stock_profile else None,
679
+ today,
680
+ )
681
+ force_refresh_all = last_sync_age is not None and last_sync_age > STARTUP_FORCE_REFRESH_AFTER_DAYS
682
+
683
  needs_stocks = (
684
+ force_refresh_all
685
+ or stock_age is None
686
  or stock_age > STOCK_STALE_AFTER_DAYS
687
  or stock_history_backfill_needed
688
  )
689
+ needs_funds = force_refresh_all or fund_age is None or fund_age > FUND_STALE_AFTER_DAYS
690
+ needs_bonds = force_refresh_all or bond_age is None or bond_age > BOND_STALE_AFTER_DAYS
691
 
692
+ if _is_weekend(now) and not force_refresh_all:
693
  weekend_needs_stocks = stock_age is None
694
  weekend_needs_funds = fund_age is None
695
  weekend_needs_bonds = bond_age is None
 
712
  needs_stocks = True
713
 
714
  logger.info(
715
+ "[startup-refresh] dataset status: weekend=%s last_sync_age_days=%s force_refresh_all=%s (threshold=%s), stocks_age_days=%s stale_after=%s history_backfill_needed=%s history_backfill_info=%s needs=%s, funds_age_days=%s stale_after=%s needs=%s, bonds_age_days=%s stale_after=%s needs=%s",
716
  _is_weekend(now),
717
+ last_sync_age,
718
+ force_refresh_all,
719
+ STARTUP_FORCE_REFRESH_AFTER_DAYS,
720
  stock_age,
721
  STOCK_STALE_AFTER_DAYS,
722
  stock_history_backfill_needed,
main.py CHANGED
@@ -21,6 +21,7 @@ from App.scheduler import (
21
  scheduled_market_loop,
22
  scheduled_news_loop,
23
  scheduled_stock_price_loop,
 
24
  )
25
 
26
  from db import init_db, close_db, clear_db
@@ -118,6 +119,8 @@ async def startup_event():
118
  logger.info("[startup] economy pipeline loop registered")
119
  asyncio.create_task(scheduled_stock_price_loop())
120
  logger.info("[startup] intraday stock-price loop started (hourly during DSE trading hours)")
 
 
121
 
122
 
123
  @app.on_event("shutdown")
 
21
  scheduled_market_loop,
22
  scheduled_news_loop,
23
  scheduled_stock_price_loop,
24
+ startup_refresh,
25
  )
26
 
27
  from db import init_db, close_db, clear_db
 
119
  logger.info("[startup] economy pipeline loop registered")
120
  asyncio.create_task(scheduled_stock_price_loop())
121
  logger.info("[startup] intraday stock-price loop started (hourly during DSE trading hours)")
122
+ asyncio.create_task(startup_refresh())
123
+ logger.info("[startup] startup refresh registered (syncs all assets if last market sync > 3 days)")
124
 
125
 
126
  @app.on_event("shutdown")