def update_job_run(job_id): curr_time = int(time.time()) item = find_job(job_id) if not item: return item["next_run_time"] = curr_time + item["interval"] item["next_run_date"] = utils.time2date(item["next_run_time"]) item["last_run_time"] = curr_time item["last_run_date"] = utils.time2date(curr_time) item["run_number"] += 1 query = {"_id": item["_id"]} conn('scheduler').find_one_and_replace(query, item)
def add_job(domain, scope_id, options=None, interval=60 * 1, name="", scope_type=AssetScopeType.DOMAIN): logger.info("add {} job {} {} {}".format(scope_type, interval, domain, scope_id)) if options is None: if scope_type == AssetScopeType.DOMAIN: options = domain_monitor_options if scope_type == AssetScopeType.IP: options = ip_monitor_options current_time = int(time.time()) + 30 item = { "domain": domain, "scope_id": scope_id, "interval": interval, "next_run_time": current_time, "next_run_date": utils.time2date(current_time), "last_run_time": 0, "last_run_date": "-", "run_number": 0, "status": SchedulerStatus.RUNNING, "monitor_options": options, "name": name, "scope_type": scope_type } conn('scheduler').insert(item) return str(item["_id"])
def run_forever(): logger.info("start scheduler server ") while True: curr_time = int(time.time()) for item in all_job(): if item.get("status") == SchedulerStatus.STOP: continue if item["next_run_time"] <= curr_time: domain = item["domain"] scope_id = item["scope_id"] options = item["monitor_options"] name = item["name"] scope_type = item.get("scope_type") if not scope_type: scope_type = AssetScopeType.DOMAIN submit_job(domain=domain, job_id=str(item["_id"]), scope_id=scope_id, options=options, name=name, scope_type=scope_type) item["next_run_time"] = curr_time + item["interval"] item["next_run_date"] = utils.time2date(item["next_run_time"]) query = {"_id": item["_id"]} conn('scheduler').find_one_and_replace(query, item) logger.info(time.time()) time.sleep(30)
def recover_job(job_id): current_time = int(time.time()) + 30 item = find_job(job_id) next_run_time = current_time + item["interval"] item["next_run_date"] = utils.time2date(next_run_time) item["next_run_time"] = next_run_time item["status"] = SchedulerStatus.RUNNING query = {"_id": ObjectId(job_id)} ret = conn('scheduler').find_one_and_replace(query, item) return ret