forked from yadudoc/cloud_kotta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
task_executor.py
executable file
·369 lines (303 loc) · 12.6 KB
/
task_executor.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env python
import requests
import time, datetime
from datetime import datetime
import bottle
import logging
import argparse
import ast
from bottle import run, route, get, post, request, response
import json
import config_manager as conf_man
import task_executor_utils as utils
import os
import applications as apps
import s3_utils as s3
import dynamo_utils as dutils
import re
import shutil
import sys
import sts
import seppukku
import sns_sqs
import inspect
metadata_server="http://169.254.169.254/latest/meta-data/"
clean_tmp_dirs = True
def get_instance_starttime() :
try:
data = requests.get(metadata_server + "local-ipv4")
last_mod = data.headers['last-modified']
return datetime.strptime(last_mod, "%a, %d %b %Y %H:%M:%S %Z")
except e:
print "Caught exception : {0}".format(e)
def get_metainfo() :
try:
data = requests.get(metadata_server + "")
last_mod = data.headers['last-modified']
return datetime.strptime(last_mod, "%a, %d %b %Y %H:%M:%S %Z")
except e:
print "Caught exception : {0}".format(e)
##################################################################
# A rudimentary times for coarse-grained profiling
##################################################################
class Timer(object):
def __init__(self, verbose=False):
self.verbose = verbose
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, *args):
self.end = time.time()
self.secs = self.end - self.start
self.msecs = self.secs * 1000 # millisecs
if self.verbose:
print 'elapsed time: %f ms' % self.msecs
def get_inputs(app, inputs, auth):
try:
print "Attempting to switch roles to : {0}".format(auth["role"])
creds = sts.get_temp_creds(auth["role"])
#sts.print_creds(creds)
s3conn = s3.get_s3_conn( creds["AccessKeyId"],
creds["SecretAccessKey"],
creds["SessionToken"] )
except Exception as e:
print "Caught exception : {0}".format(e)
raise
print "In get_inputs"
if not inputs:
return
for i in inputs:
print "Staging_inputs : ", i
if i["src"].startswith("http://"):
print "Downloading {0} via http".format(i["src"])
utils.download_file(i["src"], i["dest"])
elif re.search("https://s3.*amazonaws.com/", i["src"]):
s3_path = re.sub("https://s3.*amazonaws.com/", "", i["src"])
tmp = s3_path.split('/', 1)
s3_bucket = tmp[0]
s3_key = tmp[1]
print "Downloading {0} via s3 provider".format(i["src"])
try:
s3.smart_download_s3_keys(s3conn,
s3_bucket,
s3_key,
i["dest"],
creds)
except Exception as e:
print "Download from s3 failed {0}".format(e)
raise
elif i["src"].startswith("s3://"):
s3_path = i["src"].strip("s3://")
tmp = s3_path.split('/', 1)
s3_bucket = tmp[0]
s3_key = tmp[1]
print "Downloading {0} via s3 provider".format(i["src"])
try:
s3.smart_download_s3_keys(s3conn,
s3_bucket,
s3_key,
i["dest"],
creds)
except Exception as e:
print "Download from s3 failed {0}".format(e)
raise
else:
print "No match. Could not fetch data"
return
def put_outputs(app, outputs):
print "In put_outputs"
if not outputs:
return
for out in outputs:
print "Outputs : ", out
'''
if not os.path.exists(out["src"]):
print "Missing file. Continuing"
continue
'''
target = out["dest"].split('/', 1)
try:
s3.smart_upload_s3_keys(app.config["s3.conn"],
out["src"], # Source filename
target[0], # Bucket name
target[1], # Prefix
{"Owner": "Yadu"})
except Exception as e:
print "Upload to s3 failed {0}".format(e)
return
def update_record(record, key, value):
record[key] = value
record.save(overwrite=True)
return
def exec_job(app, jobtype, job_id, executable, args, inputs, outputs, data, auth):
# Save current folder and chdir to a temporary folder
conf_man.update_creds_from_metadata_server(app)
record = dutils.dynamodb_get(app.config["dyno.conn"], job_id)
##############################################################################
# Notify job execution start time
##############################################################################
update_record(record, "start_time", time.time())
##############################################################################
# Setup dirs for execution
##############################################################################
cwd = os.getcwd()
tmpdir = "/tmp/task_executor_jobs/{0}".format(job_id)
try:
os.makedirs(tmpdir)
except:
print "Tmpdir {0} exists. Deleting and recreating".format(tmpdir)
shutil.rmtree(tmpdir)
os.makedirs(tmpdir)
os.chdir(tmpdir)
##############################################################################
# Download the inputs to the temp folder
##############################################################################
update_record(record, "status", "staging_inputs")
stagein_start = time.time()
try:
get_inputs(app, inputs, auth)
except Exception as e:
print "Exception info : ".format(sys.exc_info()[0])
update_record(record, "ERROR", "Failed to download inputs {0}".format(e))
update_record(record, "status", "failed")
update_record(record, "complete_time", time.time())
logging.error("Failed to download inputs")
return False
stagein_total = time.time() - stagein_start
##############################################################################
# Download the inputs to the temp folder
##############################################################################
# Check if job is valid
update_record(record, "status", "processing")
if jobtype not in apps.JOBS:
logging.error("Jobtype : {0} does not exist".format(jobtype))
print "Unable to process jobtype : {0}".format(jobtype)
return False
print "JOBS : ", apps.JOBS[jobtype]
status = True
returncode = 0
process_start = time.time()
try:
returncode = apps.JOBS[jobtype](app, data)
print "Returncode : {0}".format(returncode)
conf_man.update_creds_from_metadata_server(app)
except Exception as e:
update_record(record, "status", "Failed");
update_record(record, "complete_time", time.time())
update_record(record, "ERROR", str(e));
print "Job execution failed : {0}".format(e)
status = False
process_total = time.time() - process_start
##############################################################################
# Upload the results to the S3
##############################################################################
record = dutils.dynamodb_get(app.config["dyno.conn"], job_id)
update_record(record, "status", "staging_outputs")
stageout_start = time.time()
# Upload the result to S3
try:
put_outputs(app, outputs)
except Exception as e:
print "Exception info : ".format(sys.exc_info()[0])
update_record(record, "ERROR", "Failed to upload outputs {0}".format(e))
update_record(record, "status", "failed")
update_record(record, "complete_time", time.time())
logging.error( "Failed to upload inputs")
return False
stageout_total = time.time() - stageout_start
update_record(record, "z_stagein_dur", stagein_total)
update_record(record, "z_stageout_dur", stageout_total)
update_record(record, "z_processing_dur", process_total - 1)
if returncode != 0 :
update_record(record, "status", "failed");
update_record(record, "complete_time", time.time())
update_record(record, "ERROR_CODE", returncode);
status = False
else:
update_record(record, "status", "completed")
update_record(record, "complete_time", time.time())
if clean_tmp_dirs:
shutil.rmtree(tmpdir)
# Chdir back to the original folder
os.chdir(cwd)
return True
def task_loop(app):
sqs_conn = app.config["sqs.conn"]
pending = app.config["instance.tags"]["JobsQueueName"]
active = app.config["instance.tags"]["ActiveQueueName"]
pending_q = sqs_conn.get_queue(pending)
active_q = sqs_conn.get_queue(active)
while 1:
# Wait to read a message from the pending_q
msg = pending_q.read(wait_time_seconds=20)
print "Received message from pending_q"
if msg:
# Too many things could fail here, do a blanket
# Try catch
try:
sreq = json.loads(msg.get_body())["Message"]
if not sreq :
continue
app.config["current_msg_handle"] = msg
data = ast.literal_eval(sreq)
job_id = data.get('job_id')
jobtype = data.get('jobtype')
executable = data.get('executable')
args = data.get('args')
inputs = data.get('inputs')
inputs = data.get('inputs')
outputs = data.get('outputs')
user_auth = {"user" : data.get('i_user_id'),
"role" : data.get('i_user_role'),
"token" : data.get('i_token'),
"keyid" : data.get('i_keyid'),
"keysecret" : data.get('i_keysecret')}
# Post the job to the active queue and delete it from the pending queue
attr, current_msg = sns_sqs.post_message_to_active(app, active_q, msg.get_body(), job_id)
print "Posted job from pending to active queue"
if not pending_q.delete_message(msg):
print "Deleting message from pending queue failed"
for key in data:
print "{0} : {1}".format(key, data[key])
print "Starting task"
status = exec_job(app,
jobtype,
job_id,
executable,
args,
inputs,
outputs,
data,
user_auth)
print "Status : ", status
if status == True:
conf_man.send_success_mail(data, app)
else:
conf_man.send_failure_mail(data, app)
except Exception as e:
print "Job failed to complete : {0}".format(sys.exc_info()[0])
print "Trace : ", inspect.trace()
else:
print "{0}: Waiting for job description".format(time.time())
seppukku.die_at_hour_edge(app, dry_run=True)
logging.debug("{0}: Waiting for job description".format(time.time()))
conf_man.update_creds_from_metadata_server(app)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", default="DEBUG", help="set level of verbosity, DEBUG, INFO, WARN")
parser.add_argument("-l", "--logfile", default="task_executor.log", help="Logfile path. Defaults to ./task_executor.log")
parser.add_argument("-c", "--conffile", default="test.conf", help="Config file path. Defaults to ./test.conf")
parser.add_argument("-j", "--jobid", type=str, action='append')
parser.add_argument("-i", "--workload_id", default=None)
args = parser.parse_args()
if args.verbose not in conf_man.log_levels :
print "Unknown verbosity level : {0}".format(args.verbose)
print "Cannot proceed. Exiting"
exit(-1)
logging.basicConfig(filename=args.logfile, level=conf_man.log_levels[args.verbose],
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M')
logging.getLogger('boto').setLevel(logging.CRITICAL)
logging.debug("\n{0}\nStarting task_executor\n{0}\n".format("*"*50))
app = conf_man.load_configs(args.conffile);
task_loop(app)