Introduce chunking to communication with Wiki-Bot

This commit is contained in:
Frisk 2024-07-22 23:49:50 +02:00
parent efbe074044
commit bf09459ba6

View file

@ -33,6 +33,10 @@ class DomainManager:
self.domains: dict[str, Domain] = {}
self.start_time: float = time.time()
@staticmethod
def chunkstring(payload, length):
return (payload[0 + i:length + i] for i in range(0, len(payload), length))
async def webhook_update(self, connection: asyncpg.Connection, pid: int, channel: str, payload: str):
def result_handler(result):
if result is None:
@ -82,7 +86,7 @@ class DomainManager:
domain = self.return_domain(self.get_domain(split_payload[2]))
logger.info("RCGCDBDEBUG Domain information for {}: {}".format(domain.name, str(domain)))
logger.info("RCGCDBDEBUG Wiki information for {}: {}".format(split_payload[2], domain.get_wiki(split_payload[2])))
elif split_payload[1] == "DUMP":
elif split_payload[1] == "DUMP" and len(split_payload) > 2:
# Dump debug info JSON object into postgres pubsub channel
logger.info(f"Received {' '.join(split_payload)} on pub/sub. Preparing JSON with data...")
json_object = {"uptime": time.time() - self.start_time, "domain_count": len(self.domains),
@ -98,16 +102,24 @@ class DomainManager:
json_object["domains"][name] = domain.json()
for message in messagequeue._queue:
json_object["queued_messages"].append({"metadata": str(message.discord_message.metadata), "url": message.wiki.script_url})
await connection.execute("select pg_notify('debugresponse', 'DUMP ' || $1);", json.dumps(json_object))
elif split_payload[1] == "SITE":
req_id: str = split_payload[2]
json_string: str = json.dumps(json_object)
for json_part in self.chunkstring(json_string, 7950):
await connection.execute("select pg_notify('debugresponse', 'DUMP ' || $1 || ' ' || $2);", req_id, json_part)
await connection.execute("select pg_notify('debugresponse', 'DUMP END ' || $1);", req_id)
elif split_payload[1] == "SITE" and len(split_payload) > 3:
logger.info(f"Received {' '.join(split_payload)} on pub/sub. Preparing JSON with data...")
req_id = split_payload[2]
req_id: str = split_payload[2]
domain = self.return_domain(self.get_domain(split_payload[3]))
wiki = domain.get_wiki(split_payload[3])
if wiki is not None:
logger.debug("Wiki specified in pub/sub message has been found. Preparing and sending dump.")
await connection.execute("select pg_notify('debugresponse', 'SITE ' || $1 || ' ' || $2);",
req_id, json.dumps(wiki.json()))
json_string: str = json.dumps(wiki.json())
for json_part in self.chunkstring(json_string, 7950):
await connection.execute("select pg_notify('debugresponse', 'SITE ' || $1 || ' ' || $2);",
req_id, json_part)
await connection.execute("select pg_notify('debugresponse', 'SITE END ' || $1);",
req_id)
else:
logger.error("Unknown pub/sub command! Payload: {}".format(payload))