niming_igapi/grpcServer/anoth.py

48 lines
1.2 KiB
Python
Raw Normal View History

2024-12-18 00:35:17 +08:00
import time
import random
from grpcServer import postProcessor
from utils.ThreadSafeOrderedDict import ThreadSafeOrderedDict
from utils.const import ANOTH_INTERVAL_MIN, ANOTH_INTERVAL_MAX
from db import dbhelper
task = ThreadSafeOrderedDict()
def task_round():
t = task.popitem(last=False)
if not t: # 沒任務
print("[*] No task in queue")
return
aid = t[1]["aid"]
type = t[0].split("-")[0]
print("[*] Task %s(target_aid=%d)"%(type, aid))
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:
print("[X] Task failed: %s"%msg)
elif type == "upload":
dberr = dbhelper.solo_article_set_igid(id=aid, igid=msg)
if dberr:
print("[X] Task %s(target_aid=%d): Set igid failed"%(type, aid))
print("[*] Task Done")
return
def run():
print("[*] Upload/Delete Processor Started")
while True:
task_round()
sleep = random.randint(ANOTH_INTERVAL_MIN, ANOTH_INTERVAL_MAX)
print("[*] Next Round After %ds"%sleep)
time.sleep(sleep)