Fall back to disk when in-memory store is empty on client connect
storage_helpers: load_today_records() scans logs/ for all files matching today's UTC date and VIN, merges and sorts their records. network: _accept_loop receives an optional load_today callable; if store_ref["current"] has no records it calls load_today() to replay the day's history from disk before adding the client. collect: passes a load_today lambda (None when -o is used). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+8
-3
@@ -57,7 +57,7 @@ from pathlib import Path
|
||||
|
||||
from server import log_config, network, we_connect
|
||||
from server.data_model import ALL_DOMAINS, collect_snapshot
|
||||
from server.storage_helpers import auto_out_path, load_store, save_store
|
||||
from server.storage_helpers import auto_out_path, load_store, load_today_records, save_store
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -93,10 +93,14 @@ def run(args: argparse.Namespace) -> None:
|
||||
logs_dir.mkdir(exist_ok=True)
|
||||
out = auto_out_path(logs_dir, vin)
|
||||
|
||||
push_clients, push_lock, push_last = network.start_push_server(args.host, args.port)
|
||||
load_today = (lambda: load_today_records(logs_dir, vin)) if logs_dir else None
|
||||
push_clients, push_lock, push_store_ref = network.start_push_server(
|
||||
args.host, args.port, load_today=load_today
|
||||
)
|
||||
|
||||
current_day = datetime.now(timezone.utc).date()
|
||||
store = load_store(out, vin, args.interval)
|
||||
push_store_ref["current"] = store
|
||||
log.info(
|
||||
"Collecting [%s] every %ds → %s (Ctrl-C to stop)",
|
||||
", ".join(domains),
|
||||
@@ -111,6 +115,7 @@ def run(args: argparse.Namespace) -> None:
|
||||
if today != current_day:
|
||||
out = auto_out_path(logs_dir, vin)
|
||||
store = load_store(out, vin, args.interval)
|
||||
push_store_ref["current"] = store
|
||||
current_day = today
|
||||
log.info("Midnight UTC rotation → %s", out)
|
||||
|
||||
@@ -119,7 +124,7 @@ def run(args: argparse.Namespace) -> None:
|
||||
snapshot = collect_snapshot(vehicle, domains)
|
||||
store["records"].append(snapshot)
|
||||
save_store(out, store, args.max_records)
|
||||
network.broadcast(snapshot, push_clients, push_lock, push_last)
|
||||
network.broadcast(snapshot, push_clients, push_lock)
|
||||
log.info("Record #%d saved at %s", len(store["records"]), snapshot["ts"])
|
||||
except KeyboardInterrupt:
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user