This commit is contained in:
Tony Rewin 2023-10-06 01:12:34 +03:00
parent 720d8a4a68
commit 78a3354d5f
4 changed files with 13 additions and 18 deletions

View File

@ -35,11 +35,11 @@ async def start_up():
await redis.connect() await redis.connect()
await storages_init() await storages_init()
views_stat_task = asyncio.create_task(ViewedStorage().worker()) views_stat_task = asyncio.create_task(ViewedStorage().worker())
print(views_stat_task)
try: try:
import sentry_sdk import sentry_sdk
sentry_sdk.init(SENTRY_DSN) sentry_sdk.init(SENTRY_DSN)
print("[sentry] started")
except Exception as e: except Exception as e:
print("[sentry] init error") print("[sentry] init error")
print(e) print(e)

View File

@ -5,9 +5,5 @@ from services.db import local_session
async def storages_init(): async def storages_init():
with local_session() as session: with local_session() as session:
print("[main] initialize SearchService")
await SearchService.init(session) await SearchService.init(session)
print("[main] SearchService initialized")
print("[main] initialize storages")
await ViewedStorage.init() await ViewedStorage.init()
print("[main] storages initialized")

View File

@ -12,7 +12,7 @@ class SearchService:
@staticmethod @staticmethod
async def init(session): async def init(session):
async with SearchService.lock: async with SearchService.lock:
print("[search.service] did nothing") print("[search] did nothing")
SearchService.cache = {} SearchService.cache = {}
@staticmethod @staticmethod

View File

@ -75,17 +75,16 @@ class ViewedStorage:
{"Authorization": "Bearer %s" % str(token)}, schema=schema_str {"Authorization": "Bearer %s" % str(token)}, schema=schema_str
) )
print( print(
"[stat.viewed] * authorized permanentely by ackee.discours.io: %s" "[stat] * authorized permanentely by ackee.discours.io: %s" % token
% token
) )
else: else:
print("[stat.viewed] * please set ACKEE_TOKEN") print("[stat] * please set ACKEE_TOKEN")
self.disabled = True self.disabled = True
@staticmethod @staticmethod
async def update_pages(): async def update_pages():
"""query all the pages from ackee sorted by views count""" """query all the pages from ackee sorted by views count"""
print("[stat.viewed] ⎧ updating ackee pages data ---") print("[stat] ⎧ updating ackee pages data ---")
start = time.time() start = time.time()
self = ViewedStorage self = ViewedStorage
try: try:
@ -101,12 +100,12 @@ class ViewedStorage:
await ViewedStorage.increment(slug, shouts[slug]) await ViewedStorage.increment(slug, shouts[slug])
except Exception: except Exception:
pass pass
print("[stat.viewed] ⎪ %d pages collected " % len(shouts.keys())) print("[stat] ⎪ %d pages collected " % len(shouts.keys()))
except Exception as e: except Exception as e:
raise e raise e
end = time.time() end = time.time()
print("[stat.viewed] ⎪ update_pages took %fs " % (end - start)) print("[stat] ⎪ update_pages took %fs " % (end - start))
@staticmethod @staticmethod
async def get_facts(): async def get_facts():
@ -197,26 +196,26 @@ class ViewedStorage:
self = ViewedStorage self = ViewedStorage
if self.disabled: if self.disabled:
return return
print("[stat] worker started")
while True: while True:
try: try:
print("[stat.viewed] - updating views...") print("[stat] - updating views...")
await self.update_pages() await self.update_pages()
failed = 0 failed = 0
except Exception: except Exception:
failed += 1 failed += 1
print("[stat.viewed] - update failed #%d, wait 10 seconds" % failed) print("[stat] - update failed #%d, wait 10 seconds" % failed)
if failed > 3: if failed > 3:
print("[stat.viewed] - not trying to update anymore") print("[stat] - not trying to update anymore")
break break
if failed == 0: if failed == 0:
when = datetime.now(timezone.utc) + timedelta(seconds=self.period) when = datetime.now(timezone.utc) + timedelta(seconds=self.period)
t = format(when.astimezone().isoformat()) t = format(when.astimezone().isoformat())
print( print(
"[stat.viewed] ⎩ next update: %s" "[stat] ⎩ next update: %s"
% (t.split("T")[0] + " " + t.split("T")[1].split(".")[0]) % (t.split("T")[0] + " " + t.split("T")[1].split(".")[0])
) )
await asyncio.sleep(self.period) await asyncio.sleep(self.period)
else: else:
await asyncio.sleep(10) await asyncio.sleep(10)
print("[stat.viewed] - trying to update data again") print("[stat] - trying to update data again")