import time import random import logging from grpcServer import postProcessor from utils.ThreadSafeOrderedDict import ThreadSafeOrderedDict from utils.const import ANOTH_INTERVAL_MIN, ANOTH_INTERVAL_MAX from utils.tbProcessor import easyExceptionHandler from db import dbhelper # logging anothlog = logging.getLogger("anoth") anothlog.setLevel(level=logging.INFO) # task queue task = ThreadSafeOrderedDict() def task_round(): t = task.popitem(last=False) if not t: # 沒任務 anothlog.info("No task in queue") return aid = t[1]["aid"] type = t[0].split("-")[0] anothlog.info("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: anothlog.error("Task failed: %s"%msg) elif type == "upload": dberr = dbhelper.solo_article_set_igid(id=aid, igid=msg) if dberr: anothlog.error("Task %s(target_aid=%d): Set igid failed"%(type, aid)) anothlog.info("Task Done") return def run(): anothlog.info("Upload/Delete Processor Started") while True: try: task_round() except Exception as e: easyExceptionHandler(e) sleep = random.randint(ANOTH_INTERVAL_MIN, ANOTH_INTERVAL_MAX) anothlog.info("Next Round After %ds"%sleep) time.sleep(sleep)