-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrunners.py
339 lines (268 loc) · 10.5 KB
/
runners.py
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import logging
import pathlib
import time
from srt_utils.common import create_local_directory
from srt_utils.enums import Status
from srt_utils.exceptions import SrtUtilsException
# from srt_utils.logutils import ContextualLoggerAdapter
import srt_utils.objects as objects
import srt_utils.object_runners as object_runners
# LOGGER = logging.getLogger(__name__)
logger = logging.getLogger(__name__)
### Simple Factory ###
class SimpleFactory:
def create_object(self, obj_type: str, obj_config: dict) -> objects.IObject:
obj = None
if obj_type == 'tshark':
obj = objects.Tshark.from_config(obj_config)
elif obj_type == 'srt-xtransmit':
obj = objects.SrtXtransmit.from_config(obj_config)
else:
print('No matching object found')
return obj
def create_runner(self, obj, runner_type: str, runner_config: dict) -> object_runners.IObjectRunner:
runner = None
if runner_type == 'local-runner':
runner = object_runners.LocalRunner.from_config(obj, runner_config)
elif runner_type == 'remote-runner':
runner = object_runners.RemoteRunner.from_config(obj, runner_config)
else:
print('No matching runner found')
return runner
### ITestRunner -> SingleExperimentRunner, TestRunner, CombinedTestRunner ###
# The methods will be similar to IRunner
class Task:
def __init__(
self,
key: str,
obj: objects.IObject,
obj_runner: object_runners.IObjectRunner,
config: dict
):
"""
Class to store task details.
Task represents one step of a single experiment and contains
information 1) about an object (srt-xtransmit, tshark, etc.) and a way
to run it (on a remote machine or locally, in particular which object)
runner to use; 2) like time to sleep after object start/stop,
stop order if defined, etc.
Attributes:
key:
Task key.
obj:
`objects.IObject` object to run.
obj_runner:
`object_runners.IObjectRunner` object runner.
config:
Task config.
Config Example:
{
"obj_type": "srt-xtransmit",
"obj_config": {...} # See respective object config example
},
"runner_type": "local-runner",
"runner_config": {},
"sleep_after_start": 3, # Optional
"sleep_after_stop": 3, # Optional
"stop_order": 1 # Optional
},
"""
self.key = key
self.obj = obj
self.obj_runner = obj_runner
self.sleep_after_start = config.get('sleep_after_start')
self.sleep_after_stop = config.get('sleep_after_stop')
self.stop_order = config.get('stop_order')
def __str__(self):
return f'task-{self.key}'
class SingleExperimentRunner:
def __init__(
self,
collect_results_path: pathlib.Path,
ignore_stop_order: bool,
stop_after: int,
tasks: dict
):
"""
Class to run a single experiment.
Attributes:
collect_results_path:
`pathlib.Path` directory path where the results produced by
the experiment should be copied.
ignore_stop_order:
True/False depending on whether the stop order specified in
tasks' configs should be/should not be ignored when stopping
the experiment.
stop_after:
The time in seconds after which experiment should be stopped.
tasks:
A `dict_items` object with the list of tasks to run within
the experiments.
"""
self.collect_results_path = collect_results_path
self.ignore_stop_order = ignore_stop_order
self.stop_after = stop_after
self.tasks = []
factory = SimpleFactory()
for key, config in tasks:
config['obj_config']['prefix'] = key
config['runner_config']['collect_results_path'] = self.collect_results_path
obj = factory.create_object(config['obj_type'], config['obj_config'])
obj_runner = factory.create_runner(obj, config['runner_type'], config['runner_config'])
self.tasks += [Task(key, obj, obj_runner, config)]
self.is_started = False
self.is_stopped = False
# self.log = ContextualLoggerAdapter(LOGGER, {'context': type(self).__name__})
@staticmethod
def _create_directory(dirpath: pathlib.Path):
"""
Create local directory for saving experiment results.
Raises:
SrtUtilsException
"""
logger.info(
'[SingleExperimentRunner] Creating local directory for saving '
f'experiment results: {dirpath}'
)
created = create_local_directory(dirpath)
if not created:
raise SrtUtilsException(
'Directory for saving experiment results already exists: '
f'{dirpath}. Please use non-existing directory name and '
'start the experiment again. Existing directory contents '
'will not be deleted'
)
@classmethod
def from_config(cls, config: dict):
"""
Attributes:
config:
Single experiment config.
Config Example:
# TODO
"""
return cls(
pathlib.Path(config['collect_results_path']),
config['ignore_stop_order'],
config['stop_after'],
config['tasks'].items()
)
def start(self):
"""
Start single experiment.
Raises:
SrtUtilsException
"""
# self.log.info('Starting experiment')
logger.info('Starting single experiment')
if self.is_started:
raise SrtUtilsException(
'Experiment has been started already. Start can not be done'
)
self._create_directory(self.collect_results_path)
for task in self.tasks:
logging.info(f'Starting task: {task}')
task.obj_runner.start()
sleep_after_start = task.sleep_after_start
if sleep_after_start is not None:
logger.info(f'Sleeping {sleep_after_start}s after task start')
time.sleep(sleep_after_start)
self.is_started = True
def stop(self):
"""
Stop single experiment.
Raises:
SrtUtilsException
"""
logger.info(f'Stopping single experiment')
not_stopped_tasks = 0
if not self.is_started:
raise SrtUtilsException(
'Experiment has not been started yet. Stop can not be done'
)
if self.is_stopped:
logger.info('Experiment has been stopped already. Nothing to do')
return
logger.info(f'Stopping tasks in reversed order')
# By default, stop the tasks in reverse order
# TODO: Implement stopping tasks according to the specified stop order.
# if self.ignore_stop_order:
for task in reversed(self.tasks):
logging.info(f'Stopping task: {task}')
# This try/except block is needed here in order to stop as much
# tasks as we can in case of something has failed
try:
task.obj_runner.stop()
except SrtUtilsException as error:
logger.error(f'Failed to stop task: {task}. Reason: {error}')
not_stopped_tasks += 1
continue
finally:
sleep_after_stop = task.sleep_after_stop
if sleep_after_stop is not None:
logger.info(f'Sleeping {sleep_after_stop}s after task stop')
time.sleep(sleep_after_stop)
if not_stopped_tasks != 0:
raise SrtUtilsException('Not all the tasks have been stopped')
self.is_stopped = True
def collect_results(self):
"""
Collect experiment results.
Raises:
SrtUtilsException
"""
logger.info('Collecting experiment results')
if not self.is_started:
raise SrtUtilsException(
'Experiment has not been started yet. Can not collect results'
)
# This is done to prevent the situation when the experiment is still
# running and we are trying to collect results before stopping it
if not self.is_stopped:
raise SrtUtilsException(
'Experiment is still running. Can not collect results'
)
for task in self.tasks:
logging.info(f'Collecting task results: {task}')
# This try/except block is needed here in order to collect results
# for as much tasks as we can in case of something has failed
try:
task.obj_runner.collect_results()
except SrtUtilsException as error:
logger.error(
f'Failed to collect task results: {task}. Reason: {error}'
)
continue
def clean_up(self):
"""
Perform cleaning up in case of something has gone wrong during
the experiment.
Raises:
SrtUtilsException
"""
logger.info('Cleaning up after experiment')
not_stopped_tasks = 0
for task in self.tasks:
if task.obj_runner.status == Status.running:
logging.info(f'Stopping task: {task}')
try:
task.obj_runner.stop()
except SrtUtilsException as error:
logger.error(
f'Failed to stop task: {task}, retrying to stop '
f'again. Reason: {error}'
)
try:
task.obj_runner.stop()
except SrtUtilsException as error:
logger.error(
f'Failed to stop task on the second try: {task}. '
f'Reason: {error}'
)
not_stopped_tasks += 1
continue
if not_stopped_tasks != 0:
raise SrtUtilsException(
'Not all the tasks have been stopped during cleaning up'
)
self.is_stopped = True