运行任务队列
(self)
| 2956 | |
| 2957 | |
| 2958 | def start(self): |
| 2959 | """运行任务队列""" |
| 2960 | self.init_tasks() |
| 2961 | while True: |
| 2962 | if not self.heap: |
| 2963 | time.sleep(1) |
| 2964 | continue |
| 2965 | |
| 2966 | with self.lock: |
| 2967 | # 取出最近任务 |
| 2968 | next_time, _, task = heapq.heappop(self.heap) |
| 2969 | |
| 2970 | # 计算等待时间 |
| 2971 | current_time = int(time.time()) |
| 2972 | wait_time = max(0, next_time - current_time) |
| 2973 | |
| 2974 | # 等待执行 |
| 2975 | if wait_time > 0: |
| 2976 | time.sleep(wait_time) |
| 2977 | |
| 2978 | # 出现拥塞,跳过本次执行 |
| 2979 | if not task.congestion or not self.repetition_check(task): |
| 2980 | task.run() |
| 2981 | |
| 2982 | # 获取下一次执行时间重新入队 |
| 2983 | try: |
| 2984 | next_execution_time = task.get_next_time() |
| 2985 | with self.lock: |
| 2986 | heapq.heappush(self.heap, (next_execution_time, task.get_ident(), task)) |
| 2987 | except Exception as e: |
| 2988 | # 如果无法获取下一次执行时间,任务不再重新入队 |
| 2989 | write_log("获取{}任务下次执行时间失败: {}".format(task.name, str(e)), traceback.format_exc(), _level='error', color='red') |
| 2990 | pass |
| 2991 | |
| 2992 | |
| 2993 | def start_scripts_service(): |
nothing calls this directly
no test coverage detected