-
Notifications
You must be signed in to change notification settings - Fork 10
/
test.py
executable file
·108 lines (77 loc) · 2.81 KB
/
test.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
#!/usr/bin/env python
import boto
import sys
import json
import time
import boto.sqs
import config_manager as conf_man
import uuid
import sns_sqs
import dynamo_utils as dutils
import argparse
import bottle
from bottle import template
import ast
def update_record(record, key, value):
record[key] = value
record.save(overwrite=True)
return
def submit_task(app, task_desc_file):
uid = str(uuid.uuid1())
t = int(time.time())
tstamp = str(time.strftime('%Y-%m-%d %H:%M:%S'))
task_desc = template(task_desc_file,
uid=uid,
time=t,
tstamp=tstamp)
print "-"*50
print task_desc
print "-"*50
data = ast.literal_eval(task_desc)
dutils.dynamodb_update(app.config["dyno.conn"], data)
sns_sqs.publish(app.config["sns.conn"], app.config["instance.tags"]["JobsSNSTopicARN"],
data)
return uid
def debug_print(string):
if GLOBAL_VERBOSE :
print string
def cancel_task(app, jobid):
debug_print("Cancelling task : {0}".format(jobid))
record = dutils.dynamodb_get(app.config["dyno.conn"], jobid)
tstamp = str(time.strftime('%Y-%m-%d %H:%M:%S'))
update_record(record, "status", "cancelled")
update_record(record, "reason", "User request cancel")
update_record(record, "cancel_time", tstamp)
debug_print ("{0} - {1} - {2}".format(record["job_id"], record["status"], record["reason"]))
return True
def status_task(app, jobid):
debug_print("Status task : {0}".format(jobid))
record = dutils.dynamodb_get(app.config["dyno.conn"], jobid)
status = {}
if GLOBAL_VERBOSE:
for item in record.items():
print "|{0:10} | {1:50}".format(item[0], item[1])
print record["status"]
return record["status"]
GLOBAL_VERBOSE=False
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-j", "--jobinfo", help="json job description or jobid", required=True)
parser.add_argument("-r", "--request", help="Request type [submit, status, cancel]", required=True)
parser.add_argument("-c", "--conffile", default="production.conf", help="Config file path. Defaults to ./test.conf")
parser.add_argument("-v", "--verbose", dest='verbose', action='store_true', help="Verbose output")
args = parser.parse_args()
if args.verbose is True:
GLOBAL_VERBOSE=True
app = conf_man.load_configs(args.conffile)
if args.request.lower() == "submit":
uid = submit_task(app, args.jobinfo)
print "Uid : {0}".format(uid)
elif args.request.lower() == "status":
status_task(app, args.jobinfo)
elif args.request.lower() == "cancel":
cancel_task(app, args.jobinfo)
else:
print "Unknown request"
exit(-1)
exit(0)