-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatchy.py
224 lines (176 loc) · 6.02 KB
/
batchy.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
from ConfigParser import ConfigParser as Conf
import os
import sys
import logging
from datetime import datetime, timedelta
import time
import yaml
import json
import redis
from flask import Flask, render_template, Response, request
# ------------------------------------------------------------------------
os.chdir(os.path.dirname(sys.argv[0]))
# ------------------------------------------------------------------------
# declare application
app = Flask(__name__, static_url_path = "/static", static_folder = "static")
app.debug = True
# ------------------------------------------------------------------------
# helper
def json_serial(data):
"""
JSON serializer for objects not serializable by default json code"
:param obj:
:return:
"""
if isinstance(data, datetime):
serial = data.isoformat()
return serial
raise TypeError ("Type not serializable")
def render_response(data, format='json', status=200):
if format == 'json':
wdata = json.dumps(data,default=json_serial, indent=4)
mimetype = "application/json"
elif format == 'infa':
wdata = infa_param(data)
mimetype = None
else:
status = 500
resp = Response(response=wdata, status=status,)
return resp
def parse_yaml(jobname):
"""
reads yaml file into python object
:return: checklist
"""
with open(("cfg/%s.yaml") % jobname, 'r') as yam:
pyam = yaml.load(yam)
return pyam
def infa_param(data):
result = ''
for k, v in data.iteritems():
result += '\n[' + str(k) + ']\n'
for k1, v1, in v.iteritems():
result += '$$' + str(k1) + '=' + str(v1) + '\n'
return result
class RedisInteraction(object):
"""
small wrapper on redis interaction to handle json serdes
"""
def __init__(self):
cfg_path = "{0}/batchy.cfg".format(sys.path[0])
c = Conf()
c.read(cfg_path)
server = c.get('redis', 'server')
self.rconn = redis.StrictRedis(host=server, port=6379, db=0)
def h_write(self, key, data):
for k,v in data.iteritems():
self.rconn.hset(key, k, json.dumps(v, default=json_serial))
def h_write_batch(self, key, data):
batch_id = None
for v in data.itervalues():
if v.get('batch_id',None) != None:
batch_id = v.get('batch_id')
batch_key = key + '-' + str(batch_id if batch_id != None else str(int(time.time())) + '-init')
r.h_write(batch_key, data)
r.h_write(key, data)
def h_getall(self, key):
data = self.rconn.hgetall(key)
result={}
for k, v in data.iteritems():
result[k] = json.loads(v)
return result
# ------------------------------------------------------------------------
# flask stuff
@app.route('/index') #http://localhost:5000/index
def index():
return 'Batchy v0.00001'
# ------------------------------------------------------------------------
# load and status interactions
@app.route('/load_cfg/<string:wf>', endpoint='load_cfg')
def load_cfg(wf):
"""
load state of a workflow from yaml, will delete and replace values for given workflow
:param wf:
:return:
"""
cfg = parse_yaml(wf)
r.rconn.delete(wf)
r.h_write_batch(wf, cfg)
return render_response(cfg)
@app.route('/get_status/<string:wf>', endpoint='get_status')
def get_status(wf):
"""
get config
:param wf:
:return:
"""
cfg = r.h_getall(wf)
return render_response(cfg)
# ------------------------------------------------------------------------
# batch interactions
@app.route('/open_batch/<string:wf>/<string:fmt>', endpoint='open_batch1')
@app.route('/open_batch/<string:wf>', endpoint='open_batch')
def open_batch(wf, fmt='json'):
"""
get's state of last run and opens batch based on params
:param batch:
:return:
"""
batch = r.h_getall(wf)
from_date = datetime.utcnow()
batch_id = int(time.time()) # datetime.utcnow().strftime('%Y%m%d-%H%M%S-%f')[:-3]
# now iterate and assign values for new batch
for k, v in batch.iteritems():
# now we look at prior status and compute the from_date
if v.get('status') == 'success':
offset = v.get('reprocess_hours', 0)
old_batch_start = datetime.strptime(v.get('batch_start', '1776-07-04T23:59:00.000'), "%Y-%m-%dT%H:%M:%S.%f")
from_date = old_batch_start - timedelta(hours=offset)
if v.get('from_date') == None:
from_date = datetime.strptime('1776-07-04T23:59:00.000', "%Y-%m-%dT%H:%M:%S.%f")
if v.get('trunc_start', False) == True:
from_date = from_date.replace(minute=0, hour=0, second=0, microsecond=0)
batch[k]['from_date'] = from_date
batch[k]['batch_start'] = datetime.utcnow()
batch[k]['batch_end'] = None
batch[k]['status'] = 'open'
batch[k]['batch_id'] = batch_id
infa_param(batch)
#write new batch
r.h_write_batch(wf, batch)
return render_response(batch, fmt)
@app.route('/close_batch/<string:wf>', endpoint='close_batch')
def close_batch(wf):
"""
closes batch
:param batch:
:return:
"""
batch = r.h_getall(wf)
for k, v in batch.iteritems():
batch[k]['batch_end'] = datetime.utcnow()
batch[k]['status'] = 'success'
r.h_write_batch(wf, batch)
return render_response(batch)
@app.route('/fail_batch/<string:wf>', endpoint='fail_batch')
def fail_batch(wf):
"""
closes batch
:param batch:
:return:
"""
batch = r.h_getall(wf)
for k, v in batch.iteritems():
batch[k]['batch_end'] = datetime.utcnow()
batch[k]['status'] = 'failure'
r.h_write_batch(wf, batch)
return render_response(batch)
# ------------------------------------------------------------------------
#logging wrapper:
def log(message):
logging.info(message)
print(message)
# ------------------------------------------------------------------------
if __name__ == '__main__':
r = RedisInteraction()
app.run(host='0.0.0.0', debug = True)