niming_igapi/grpcServer/anoth.py

58 lines
1.5 KiB
Python
Raw Normal View History

2024-12-18 00:35:17 +08:00
import time
import random
2024-12-18 03:06:40 +08:00
import logging
2024-12-18 00:35:17 +08:00
from grpcServer import postProcessor
from utils.ThreadSafeOrderedDict import ThreadSafeOrderedDict
from utils.const import ANOTH_INTERVAL_MIN, ANOTH_INTERVAL_MAX
2024-12-18 03:06:40 +08:00
from utils.tbProcessor import easyExceptionHandler
2024-12-18 00:35:17 +08:00
from db import dbhelper
2024-12-18 03:06:40 +08:00
# logging
anothlog = logging.getLogger("anoth")
anothlog.setLevel(level=logging.INFO)
# task queue
2024-12-18 00:35:17 +08:00
task = ThreadSafeOrderedDict()
def task_round():
t = task.popitem(last=False)
if not t: # 沒任務
2024-12-18 03:06:40 +08:00
anothlog.info("No task in queue")
2024-12-18 00:35:17 +08:00
return
aid = t[1]["aid"]
type = t[0].split("-")[0]
2024-12-18 03:06:40 +08:00
anothlog.info("Task %s(target_aid=%d)"%(type, aid))
2024-12-18 00:35:17 +08:00
if type == "upload": # upload
msg, err = postProcessor.upload(aid)
elif type == "delete":
code = t[1]["code"]
msg, err = postProcessor.remove(code)
else:
msg, err = "Invalid task type %s"%type, 1
if err:
2024-12-18 03:06:40 +08:00
anothlog.error("Task failed: %s"%msg)
2024-12-18 00:35:17 +08:00
elif type == "upload":
dberr = dbhelper.solo_article_set_igid(id=aid, igid=msg)
if dberr:
2024-12-18 03:06:40 +08:00
anothlog.error("Task %s(target_aid=%d): Set igid failed"%(type, aid))
2024-12-18 00:35:17 +08:00
2024-12-18 03:06:40 +08:00
anothlog.info("Task Done")
2024-12-18 00:35:17 +08:00
return
def run():
2024-12-18 03:06:40 +08:00
anothlog.info("Upload/Delete Processor Started")
2024-12-18 00:35:17 +08:00
while True:
2024-12-18 03:06:40 +08:00
try:
task_round()
except Exception as e:
easyExceptionHandler(e)
2024-12-18 00:35:17 +08:00
sleep = random.randint(ANOTH_INTERVAL_MIN, ANOTH_INTERVAL_MAX)
2024-12-18 03:06:40 +08:00
anothlog.info("Next Round After %ds"%sleep)
2024-12-18 00:35:17 +08:00
time.sleep(sleep)