def run(self): """ 获取cron_info信息格式:['0','0/5','*','*','*','*','*'] """ while True: # 遍历cron列表检查并检查定时执行信息 cron_set = datamodel.get_cron_set() try: if int(time.time())%86400==57600: datamodel.clean_history() for cron in cron_set: try: if check_cron_info(cron['cron_info']): if cron['func'] in task_reg_model.task_registry: taskid = "%s-%s-%s" % (cron['func'],time.strftime('%Y%m%d'),int(time.time())) cron['args']['pid'] = taskid cron['args']['func']=cron['func'] task_reg_model.task_registry[cron['func']]((),**cron['args']) logger.info(" exec func[%s] biz[%s]" % (cron['func'],cron['biz'])) else: logger.error(" exec func[%s] biz[%s] no task registry " % (cron['func'],cron['biz'])) except Exception as e: errinfo = traceback.format_exc() logging.getLogger('main').error(errinfo) except: pass time.sleep(1)
def has_cron(func,biz): """检查任务是否已存在 """ for cron in datamodel.get_cron_set(): if cron['func'] == func and cron['biz'] == biz: return cron return False
def remove_cron(func,biz): """移除定时任务 """ cron_set = datamodel.get_cron_set() for cron in cron_set: if cron['func'] == func and cron['biz'] == biz: cron_set.remove(cron) logger.info("cron remove func[%s] biz[%s]" %(func,biz))
def add_cron(cron_data): """ 添加定时任务 cron:{'func':任务函数名, 'args':函数参数, 'biz':业务号, 'cron_info': ['0','0/5','*','*','*','*','*'] 定时内容 格式参详linux的crontable """ cron_set = datamodel.get_cron_set() cron=has_cron(cron_data['func'],cron_data['biz']) if cron: cron_set.remove(cron) cron_set.add(cron_data) logger.info("cron add func[%s] biz[%s]" %(cron_data['func'],cron_data['biz']))