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
|
#!/usr/bin/env python3
"""
Runs a process and then kills it after a timeout while monitoring
its stdout.
"""
import subprocess
import os
import sys
import time
import threading
STARTED_TAG = b'Ubuntu 20.04'
os.chdir(os.path.dirname(os.path.abspath(__file__)))
class ProcessMonitor(threading.Thread):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.process = None
self.t0 = 0
self.timeout = 120
def run(self):
self.process = subprocess.Popen('./run-docker.sh', stdout=subprocess.PIPE)
for line in iter(lambda: self.process.stdout.readline(), b''):
sys.stdout.buffer.write(line)
if STARTED_TAG in line:
print('Starting timeout countdown.')
self.t0 = time.time()
@property
def stale_seconds(self):
return time.time() - self.t0
def is_alive(self):
return self.process is None or self.process.poll() is None
def fresh(self):
if not self.t0:
return True
if self.stale_seconds < self.timeout:
return True
return False
t = ProcessMonitor()
t.daemon = True
print('\rStarting run-docker.sh.')
t.start()
while 1:
if not t.is_alive():
print('\rProcess has exited.')
break
if t.fresh():
if not t.t0:
print('\rWaiting for console tag.')
else:
print('\rStale for %s seconds.' % t.stale_seconds)
else:
print('\rKilling stale process...')
os.system('./close-docker.sh')
break
time.sleep(5)
|