This commit is contained in:
2026-05-02 01:23:30 +03:00
parent 1c65043eb3
commit 502b330ac5

View File

@@ -1,6 +1,6 @@
import json import json
import logging import logging
from datetime import datetime from datetime import datetime, timedelta
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@@ -79,21 +79,21 @@ def evaluate_blueprints(conn):
log.warning(f"[blueprint] buildData keys: {list(build_data.keys())}") log.warning(f"[blueprint] buildData keys: {list(build_data.keys())}")
log.warning(f"[blueprint] buildingOrder length: {len(build_queue)}") log.warning(f"[blueprint] buildingOrder length: {len(build_queue)}")
# Don't queue anything if there's already a valid pending/executing command in DB # Don't queue anything if there's already a valid pending/executing command in DB.
# However, if a pending command has been sitting untouched for >5 minutes it's
# a ghost (inserted by an old broken blueprint run) — delete it and re-evaluate.
five_min_ago = (datetime.utcnow() - timedelta(minutes=5)).isoformat()
db_pending = conn.execute( db_pending = conn.execute(
"SELECT id, type, status, updated_at FROM commands WHERE town_id = ? AND type IN ('build', 'research') AND status IN ('pending', 'executing')", "SELECT id, type, status, updated_at FROM commands WHERE town_id = ? AND type IN ('build', 'research') AND status IN ('pending', 'executing')",
(town_id,) (town_id,)
).fetchall() ).fetchall()
if db_pending: if db_pending:
# Check if any of these are malformed (NULL updated_at = created by old broken blueprint engine) ghost_ids = [r['id'] for r in db_pending if r['status'] == 'pending' and (r['updated_at'] is None or r['updated_at'] < five_min_ago)]
# Auto-delete those so we can re-insert them correctly if ghost_ids:
broken_ids = [r['id'] for r in db_pending if r['updated_at'] is None] log.warning(f"[blueprint] Deleting {len(ghost_ids)} ghost pending commands {ghost_ids} for town {town_id}")
if broken_ids: conn.execute(f"DELETE FROM commands WHERE id IN ({','.join('?' for _ in ghost_ids)})", ghost_ids)
log.warning(f"[blueprint] Deleting {len(broken_ids)} malformed stuck commands {broken_ids} for town {town_id}")
conn.execute(f"DELETE FROM commands WHERE id IN ({','.join('?' for _ in broken_ids)})", broken_ids)
conn.commit() conn.commit()
# Re-check if there are still real pending commands
db_pending = conn.execute( db_pending = conn.execute(
"SELECT id, type, status, updated_at FROM commands WHERE town_id = ? AND type IN ('build', 'research') AND status IN ('pending', 'executing')", "SELECT id, type, status, updated_at FROM commands WHERE town_id = ? AND type IN ('build', 'research') AND status IN ('pending', 'executing')",
(town_id,) (town_id,)