-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRushTIScheduler.py
292 lines (242 loc) · 11.8 KB
/
RushTIScheduler.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
import datetime
import logging
import os
import shlex
import sys
def set_current_directory():
abspath = os.path.abspath(__file__)
directory = os.path.dirname(abspath)
# set current directory
os.chdir(directory)
return directory
APP_NAME = "RushTIScheduler"
CURRENT_DIRECTORY = set_current_directory()
LOGFILE = os.path.join(CURRENT_DIRECTORY, APP_NAME + ".log")
# CONFIG = os.path.join(CURRENT_DIRECTORY, "config.ini")
MSG_RUSHTISCHEDULER_STARTS = "{app_name} starts. Parameters: {parameters}."
MSG_RUSHTISCHEDULER_TOO_FEW_ARGUMENTS = "{app_name} needs to be executed with 3 arguments."
MSG_RUSHTISCHEDULER_ARGUMENT1_INVALID = "Argument 1 (path to file) invalid. File needs to exist."
MSG_RUSHTISCHEDULER_ARGUMENT2_INVALID = "Argument 2 (max workers) invalid. Argument needs to be an integer number."
MSG_RUSHTISCHEDULER_ARGUMENT3_INVALID = "Argument 3 (repository for output file) invalid. Repository needs to exist."
MSG_RUSHTISCHEDULER_ENDS = "{app_name} ends. {fails} fails out of {executions} executions. Elapsed time: {time}"
logging.basicConfig(
filename=LOGFILE,
format='%(asctime)s - ' + APP_NAME + ' - %(levelname)s - %(message)s',
level=logging.INFO)
def extract_info_from_line(line):
""" Translate one line from txt file into arguments for execution
:param: line: Arguments for execution. E.g. id="5" predecessors="2,3" instance="tm1srv01" process="Bedrock.Server.Wait" pWaitSec=5
:return: attributes
"""
attributes = {}
temp = []
for pair in shlex.split(line):
attribute, value = pair.split("=")
# if instance or process, needs to be case insensitive
if attribute.lower() == 'process' or attribute.lower() == 'instance':
attributes[attribute.lower()] = value.strip('"').strip()
# Convert string attribute value into list
elif attribute.lower() == 'predecessors':
temp = value.strip('"').strip().split(',')
if temp[0] == '':
attributes[attribute] = []
else:
attributes[attribute] = temp
# attributes (e.g. pWaitSec) are case sensitive in TM1 REST API !
else:
attributes[attribute] = value.strip('"').strip()
return attributes
def extract_info_from_file(file_path):
""" Read a file that respect specification for RushTIScheduler and transform it into dictionary named tasks
:param: file_path:
:return: tasks
"""
tasks = {}
task_attributes = {}
with open(file_path) as input_file:
lines = input_file.readlines()
# Build tasks dictionnay
for line in lines:
task_attributes = extract_info_from_line(line)
task_attributes["successors"] = []
tasks[task_attributes["id"]] = task_attributes
# Deduct the successors attribut and add it to the task_attributes
for task in tasks.values():
predecessors = task["predecessors"]
if len(predecessors) != 0:
for predecessor in predecessors:
successor = task["id"]
task_attributes = tasks[predecessor]
task_attributes["successors"].append(successor)
return tasks
def deduce_levels_of_tasks(**tasks):
""" ...
:param: tasks:
:return: levels
"""
levels = {}
task_attributes = {}
# level 0 contains all tasks without predecessors
level = 0
levels[level] = []
for task in tasks.values():
predecessors = task["predecessors"]
if len(predecessors) == 0:
levels[level].append(task["id"])
# Handek other levels
level = 0
for task in tasks:
level_tasks = levels[level]
next_level_created = False
for level_task in level_tasks:
task_attributes = tasks[level_task]
successors = task_attributes["successors"]
# Create next level if necessary and add successors to this new level
if len(successors) != 0:
if not(next_level_created):
precedent_level = level
level += 1
levels[level] = []
next_level_created = True
for successor in successors:
# test if task exists in current level
if not(successor in levels[level]):
levels[level].append(successor)
# Delet successor in precedent level
if successor in levels[precedent_level]:
levels[precedent_level].remove(successor)
return levels
def rearrange_tasks_in_levels(maximum_workers, **tasks):
""" ...
:param: tasks:
:return: levels
"""
levels = deduce_levels_of_tasks(**tasks)
levels_count = len(levels.items())
for task_key in tasks.keys():
index = 0
while index < levels_count:
level_key = index
level = levels[level_key]
if level_key + 1 < levels_count:
next_level = levels[level_key + 1]
if len(next_level) < maximum_workers:
for task in level:
successors = tasks[task]["successors"]
next_level_contains_successor = False
for successor in successors:
if next_level.count(successor) != 0:
next_level_contains_successor = True
if not(next_level_contains_successor):
# move task from level to next_level
levels[level_key].remove(task)
levels[level_key + 1].append(task)
index += 1
return levels
def output_to_rushti_task_file(input_file_path, maximum_workers, output_file_path):
""" Transform a file that respect specification for RushTIScheduler into a scheduled file for RushTI
:param: input_file_path:
:param: maximum_workers:
:param: output_file_path:
:return: True
"""
tasks = {}
levels ={}
tasks = extract_info_from_file(input_file_path)
# levels = deduce_levels_of_tasks(**tasks)
levels = rearrange_tasks_in_levels(maximum_workers, **tasks)
levels_count = len(levels.items())
# Delete former file if exists
if os.path.isfile(output_file_path):
os.remove(output_file_path)
with open(output_file_path, "a") as output_file:
index = 0
for level in levels.values():
index += 1
for task in level:
line =''
task_attributes = tasks[task]
for attribute_key, attribute_value in task_attributes.items():
if attribute_key.lower() != 'id' and attribute_key.lower() != 'predecessors' and attribute_key.lower() != 'successors':
line = line + attribute_key + '=' + '"' + attribute_value + '"' + ' '
output_file.write(line + '\n')
if index < levels_count:
output_file.write('wait\n')
return levels_count
def file_path_exists(file_path):
""" ...
:param: file_path:
:return: boolean
"""
exists = False
folders = file_path.split('\\')
path =''
index = 0
while index < (len(folders) - 1):
if path == '':
path = folders[index]
else:
path = path + '/' + folders[index]
index +=1
if os.path.exists(path):
exists = True
return exists
def translate_cmd_arguments(*args):
""" Translation and Validity-checks for command line arguments.
:param args:
:return: input_file_path, maximum_workers and output_file_path
"""
# three few arguments
if len(args) < 4:
msg = MSG_RUSHTISCHEDULER_TOO_FEW_ARGUMENTS.format(app_name=APP_NAME)
logging.error(msg)
sys.exit(msg)
# txt file doesnt exist
if not os.path.isfile(args[1]):
msg = MSG_RUSHTISCHEDULER_ARGUMENT1_INVALID
logging.error(msg)
sys.exit(msg)
# max_workers is not a number
if not args[2].isdigit():
msg = MSG_RUSHTISCHEDULER_ARGUMENT2_INVALID
logging.error(msg)
sys.exit(msg)
# txt repository for file doesnt exist
if not file_path_exists(args[3]):
msg = MSG_RUSHTISCHEDULER_ARGUMENT3_INVALID
logging.error(msg)
sys.exit(msg)
return args[1], args[2], args[3]
def exit_rushtischeduler(executions, successes, elapsed_time):
""" Exit RushTI with exit code 0 or 1 depending on the TI execution outcomes
:param executions: Number of executions
:param successes: Number of executions that succeeded
:param elapsed_time:
:return:
"""
fails = executions - successes
message = MSG_RUSHTISCHEDULER_ENDS.format(
app_name=APP_NAME,
fails=fails,
executions=executions,
time=str(elapsed_time))
if fails > 0:
logging.error(message)
sys.exit(message)
else:
logging.info(message)
sys.exit(0)
# receives three arguments: 1) path-to-input-txt-file, 2) max-workers, 3) path-to-output-txt-file
if __name__ == "__main__":
logging.info(MSG_RUSHTISCHEDULER_STARTS.format(
app_name=APP_NAME,
parameters=sys.argv))
# start timer
start = datetime.datetime.now()
# read commandline arguments
input_file_path, maximum_workers, output_file_path = translate_cmd_arguments(*sys.argv)
# execution
levels_count = output_to_rushti_task_file(input_file_path, int(maximum_workers), output_file_path)
# timing
duration = datetime.datetime.now() - start
exit_rushtischeduler(executions=1, successes=1, elapsed_time=duration)