core/services/zine/shoutauthor.py

43 lines
1.1 KiB
Python
Raw Normal View History

import asyncio
2022-08-11 05:53:14 +00:00
from base.orm import local_session
from orm.shout import ShoutAuthor
class ShoutAuthorStorage:
authors_by_shout = {}
lock = asyncio.Lock()
period = 30*60 #sec
@staticmethod
async def load(session):
self = ShoutAuthorStorage
2022-08-05 13:14:02 +00:00
authors = session.query(ShoutAuthor).all()
for author in authors:
user = author.user
shout = author.shout
if shout in self.authors_by_shout:
self.authors_by_shout[shout].append(user)
else:
self.authors_by_shout[shout] = [user]
2022-08-11 11:22:10 +00:00
print('[service.shoutauthor] %d authors ' % len(self.authors_by_shout))
# FIXME: [service.shoutauthor] 4251 authors
@staticmethod
async def get_authors(shout):
self = ShoutAuthorStorage
async with self.lock:
return self.authors_by_shout.get(shout, [])
@staticmethod
async def worker():
self = ShoutAuthorStorage
while True:
try:
with local_session() as session:
async with self.lock:
await self.load(session)
2022-08-11 09:09:57 +00:00
print("[service.shoutauthor] updated")
except Exception as err:
2022-08-11 09:09:57 +00:00
print("[service.shoutauthor] errror: %s" % (err))
await asyncio.sleep(self.period)