async-fix
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import asyncio
|
||||
import time
|
||||
from datetime import timedelta, timezone, datetime
|
||||
from os import environ
|
||||
from os import environ, path
|
||||
|
||||
from gql import Client, gql
|
||||
from gql.transport.requests import RequestsHTTPTransport
|
||||
from gql.transport.httpx import HTTPXAsyncTransport
|
||||
|
||||
from services.db import local_session
|
||||
from orm import Topic
|
||||
@@ -39,14 +39,14 @@ load_pages = gql(
|
||||
} } """
|
||||
)
|
||||
|
||||
schema_str = open("schemas/ackee.graphql").read()
|
||||
schema_str = open(path.dirname(__file__) + "/ackee.graphql").read()
|
||||
token = environ.get("ACKEE_TOKEN", "")
|
||||
|
||||
|
||||
def create_client(headers=None, schema=None):
|
||||
return Client(
|
||||
schema=schema,
|
||||
transport=RequestsHTTPTransport(
|
||||
transport=HTTPXAsyncTransport(
|
||||
url="https://ackee.discours.io/api",
|
||||
headers=headers,
|
||||
),
|
||||
@@ -75,16 +75,17 @@ class ViewedStorage:
|
||||
{"Authorization": "Bearer %s" % str(token)}, schema=schema_str
|
||||
)
|
||||
print(
|
||||
"[stat] * authorized permanentely by ackee.discours.io: %s" % token
|
||||
"[stat.viewed] * authorized permanentely by ackee.discours.io: %s"
|
||||
% token
|
||||
)
|
||||
else:
|
||||
print("[stat] * please set ACKEE_TOKEN")
|
||||
print("[stat.viewed] * please set ACKEE_TOKEN")
|
||||
self.disabled = True
|
||||
|
||||
@staticmethod
|
||||
async def update_pages():
|
||||
"""query all the pages from ackee sorted by views count"""
|
||||
print("[stat] ⎧ updating ackee pages data ---")
|
||||
print("[stat.viewed] ⎧ updating ackee pages data ---")
|
||||
start = time.time()
|
||||
self = ViewedStorage
|
||||
try:
|
||||
@@ -100,12 +101,12 @@ class ViewedStorage:
|
||||
await ViewedStorage.increment(slug, shouts[slug])
|
||||
except Exception:
|
||||
pass
|
||||
print("[stat] ⎪ %d pages collected " % len(shouts.keys()))
|
||||
print("[stat.viewed] ⎪ %d pages collected " % len(shouts.keys()))
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
end = time.time()
|
||||
print("[stat] ⎪ update_pages took %fs " % (end - start))
|
||||
print("[stat.viewed] ⎪ update_pages took %fs " % (end - start))
|
||||
|
||||
@staticmethod
|
||||
async def get_facts():
|
||||
@@ -196,29 +197,26 @@ class ViewedStorage:
|
||||
self = ViewedStorage
|
||||
if self.disabled:
|
||||
return
|
||||
print("[stat] worker started")
|
||||
|
||||
while True:
|
||||
try:
|
||||
print("[stat] - updating views...")
|
||||
print("[stat.viewed] - updating views...")
|
||||
await self.update_pages()
|
||||
failed = 0
|
||||
except Exception:
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
failed += 1
|
||||
print("[stat] - update failed #%d, wait 10 seconds" % failed)
|
||||
print("[stat.viewed] - update failed #%d, wait 10 seconds" % failed)
|
||||
if failed > 3:
|
||||
print("[stat] - not trying to update anymore")
|
||||
print("[stat.viewed] - not trying to update anymore")
|
||||
break
|
||||
if failed == 0:
|
||||
when = datetime.now(timezone.utc) + timedelta(seconds=self.period)
|
||||
t = format(when.astimezone().isoformat())
|
||||
print(
|
||||
"[stat] ⎩ next update: %s"
|
||||
"[stat.viewed] ⎩ next update: %s"
|
||||
% (t.split("T")[0] + " " + t.split("T")[1].split(".")[0])
|
||||
)
|
||||
await asyncio.sleep(self.period)
|
||||
else:
|
||||
await asyncio.sleep(10)
|
||||
print("[stat] - trying to update data again")
|
||||
print("[stat.viewed] - trying to update data again")
|
||||
|
Reference in New Issue
Block a user