random-worker
This commit is contained in:
parent
d1caded052
commit
700e5b2e6e
3
main.py
3
main.py
|
@ -19,6 +19,7 @@ from services.main import storages_init
|
|||
from services.stat.reacted import ReactedStorage
|
||||
from services.stat.topicstat import TopicStat
|
||||
from services.stat.viewed import ViewedStorage
|
||||
from services.zine.topics import TopicStorage
|
||||
from services.zine.gittask import GitTask
|
||||
from services.zine.shoutauthor import ShoutAuthorStorage
|
||||
from settings import DEV_SERVER_STATUS_FILE_NAME
|
||||
|
@ -36,6 +37,8 @@ async def start_up():
|
|||
init_tables()
|
||||
await redis.connect()
|
||||
await storages_init()
|
||||
topics_random_work = asyncio.create_task(TopicStorage().worker())
|
||||
print(topics_random_work)
|
||||
views_stat_task = asyncio.create_task(ViewedStorage().worker())
|
||||
print(views_stat_task)
|
||||
reacted_storage_task = asyncio.create_task(ReactedStorage.worker())
|
||||
|
|
|
@ -80,6 +80,7 @@ async def search_user_chats(by, messages: set, slug: str, limit, offset):
|
|||
if body_like:
|
||||
# search in all messages in all user's chats
|
||||
for c in cids:
|
||||
# FIXME: user redis scan here
|
||||
mmm = set(await load_messages(c, limit, offset))
|
||||
for m in mmm:
|
||||
if body_like in m["body"]:
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
import sqlalchemy as sa
|
||||
from sqlalchemy import and_, select
|
||||
from sqlalchemy import and_
|
||||
from auth.authenticate import login_required
|
||||
from base.orm import local_session
|
||||
from base.resolvers import mutation, query
|
||||
from orm import Shout
|
||||
from orm.topic import Topic, TopicFollower
|
||||
from services.zine.topics import TopicStorage
|
||||
from services.stat.topicstat import TopicStat
|
||||
|
@ -110,8 +108,4 @@ async def topic_unfollow(user, slug):
|
|||
|
||||
@query.field("topicsRandom")
|
||||
async def topics_random(_, info, amount=12):
|
||||
with local_session() as session:
|
||||
q = select(Topic).join(Shout).group_by(Topic.id).having(sa.func.count(Shout.id) > 2).order_by(
|
||||
sa.func.random()).limit(amount)
|
||||
random_topics = list(map(lambda result_item: result_item.Topic, session.execute(q)))
|
||||
return random_topics
|
||||
return TopicStorage.get_random_topics(amount)
|
||||
|
|
|
@ -1,10 +1,15 @@
|
|||
import asyncio
|
||||
from base.orm import local_session
|
||||
from orm.topic import Topic
|
||||
from orm.shout import Shout
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import select
|
||||
|
||||
|
||||
class TopicStorage:
|
||||
topics = {}
|
||||
lock = asyncio.Lock()
|
||||
random_topics = []
|
||||
|
||||
@staticmethod
|
||||
def init(session):
|
||||
|
@ -27,6 +32,30 @@ class TopicStorage:
|
|||
# topic.parents = parents
|
||||
# return topic
|
||||
|
||||
@staticmethod
|
||||
def get_random_topics(amount):
|
||||
return TopicStorage.random_topics[0:amount]
|
||||
|
||||
@staticmethod
|
||||
def renew_topics_random():
|
||||
with local_session() as session:
|
||||
q = select(Topic).join(Shout).group_by(Topic.id).having(sa.func.count(Shout.id) > 2).order_by(
|
||||
sa.func.random()).limit(50)
|
||||
TopicStorage.random_topics = list(map(
|
||||
lambda result_item: result_item.Topic, session.execute(q)
|
||||
))
|
||||
|
||||
@staticmethod
|
||||
async def worker():
|
||||
self = TopicStorage
|
||||
async with self.lock:
|
||||
while True:
|
||||
try:
|
||||
self.renew_topics_random()
|
||||
except Exception as err:
|
||||
print("[zine.topics] error %s" % (err))
|
||||
await asyncio.sleep(300) # 5 mins
|
||||
|
||||
@staticmethod
|
||||
async def get_topics_all():
|
||||
self = TopicStorage
|
||||
|
|
Loading…
Reference in New Issue
Block a user