redis-multi-exec-fix

This commit is contained in:
Untone 2023-10-13 11:31:14 +03:00
parent 3b3dfd0f20
commit dbf3d1c745
2 changed files with 14 additions and 11 deletions

View File

@ -51,6 +51,9 @@ async def create_chat(_, info, title="", members=None):
print("create_chat members: %r" % members)
if author_id not in members:
members.append(int(author_id))
await redis.execute('MULTI')
# NOTE: private chats has no title
# reuse private chat created before if exists
if len(members) == 2 and title == "":
@ -86,7 +89,10 @@ async def create_chat(_, info, title="", members=None):
await redis.execute("SADD", f"chats_by_author/{m}", chat_id)
await redis.execute("SET", f"chats/{chat_id}", json.dumps(chat))
await redis.execute("SET", f"chats/{chat_id}/next_message_id", str(0))
await redis.execute("COMMIT")
response = await redis.execute('EXEC')
print(f"EXEC response: {response}")
return {"error": None, "chat": chat}

View File

@ -1,5 +1,4 @@
import json
from services.core import get_author, get_network
from services.redis import redis
from services.auth import login_required
@ -37,33 +36,31 @@ async def load_messages(chat_id: str, limit: int = 5, offset: int = 0, ids=None)
if replies:
messages += await load_messages(chat_id, offset, limit, replies)
except Exception as e:
print(e)
print(f"Error loading messages for chat {chat_id}: {e}")
return messages
@query.field("loadChats")
@login_required
async def load_chats(_, info, limit: int = 50, offset: int = 0):
"""load :limit chats of current user with :offset"""
author_id = info.context["author_id"]
print(f"Loading chats for user with id={author_id}")
await redis.execute('MULTI')
cids = (await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")) or []
cids = cids[offset:(offset + limit)]
members_online = (await redis.execute("SMEMBERS", "authors-online")) or []
await redis.execute('EXEC')
cids = cids[offset:(offset + limit)]
chats = []
lock = asyncio.Lock()
if len(cids) == 0:
print(f"[resolvers.load] no chats for user with id={author_id}, create one with Discours (id=2)")
r = await create_chat(None, info, members=[2]) # member with id = 2 is discours
cids.append(r["chat"]["id"])
await redis.execute("COMMIT")
for cid in cids:
print(type(cid))
print(f"Processing chat ID: {cid}")
async with lock:
print(cid)
all_chats = await redis.execute("GET", f"chats/{cid}")
print(all_chats)
c = await redis.execute("GET", f"chats/{cid}")
print(f"GET result for chat {cid}: {c}") # Add this line
print(f"GET result for chat {cid}: {c}")
if c:
c = json.loads(c)
c["messages"] = await load_messages(cid, 5, 0)