forked from tingbot/tbprocessd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tbprocessd.py
293 lines (215 loc) Β· 6.83 KB
/
tbprocessd.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
#!/usr/bin/env python
import os, time, socket, subprocess, logging, errno, json, sys, textwrap, string
import zmq
try:
from http.server import HTTPServer, BaseHTTPRequestHandler
except ImportError:
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from fcntl import fcntl, F_GETFL, F_SETFL
from select import select
from io import BlockingIOError
try:
from time import monotonic
except ImportError:
monotonic = time.time
HOME_APP = os.environ.get('HOME_APP', 'testing/home.py')
STARTUP_APP = os.environ.get('STARTUP_APP', HOME_APP)
# set the python unbuffered flag
# this makes the app's logs stream to UDP realtime, but only for python programs
# other programs will have to manually call `flush` in their logging routines
os.environ['PYTHONUNBUFFERED'] = '1'
verbose = False
def main():
console_setup()
console_message('tbprocessd starting...')
http_setup()
log_stream_setup()
app_start(STARTUP_APP)
try:
run_loop()
finally:
if app_is_running():
app_stop()
def run_loop():
while True:
http_loop()
app_loop()
# pause the run loop until we see any new inputs
# this is more efficient than a sleep, since it only wakes the process when
# there's something to do
if app_process:
wait_fds = [httpd, app_process.stdout, app_process.stderr]
else:
wait_fds = [httpd]
select(wait_fds, [], [])
########
# HTTP #
########
httpd = None
class Handler(BaseHTTPRequestHandler):
def do_POST(self):
if self.path == '/run':
self.send_response(200)
self.end_headers()
content_length = self.headers['Content-Length']
if content_length:
app_path = self.rfile.read(int(content_length))
app_start(app_path=app_path)
self.request.sendall(b'OK')
return
else:
self.send_error(400)
self.send_error(404)
def log_message(self, *args, **kwargs):
if verbose:
super(Handler, self).log_message(*args, **kwargs)
def http_setup():
global httpd
bind_address = ('localhost', 10451)
httpd = HTTPServer(bind_address, Handler)
httpd.timeout = 0
def http_loop():
httpd.handle_request()
#######
# APP #
#######
app_process = None
def app_setup():
pass
def app_loop():
if not app_is_running():
app_start(HOME_APP)
app_pipe_output()
def app_start(app_path):
global app_process
if app_process:
app_stop()
console_message('Starting %s...' % app_path)
args = ['python', '-m', 'tbtool', 'tingbot_run', app_path]
app_process = subprocess.Popen(
args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# set the stdout and stderr pipes to be non-blocking
flags = fcntl(app_process.stdout, F_GETFL)
fcntl(app_process.stdout, F_SETFL, flags | os.O_NONBLOCK)
flags = fcntl(app_process.stderr, F_GETFL)
fcntl(app_process.stderr, F_SETFL, flags | os.O_NONBLOCK)
log_stream_send({'started': app_process.pid, 'path': app_path})
def app_stop():
global app_process
# pipe any remaining output before killing the process
app_pipe_output()
app_process.poll()
needs_termination = app_process.returncode is None
if needs_termination:
app_process.terminate()
# wait for termination (2 seconds)
wait_start = monotonic()
while app_is_running() and monotonic() < wait_start + 5.0:
app_pipe_output()
time.sleep(0.02)
if app_is_running():
logging.warning('App did not terminate 5 seconds after SIGTERM. Sending SIGTERM again...')
app_process.terminate()
while app_is_running() and monotonic() < wait_start + 10.0:
app_pipe_output()
time.sleep(0.02)
if app_is_running():
logging.warning('App did not terminate 10 seconds after SIGTERM. Sending SIGKILL...')
# send SIGKILL and wait indefinitely for termination
app_process.kill()
while app_is_running():
app_pipe_output()
time.sleep(0.02)
log_stream_send({'ended': app_process.pid, 'code': app_process.returncode, 'terminated': needs_termination})
app_process = None
def app_is_running():
if app_process is None:
return False
app_process.poll()
return app_process.returncode is None
class terminal_colors:
red = '\033[31m'
green = '\033[32m'
yellow = '\033[33m'
blue = '\033[34m'
cyan = '\033[36m'
bright_red = '\033[91m'
bright_green = '\033[92m'
bold = '\033[1m'
faint = '\033[2m'
end = '\033[0m'
def app_pipe_output():
if app_process is None:
return
stdout = app_nonblocking_read(app_process.stdout)
if stdout:
sys.stdout.write(stdout)
log_stream_send({'stdout': stdout})
stderr = app_nonblocking_read(app_process.stderr)
if stderr:
sys.stderr.write(stderr)
log_stream_send({'stderr': stderr})
def app_nonblocking_read(fd):
try:
return os.read(fd.fileno(), 1480)
except OSError as ex:
if ex.errno == errno.EWOULDBLOCK:
return None
else:
raise
##############
# LOG STREAM #
##############
zmq_socket = None
def log_stream_setup():
global zmq_socket
context = zmq.Context()
zmq_socket = context.socket(zmq.PUB)
zmq_socket.bind('tcp://127.0.0.1:10452')
def log_stream_send(msg):
message_str = json.dumps(msg) + '\n'
zmq_socket.send(message_str)
###########
# CONSOLE #
###########
rows = None
columns = None
wrapper = None
def console_setup():
global rows, columns, wrapper
try:
stty_output = subprocess.check_output(['stty', 'size']).split()
rows = int(stty_output[0])
columns = int(stty_output[1])
except subprocess.CalledProcessError:
rows = 0
columns = 0
wrapper = textwrap.TextWrapper(
width=columns-4,
initial_indent=' ',
subsequent_indent=' ')
def console_message(message):
# clear the screen
sys.stdout.write("\x1b[2J\x1b[H")
lines = wrapper.wrap(message)
before_padding_lines_count = (rows - len(lines)) // 2
after_padding_lines_count = rows - len(lines) - before_padding_lines_count
if before_padding_lines_count > 0:
before_padding_lines = '\n' * before_padding_lines_count
else:
before_padding_lines = ''
sys.stdout.write(before_padding_lines)
for line in lines:
sys.stdout.write(string.center(line, columns) + '\n')
if after_padding_lines_count > 0:
after_padding_lines = '\n' * after_padding_lines_count
else:
after_padding_lines = ''
sys.stdout.write(after_padding_lines)
########
# MAIN #
########
if __name__ == '__main__':
main()