shout-topic-storage, work load fix wip
This commit is contained in:
parent
1269e96aed
commit
96c973b70f
|
@ -119,6 +119,7 @@ async def get_search_results(_, _info, searchtext, offset, limit):
|
||||||
|
|
||||||
@query.field("shoutsByTopics")
|
@query.field("shoutsByTopics")
|
||||||
async def shouts_by_topics(_, _info, slugs, offset, limit):
|
async def shouts_by_topics(_, _info, slugs, offset, limit):
|
||||||
|
# TODO: use ShoutTopicsStorage
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
shouts = (
|
shouts = (
|
||||||
session.query(Shout)
|
session.query(Shout)
|
||||||
|
@ -154,6 +155,7 @@ async def shouts_by_collection(_, _info, collection, offset, limit):
|
||||||
|
|
||||||
@query.field("shoutsByAuthors")
|
@query.field("shoutsByAuthors")
|
||||||
async def shouts_by_authors(_, _info, slugs, offset, limit):
|
async def shouts_by_authors(_, _info, slugs, offset, limit):
|
||||||
|
# TODO: use ShoutAuthorsStorage
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
shouts = (
|
shouts = (
|
||||||
session.query(Shout)
|
session.query(Shout)
|
||||||
|
|
|
@ -225,7 +225,7 @@ type Query {
|
||||||
# shouts
|
# shouts
|
||||||
getShoutBySlug(slug: String!): Shout!
|
getShoutBySlug(slug: String!): Shout!
|
||||||
shoutsForFeed(offset: Int!, limit: Int!): [Shout]! # test
|
shoutsForFeed(offset: Int!, limit: Int!): [Shout]! # test
|
||||||
shoutsByTopics(slugs: [String]!, offset: Int!, limit: Int!): [Shout]!
|
shoutsByTopics(slugs: [String]!, offset: Int!, limit: Int!): [Shout]! # TODO: work load fix
|
||||||
shoutsByAuthors(slugs: [String]!, offset: Int!, limit: Int!): [Shout]!
|
shoutsByAuthors(slugs: [String]!, offset: Int!, limit: Int!): [Shout]!
|
||||||
shoutsByCommunities(slugs: [String]!, offset: Int!, limit: Int!): [Shout]!
|
shoutsByCommunities(slugs: [String]!, offset: Int!, limit: Int!): [Shout]!
|
||||||
# topReacted(offset: Int!, limit: Int!): [Shout]!
|
# topReacted(offset: Int!, limit: Int!): [Shout]!
|
||||||
|
|
38
services/zine/shouttopic.py
Normal file
38
services/zine/shouttopic.py
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
from base.orm import local_session
|
||||||
|
from orm.shout import ShoutTopic
|
||||||
|
|
||||||
|
|
||||||
|
class ShoutTopicStorage:
|
||||||
|
topics_by_shout = {}
|
||||||
|
lock = asyncio.Lock()
|
||||||
|
period = 30 * 60 # sec
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def load(session):
|
||||||
|
self = ShoutTopicStorage
|
||||||
|
sas = session.query(ShoutTopic).all()
|
||||||
|
for sa in sas:
|
||||||
|
self.topics_by_shout[sa.shout] = self.topics_by_shout.get(sa.shout, [])
|
||||||
|
self.topics_by_shout[sa.shout].append([sa.user, sa.caption])
|
||||||
|
print("[zine.topics] %d shouts preprocessed" % len(self.topics_by_shout))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def get_topics(shout):
|
||||||
|
self = ShoutTopicStorage
|
||||||
|
async with self.lock:
|
||||||
|
return self.topics_by_shout.get(shout, [])
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def worker():
|
||||||
|
self = ShoutTopicStorage
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
with local_session() as session:
|
||||||
|
async with self.lock:
|
||||||
|
await self.load(session)
|
||||||
|
print("[zine.topics] state updated")
|
||||||
|
except Exception as err:
|
||||||
|
print("[zine.topics] errror: %s" % (err))
|
||||||
|
await asyncio.sleep(self.period)
|
Loading…
Reference in New Issue
Block a user