redis-multi-exec-fix
This commit is contained in:
parent
3b3dfd0f20
commit
dbf3d1c745
|
@ -51,6 +51,9 @@ async def create_chat(_, info, title="", members=None):
|
||||||
print("create_chat members: %r" % members)
|
print("create_chat members: %r" % members)
|
||||||
if author_id not in members:
|
if author_id not in members:
|
||||||
members.append(int(author_id))
|
members.append(int(author_id))
|
||||||
|
|
||||||
|
await redis.execute('MULTI')
|
||||||
|
|
||||||
# NOTE: private chats has no title
|
# NOTE: private chats has no title
|
||||||
# reuse private chat created before if exists
|
# reuse private chat created before if exists
|
||||||
if len(members) == 2 and title == "":
|
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("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}", json.dumps(chat))
|
||||||
await redis.execute("SET", f"chats/{chat_id}/next_message_id", str(0))
|
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}
|
return {"error": None, "chat": chat}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from services.core import get_author, get_network
|
from services.core import get_author, get_network
|
||||||
from services.redis import redis
|
from services.redis import redis
|
||||||
from services.auth import login_required
|
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:
|
if replies:
|
||||||
messages += await load_messages(chat_id, offset, limit, replies)
|
messages += await load_messages(chat_id, offset, limit, replies)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(f"Error loading messages for chat {chat_id}: {e}")
|
||||||
return messages
|
return messages
|
||||||
|
|
||||||
|
|
||||||
@query.field("loadChats")
|
@query.field("loadChats")
|
||||||
@login_required
|
@login_required
|
||||||
async def load_chats(_, info, limit: int = 50, offset: int = 0):
|
async def load_chats(_, info, limit: int = 50, offset: int = 0):
|
||||||
"""load :limit chats of current user with :offset"""
|
"""load :limit chats of current user with :offset"""
|
||||||
author_id = info.context["author_id"]
|
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 = (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 []
|
members_online = (await redis.execute("SMEMBERS", "authors-online")) or []
|
||||||
|
await redis.execute('EXEC')
|
||||||
|
cids = cids[offset:(offset + limit)]
|
||||||
chats = []
|
chats = []
|
||||||
lock = asyncio.Lock()
|
lock = asyncio.Lock()
|
||||||
if len(cids) == 0:
|
if len(cids) == 0:
|
||||||
print(f"[resolvers.load] no chats for user with id={author_id}, create one with Discours (id=2)")
|
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
|
r = await create_chat(None, info, members=[2]) # member with id = 2 is discours
|
||||||
cids.append(r["chat"]["id"])
|
cids.append(r["chat"]["id"])
|
||||||
await redis.execute("COMMIT")
|
|
||||||
for cid in cids:
|
for cid in cids:
|
||||||
print(type(cid))
|
print(f"Processing chat ID: {cid}")
|
||||||
async with lock:
|
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}")
|
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:
|
if c:
|
||||||
c = json.loads(c)
|
c = json.loads(c)
|
||||||
c["messages"] = await load_messages(cid, 5, 0)
|
c["messages"] = await load_messages(cid, 5, 0)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user