-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule.py
More file actions
213 lines (171 loc) · 7.62 KB
/
schedule.py
File metadata and controls
213 lines (171 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import time
import threading
from typing import Optional
import logging
class TestbenchSchedule:
def __init__(self, config: dict, tools: dict, tasks: dict):
self.log = logging.getLogger("schedule")
self.__config = config
self.__tools = tools
self.__tasks = tasks
try:
self.__parse_schedule()
except Exception as e:
raise ValueError(f"Error setting up schedule: {e}")
self.__current = self.__get_lowest(0)
if self.__current is None:
self.log.warning("Schedule is empty, no tasks to run")
def __parse_schedule(self) -> None:
self.log.debug("Parsing schedule configuration")
if self.__config is None:
return
for task_name in self.__config:
self.log.debug(f"Processing task: {task_name}")
if task_name not in self.__tasks:
raise KeyError(f'Task "{task_name}" not found in tasks configuration')
task = self.__tasks[task_name]
schedule_task = self.__config[task_name]
if "order" not in schedule_task:
raise KeyError(f'No order found in schedule for task "{task_name}"')
task_order = schedule_task["order"]
if "steps" not in schedule_task:
raise KeyError(f'No steps found in schedule for task "{task_name}"')
task_steps = schedule_task["steps"]
cleanup_steps = schedule_task.get("cleanup", [])
task_steps_list = []
for step_name in task_steps:
step = schedule_task["steps"][step_name]
step_tool_type, step_tool = step.split(";")
if step_tool_type not in task["tools"]:
raise KeyError(
f'Tool type "{step_tool_type}" not found in tools of task "{task_name}"'
)
if step_tool not in task["tools"][step_tool_type]:
raise KeyError(
f'No tool "{step_tool}" of type "{step_tool_type}" found in task "{task_name}"'
)
if (
step_name
not in self.__tools[step_tool_type][step_tool]["cls"].__dict__
):
raise KeyError(
f'Tool "{step_tool}" of type "{step_tool_type}" does not have function "{step_name}"'
)
task_steps_list.append(
{"type": step_tool_type, "tool": step_tool, "func": step_name}
)
task_cleanup_list = []
for step_name in cleanup_steps:
step = schedule_task["cleanup"][step_name]
step_tool_type, step_tool = step.split(";")
if step_tool_type not in task["tools"]:
raise KeyError(
f'Tool type "{step_tool_type}" not found in tools of task "{task_name}"'
)
if step_tool not in task["tools"][step_tool_type]:
raise KeyError(
f'No tool "{step_tool}" of type "{step_tool_type}" found in task "{task_name}"'
)
if (
step_name
not in self.__tools[step_tool_type][step_tool]["cls"].__dict__
):
raise KeyError(
f'Tool "{step_tool}" of type "{step_tool_type}" does not have function "{step_name}"'
)
task_cleanup_list.append(
{"type": step_tool_type, "tool": step_tool, "func": step_name}
)
self.__tasks[task_name]["order"] = task_order
self.__tasks[task_name]["steps"] = task_steps_list
self.__tasks[task_name]["cleanup"] = task_cleanup_list
for task_name in self.__tasks:
if "order" not in self.__tasks[task_name]:
self.log.warning(f'Task "{task_name}" included but not in schedule')
continue
def __get_lowest(self, min: int) -> Optional[int]:
# Get lowest order task above min
lowest = None
for task_name in self.__tasks:
if "order" not in self.__tasks[task_name]:
continue
task_order = self.__tasks[task_name]["order"]
if task_order < min:
continue
if not lowest or task_order < lowest:
lowest = task_order
return lowest
def __get_tasks(self, order: int) -> dict:
tasks = {}
for task_name in self.__tasks:
if "order" not in self.__tasks[task_name]:
continue
task_order = self.__tasks[task_name]["order"]
if task_order == order:
tasks[task_name] = self.__tasks[task_name]
return tasks
def is_done(self) -> bool:
return self.__current is None
def iterate(self) -> Optional[int]:
self.log.debug("Iterating schedule")
if self.__current is None:
self.log.warning("No current task to run, schedule is done.")
return None
current = self.__current
self.__current = self.__get_lowest(current + 1)
current_tasks = self.__get_tasks(current)
while len(current_tasks) == 0:
current = self.__current
if current is None:
return None
current_tasks = self.__get_tasks(current)
self.log.debug(f"Running order {len(current_tasks)} tasks at order {current}")
if len(current_tasks) > 1:
threads = []
for task_name in current_tasks:
task = current_tasks[task_name]
thread = threading.Thread(
target=self.__run_task, args=(task_name, task)
)
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
else:
task_name = list(current_tasks.keys())[0]
task = current_tasks[task_name]
self.__run_task(task_name, task)
return current
def __run_task(self, task_name: str, task: dict) -> None:
self.log.info(f"Running task: {task_name}")
for step in task["steps"]:
self.log.info(
f"Running step: {task_name}/{step['type']}/{step['tool']}/{step['func']}"
)
start_time = time.time()
try:
getattr(task["tools"][step["type"]][step["tool"]], step["func"])()
end_time = time.time()
self.log.info(
f"Done ({end_time - start_time:.2f}s): {task_name}/{step['type']}/{step['tool']}/{step['func']}"
)
except Exception as e:
end_time = time.time()
self.log.error(
f"Failed ({end_time - start_time:.2f}s): {task_name}/{step['type']}/{step['tool']}/{step['func']} ({e})"
)
break
for step in task["cleanup"]:
start_time = time.time()
try:
start_time = time.time()
getattr(task["tools"][step["type"]][step["tool"]], step["func"])()
end_time = time.time()
self.log.info(
f"Cleaned ({end_time - start_time:.2f}s): {task_name}/{step['type']}/{step['tool']}/{step['func']}"
)
except Exception as e:
end_time = time.time()
self.log.error(
f"Clean failed ({end_time - start_time:.2f}s): {task_name}/{step['type']}/{step['tool']}/{step['func']} ({e})"
)