fix: preserve strategy intent and recover accounts automatically

This commit is contained in:
AttilaNet
2026-11-11 19:37:15 +08:00
parent ccb945631d
commit 5c253d5cf9
26 changed files with 1628 additions and 236 deletions
+43 -14
View File
@@ -1,6 +1,7 @@
"""Desktop scheduling semantics without Qt timers or concurrent Engine access."""
from __future__ import annotations
import logging
import math
import random
import threading
@@ -8,6 +9,7 @@ import time
FAST_KINDS = ("hatch", "gift", "explore", "breakthrough")
logger = logging.getLogger(__name__)
class AccountSchedule:
@@ -17,6 +19,7 @@ class AccountSchedule:
self.clock, self.uniform = clock, uniform
self.wall_clock = wall_clock
self.next_poll = math.inf
self._poll_deferred = False
self.next_fast = {kind: math.inf for kind in FAST_KINDS}
self.monitoring = False
self.last_maintenance_error = {}
@@ -43,7 +46,7 @@ class AccountSchedule:
return None
origin = max(self.disconnected_at, self.auto_relogin_failed_at
if self.auto_relogin_failed_at is not None else self.disconnected_at)
return origin + preference["delay_minutes"] * 60
return origin + preference.get("delay_minutes", 30) * 60
def auto_relogin_failed(self):
# Back off from completion, including time spent authenticating.
@@ -59,15 +62,21 @@ class AccountSchedule:
self.retry_deadline = self.clock() + delay
return delay
def game_resumed(self):
def game_resumed(self, *, clear_disconnection=True):
"""Resume monitored reads; the service decides if outage evidence clears."""
was_paused = self.game_paused
self.game_paused = False
self.retry_deadline = None
self.retry_attempt = 0
self.clear_disconnection()
if clear_disconnection:
self.clear_disconnection()
if self.monitoring and (was_paused or self.next_poll == math.inf):
self.next_poll = min(self.next_poll, self.clock())
def monitor(self, enabled=True):
self.monitoring = bool(enabled)
self.next_poll = self.clock() if enabled else math.inf
self._poll_deferred = False
def stop(self):
for kind in FAST_KINDS:
@@ -75,6 +84,7 @@ class AccountSchedule:
def poll_finished(self, config):
self.next_poll = self.clock() + self.uniform(0, config.get("interval", 30))
self._poll_deferred = False
def maintenance_failed(self, kind):
self.last_maintenance_error[kind] = self.clock()
@@ -83,7 +93,7 @@ class AccountSchedule:
if completed in self.next_fast:
self.next_fast[completed] = math.inf
snapshot = engine.snapshot
if not self.monitoring or not running or snapshot is None:
if not self.monitoring or self.game_paused or not running or snapshot is None:
self.stop()
return
now = self.clock()
@@ -123,22 +133,35 @@ class AccountSchedule:
def allowed(kind):
return now - self.last_maintenance_error.get(kind, -math.inf) >= 30
poll_due = self.monitoring and now >= self.next_poll and allowed("poll")
if not poll_due:
self._poll_deferred = False
elif self._poll_deferred:
return "poll"
def before_poll(kind):
# Preserve maintenance/hatch priority once, then give an overdue
# snapshot its turn even if the preceding work remains due.
if poll_due:
self._poll_deferred = True
return kind
connected = not engine.demo and engine.client is not None and engine.snapshot is not None
if connected and engine.heartbeat_enabled and now - engine.last_heartbeat >= 240 and allowed("heartbeat"):
return "heartbeat"
return before_poll("heartbeat")
if connected and self.monitoring and engine.pending_recovery_due() and allowed("reconcile"):
return "reconcile"
return before_poll("reconcile")
if connected and engine.heartbeat_enabled and engine.master_sync_due() and allowed("master_sync"):
return "master_sync"
return before_poll("master_sync")
if connected and engine.heartbeat_enabled and engine.growth_sync_due() and allowed("growth_sync"):
return "growth_sync"
if self.monitoring and running and now >= self.next_fast["hatch"]:
return "hatch"
if self.monitoring and now >= self.next_poll:
return before_poll("growth_sync")
if self.monitoring and running and now >= self.next_fast["hatch"] and allowed("hatch"):
return before_poll("hatch")
if poll_due:
return "poll"
if self.monitoring and running:
for kind in ("gift", "explore", "breakthrough"):
if now >= self.next_fast[kind]:
if now >= self.next_fast[kind] and allowed(kind):
return kind
return None
@@ -155,8 +178,14 @@ class Scheduler:
def _run(self):
while not self.closed.wait(self.interval):
for worker in self.workers():
worker.request_tick()
try:
for worker in self.workers():
try:
worker.request_tick()
except Exception:
logger.exception("Unable to request account tick; continuing other accounts")
except Exception:
logger.exception("Unable to enumerate account workers; retrying next scheduler pass")
def close(self):
self.closed.set()