perf: seed alert seen IDs in background on add
Reply immediately with empty seen, run a silent _poll_once in a background task to populate seen IDs, then start the poller. Eliminates the 30-120s blocking wait from 27 sequential backend queries. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2054,19 +2054,6 @@ async def cmd_alert(bot, message):
|
||||
await bot.reply(message, f"Alert limit reached ({_MAX_SUBS})")
|
||||
return
|
||||
|
||||
# Seed: query all backends to populate seen lists (prevents flood)
|
||||
loop = asyncio.get_running_loop()
|
||||
seen: dict[str, list[str]] = {}
|
||||
for tag, backend in _BACKENDS.items():
|
||||
try:
|
||||
items = await loop.run_in_executor(None, backend, keyword)
|
||||
ids = [item["id"] for item in items]
|
||||
if len(ids) > _MAX_SEEN:
|
||||
ids = ids[-_MAX_SEEN:]
|
||||
seen[tag] = ids
|
||||
except Exception:
|
||||
seen[tag] = []
|
||||
|
||||
now = datetime.now(timezone.utc).isoformat()
|
||||
data = {
|
||||
"keyword": keyword,
|
||||
@@ -2077,16 +2064,21 @@ async def cmd_alert(bot, message):
|
||||
"added_at": now,
|
||||
"last_poll": now,
|
||||
"last_error": "",
|
||||
"seen": seen,
|
||||
"seen": {},
|
||||
}
|
||||
_save(bot, key, data)
|
||||
_subscriptions[key] = data
|
||||
_start_poller(bot, key)
|
||||
|
||||
counts = ", ".join(f"{len(seen.get(t, []))} {t}" for t in _BACKENDS)
|
||||
# Seed seen IDs in background (silent poll), then start the poller
|
||||
async def _seed():
|
||||
await _poll_once(bot, key, announce=False)
|
||||
_start_poller(bot, key)
|
||||
|
||||
asyncio.create_task(_seed())
|
||||
|
||||
await bot.reply(
|
||||
message,
|
||||
f"Alert '{name}' added for: {keyword} ({counts} existing)",
|
||||
f"Alert '{name}' added for: {keyword} (seeding in background)",
|
||||
)
|
||||
return
|
||||
|
||||
|
||||
Reference in New Issue
Block a user