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 logging
from datetime import datetime
from datetime import datetime, timedelta
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] 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(
"SELECT id, type, status, updated_at FROM commands WHERE town_id = ? AND type IN ('build', 'research') AND status IN ('pending', 'executing')",
(town_id,)
).fetchall()
if db_pending:
# Check if any of these are malformed (NULL updated_at = created by old broken blueprint engine)
# Auto-delete those so we can re-insert them correctly
broken_ids = [r['id'] for r in db_pending if r['updated_at'] is None]
if broken_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)
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)]
if ghost_ids:
log.warning(f"[blueprint] Deleting {len(ghost_ids)} ghost pending commands {ghost_ids} for town {town_id}")
conn.execute(f"DELETE FROM commands WHERE id IN ({','.join('?' for _ in ghost_ids)})", ghost_ids)
conn.commit()
# Re-check if there are still real pending commands
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')",
(town_id,)