Skip to content

Commit a5a1dde

Browse files
committed
fix: refresh disabled automation rules from backend faster
Read disabled rule ids from the backend runtime endpoint and shorten the poll interval so rule toggles apply without a manual bot restart.
1 parent 84f21ec commit a5a1dde

2 files changed

Lines changed: 37 additions & 13 deletions

File tree

flexus_client_kit/ckit_automation.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@
66
from pathlib import Path
77
from typing import Any, Optional
88

9+
import aiohttp
10+
import gql
11+
import gql.transport.exceptions
912
import jsonschema
10-
from pymongo.errors import PyMongoError
13+
14+
from flexus_client_kit import ckit_client
1115

1216
logger = logging.getLogger(__name__)
1317

18+
_GQL_DISABLED_RULES = gql.gql(
19+
"""query AutomationDisabledRulesRuntime($persona_id: String!) {
20+
automation_disabled_rules(persona_id: $persona_id)
21+
}"""
22+
)
23+
1424
# Loaded by set_automation_schema_dict() from ckit_automation_v1_schema_build (authoritative) or
1525
# set_automation_schema(path) for tests / offline fixtures.
1626
_AUTOMATION_SCHEMA: dict | None = None
@@ -113,8 +123,18 @@ def validate_automation_json(data: dict) -> list[str]:
113123

114124

115125
class DisabledRulesCache:
116-
def __init__(self, mongo_db: Any, interval: float = 30.0):
117-
self._mongo_db = mongo_db
126+
"""
127+
In-memory cache of disabled automation rule IDs for a persona, refreshed periodically
128+
from the backend GraphQL automation_disabled_rules endpoint.
129+
130+
Polling at 5 s so that rule enable/disable toggles made in the UI propagate to the
131+
runtime within a few seconds without requiring a bot restart. The GQL query is cheap
132+
(returns only a list of IDs), so the extra request frequency is negligible.
133+
"""
134+
135+
def __init__(self, fclient: ckit_client.FlexusClient, persona_id: str, interval: float = 5.0):
136+
self._fclient = fclient
137+
self._persona_id = persona_id
118138
self._interval = interval
119139
self._disabled: set = set()
120140
self._task: Optional[asyncio.Task] = None
@@ -136,15 +156,19 @@ def get(self) -> set:
136156

137157
async def _refresh(self) -> None:
138158
try:
139-
doc = await self._mongo_db["bot_runtime_config"].find_one({"_id": "disabled_rule_ids"})
140-
if doc and isinstance(doc.get("ids"), list):
141-
self._disabled = {str(x) for x in doc["ids"] if x}
142-
else:
143-
self._disabled = set()
144-
except PyMongoError as e:
145-
logger.warning("DisabledRulesCache refresh failed (mongo), keeping last known state: %s %s", type(e).__name__, e)
146-
except (TypeError, ValueError) as e:
147-
logger.warning("DisabledRulesCache refresh failed (bad doc), keeping last known state: %s %s", type(e).__name__, e)
159+
async with (await self._fclient.use_http_on_behalf(self._persona_id, "")) as http:
160+
result = await http.execute(
161+
_GQL_DISABLED_RULES,
162+
variable_values={"persona_id": self._persona_id},
163+
)
164+
ids = result.get("automation_disabled_rules") or []
165+
self._disabled = {str(x) for x in ids if x}
166+
except gql.transport.exceptions.TransportError as e:
167+
logger.warning("DisabledRulesCache refresh failed (backend), keeping last known state: %s %s", type(e).__name__, e)
168+
except aiohttp.ClientError as e:
169+
logger.warning("DisabledRulesCache refresh failed (network), keeping last known state: %s %s", type(e).__name__, e)
170+
except (TypeError, ValueError, KeyError) as e:
171+
logger.warning("DisabledRulesCache refresh failed (bad response), keeping last known state: %s %s", type(e).__name__, e)
148172

149173
async def _loop(self) -> None:
150174
while True:

flexus_simple_bots/discord_bot/discord_bot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ async def discord_bot_main_loop(fclient: ckit_client.FlexusClient, rcx: ckit_bot
403403
await ckit_crm_members.migrate_legacy_collections(mongo_db)
404404
await ckit_crm_members.ensure_member_indexes(mongo_db)
405405

406-
disabled_cache = DisabledRulesCache(mongo_db)
406+
disabled_cache = DisabledRulesCache(fclient, rcx.persona.persona_id)
407407
await disabled_cache.start()
408408

409409
rules = ckit_automation_engine.load_rules(persona_setup_raw)

0 commit comments

Comments
 (0)