/
run.py
342 lines (267 loc) · 9.45 KB
/
run.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
#!/usr/bin/python
"""
This is an example of an infrastructure process that manages
app services running behind Mongrel2. run.py binds all the infrastructure
addresses for the service name passed to it and starts it up.
It runs 2 intervals, one to check for file change so it can restart the service,
and one that pings the service's checkup socket to make sure it's still responding,
restarting if necessary. It also prints all output from the service to stdout.
By running your service with run.py, you have a local debugging rig without
complicating your app code.
I'm using Tornado's IO Loop since a reactor loop simplifies interval code
(at least in my head). I need to decide on a config API that can be used no
matter what language the service is written in, right now it uses Python's
namespacing flexibility to hack together something that works.
"""
import os, sys, subprocess, signal, time
import hashlib, urllib2, errno, uuid, json
import zmq
from zmq.eventloop.ioloop import PeriodicCallback, DelayedCallback
from zmq.eventloop.zmqstream import ZMQStream
# constants
CHECK_INTERVAL = 1000
CHECKUP_INTERVAL = CHECK_INTERVAL * 2
CHECKUP_TIMEOUT = CHECKUP_INTERVAL / 2
PAUSE_BEFORE_RESTART = CHECK_INTERVAL / 2
PAUSE_AFTER_RESTART = PAUSE_BEFORE_RESTART
# helpers
def create_checksums(root,
nosync=None,
allowed_exts=None,
max_filesize=500 * 1024):
"""
Walk `root` and return a hash of `path`:`checksum` pairs
for each file sized under `max_filesize` that's not inside the
`nosync` list and has an extension in the `allowed_exts` list.
"""
checksums = {}
for path, dirs, files in os.walk(root):
dirs[:] = [d for d in dirs if d not in nosync]
for f in files:
full_path = os.path.join(path, f)
root_path = full_path.split(root)[-1].lstrip('/')
r, ext = os.path.splitext(f)
if ext in allowed_exts:
if os.stat(full_path).st_size < max_filesize:
try:
with open(full_path, 'rb') as d:
sha = hashlib.sha1()
key = "{filepath} {size}\0".format(
filepath=root_path,
size=os.path.getsize(full_path))
sha.update(key)
sha.update(d.read())
checksums[root_path] = sha.hexdigest()
except IOError as e:
raise e
return checksums
class DictDiffer(object):
"""
Calculate the difference between two dictionaries as:
(1) items added
(2) items removed
(3) keys same in both but changed values
(4) keys same in both and unchanged values
"""
def __init__(self, current_dict, past_dict):
self.current_dict, self.past_dict = current_dict, past_dict
self.set_current = set(current_dict.keys())
self.set_past = set(past_dict.keys())
self.intersect = self.set_current.intersection(self.set_past)
def added(self):
return self.set_current - self.intersect
def removed(self):
return self.set_past - self.intersect
def changed(self):
return set(o for o in self.intersect
if self.past_dict[o] != self.current_dict[o])
def unchanged(self):
return set(o for o in self.intersect
if self.past_dict[o] == self.current_dict[o])
def get_diff(new, old):
diff = DictDiffer(new, old)
return diff.added().union(diff.changed()), diff.removed()
def check(path, exts):
return create_checksums(path,
nosync=['.git'],
allowed_exts=exts)
def print_output(frames):
print(frames[0])
# flag for whether we can reach the service
responding = False
# routines
def launch_service():
global path, srv, KEY
try:
srv.terminate()
except NameError as e:
# first run of launch_service, so silence
pass
with open(os.devnull, 'w') as out:
CONFIG['service'].append(KEY)
srv = subprocess.Popen(CONFIG['service'],
cwd=path,
env=CONFIG['env'],
stdout=out,
stderr=out)
return srv
def send_checkup():
def checkup_timeout():
global srv, responding, timeout
timeout.stop()
if not responding:
# we've timed out, restart
# TODO: provide config var for how many times to attempt start before exiting
print('{0} not responding, attempting start.'.format(MODULE))
srv = launch_service()
def recv_checkup(msg):
global responding
responding = True
# access globals
global timeout, checkup, responding, loop
# listen for ping back
checkup.on_recv(recv_checkup)
# do what's needed to rescue on timeout
responding = False
timeout = DelayedCallback(checkup_timeout, CHECKUP_TIMEOUT, io_loop=loop)
timeout.start()
# send ping
checkup.send('You alive?')
def restart_service():
global srv
def restart_checkup():
global checkup_periodic
checkup_periodic.start()
global loop
checkup_restart = DelayedCallback(restart_checkup,
PAUSE_AFTER_RESTART,
io_loop=loop)
service = launch_service()
checkup_restart.start()
def check_for_change():
global checkup, loop, checksums, responding, checkup_periodic, KEY
curr_sums = check(path, watch)
changed, deleted = get_diff(curr_sums, checksums)
if len(changed) or len(deleted):
checksums = curr_sums
print('restarting {0}.'.format(MODULE))
checkup_periodic.stop()
command.send(json.dumps({
'key': KEY,
'command': 'die'
}))
delay = DelayedCallback(restart_service,
PAUSE_BEFORE_RESTART + 300,
io_loop=loop)
delay.start()
def start(root):
"""
Starts main change watching loop.
"""
global loop, srv, path, command, checkup, out
global checksums, watch, check_periodic, checkup_periodic
path = root
print('Starting.')
# define values for config vars
try:
service = CONFIG['service']
command = CONFIG['command']
out = CONFIG['out']
checkup = CONFIG['checkup']
try:
watch = CONFIG['watch']
except KeyError as e:
watch = ['.py', '.js', '.php', '.rb']
try:
env = CONFIG['env']
except KeyError as e:
env = {}
except KeyError as e:
print('''You must include CONFIG global in {0}.
Keys `command`, `command`, `checkup` and `out` are required.
example:
CONFIG = {
'service': ['python', 'service.py'],
'env': {'VAR1': 'abc', 'VAR2': 'xyz'},
'command': 'tcp://127.0.0.1:7004',
'checkup': 'tcp://127.0.0.1:7005',
'out': 'tcp://127.0.0.1:7006'
}
'''.format(MODULE))
sys.exit(1)
# define context
ctx = zmq.Context()
# create ioloop
loop = zmq.eventloop.ioloop.IOLoop()
# bind command address
s = ctx.socket(zmq.PUB)
s.bind(command)
command = ZMQStream(s, io_loop=loop)
# bind req to checkup
c = ctx.socket(zmq.REQ)
c.bind(checkup)
c.hwm = 1
checkup = ZMQStream(c, io_loop=loop)
# bind a sub to out address
o = ctx.socket(zmq.SUB)
o.setsockopt(zmq.SUBSCRIBE, '')
o.bind(out)
out = ZMQStream(o, io_loop=loop)
out.on_recv(print_output)
# define 'check for change' interval
check_periodic = PeriodicCallback(check_for_change,
CHECK_INTERVAL,
io_loop=loop)
# define 'checkup' interval
checkup_periodic = PeriodicCallback(send_checkup,
CHECKUP_INTERVAL,
io_loop=loop)
# start service
srv = launch_service()
# build filesystem state
checksums = check(path, watch)
# start the loop
check_periodic.start()
checkup_periodic.start()
loop.start()
def stop(signum, frame):
# clean up once command msg is sent
def sent(msg, status):
global loop, command, checkup, out
try:
command.close()
checkup.close()
out.close()
loop.stop()
except Exception as e:
print("Couldn't stop IO loop.")
sys.exit(1)
global loop, out, command, KEY
print('\nStopping.')
command.on_send(sent)
command.send(json.dumps({
'key': KEY,
'command': 'die'
}))
def main():
signal.signal(signal.SIGINT, stop)
signal.signal(signal.SIGTERM, stop)
root = os.getcwd()
start(root)
if __name__ == '__main__':
try:
KEY = str(uuid.uuid4())
MODULE = sys.argv[1]
CONFIG = __import__(MODULE).__dict__['CONFIG']
except IndexError as e:
print('You must pass the name of the module to run. Example: ')
print('> python run.py service')
sys.exit(1)
except ImportError as e:
print(e)
print('could not import `{0}` module to run.'.format(MODULE))
sys.exit(1)
except KeyError as e:
print('module `{0}` does not assign a CONFIG global.'.format(MODULE))
sys.exit(1)
main()