Skip to content

Commit

Permalink
improve interrupt catching when hosting
Browse files Browse the repository at this point in the history
  • Loading branch information
rjchallis committed Apr 1, 2020
1 parent 86d3358 commit 56ff620
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
26 changes: 16 additions & 10 deletions blobtools
Expand Up @@ -42,7 +42,8 @@ examples:
"""

import os
from subprocess import call
import signal
from subprocess import Popen

from docopt import docopt

Expand All @@ -57,20 +58,25 @@ if __name__ == '__main__':
options_first=True)
ARGV = [ARGS['<command>']] + ARGS['<args>']
if ARGS['<command>'] == 'add':
exit(call([LIBDIR + '/add.py'] + ARGV))
PROC = Popen([LIBDIR + '/add.py'] + ARGV)
elif ARGS['<command>'] == 'calc':
exit(call([LIBDIR + '/calc.py'] + ARGV))
PROC = Popen([LIBDIR + '/calc.py'] + ARGV)
elif ARGS['<command>'] == 'create':
exit(call([LIBDIR + '/add.py'] + ['add', '--create'] + ARGS['<args>']))
PROC = Popen([LIBDIR + '/add.py'] + ['add', '--create'] + ARGS['<args>'])
elif ARGS['<command>'] == 'filter':
exit(call([LIBDIR + '/filter.py'] + ARGV))
PROC = Popen([LIBDIR + '/filter.py'] + ARGV)
elif ARGS['<command>'] == 'host':
exit(call([LIBDIR + '/host.py'] + ARGV))
PROC = Popen([LIBDIR + '/host.py'] + ARGV)
elif ARGS['<command>'] == 'replace':
exit(call([LIBDIR + '/add.py'] + ['add', '--replace'] + ARGS['<args>']))
PROC = Popen([LIBDIR + '/add.py'] + ['add', '--replace'] + ARGS['<args>'])
elif ARGS['<command>'] == 'remove':
exit(call([LIBDIR + '/remove.py'] + ARGV))
PROC = Popen([LIBDIR + '/remove.py'] + ARGV)
elif ARGS['<command>'] == 'view':
exit(call([LIBDIR + '/view.py'] + ARGV))
PROC = Popen([LIBDIR + '/view.py'] + ARGV)
else:
exit(call([os.path.join(MAINDIR, './blobtools'), '-h']))
PROC = Popen([os.path.join(MAINDIR, './blobtools'), '-h'])

try:
PROC.wait()
except KeyboardInterrupt:
PROC.send_signal(signal.SIGINT)
8 changes: 5 additions & 3 deletions lib/host.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3

# pylint: disable=no-member, too-many-branches, too-many-statements, too-many-locals, W0603
# pylint: disable=no-member, too-many-branches, too-many-statements, too-many-locals, W0603, W0703

"""
Host a collection of BlobDirs.
Expand Down Expand Up @@ -160,10 +160,12 @@ def main():
if __name__ == '__main__':
try:
main()
except Exception as err:
except KeyboardInterrupt:
pass
finally:
for pid in PIDS:
print(pid)
try:
os.kill(pid, signal.SIGTERM)
except ProcessLookupError:
pass
raise err
26 changes: 15 additions & 11 deletions lib/view.py
Expand Up @@ -26,6 +26,7 @@
"""
import os
import shlex
import signal
import sys
import time
from pathlib import Path
Expand Down Expand Up @@ -94,8 +95,7 @@ def test_loc(args):
process = Popen(shlex.split(cmd),
stdout=PIPE,
stderr=PIPE,
encoding='ascii',
start_new_session=True)
encoding='ascii')
loc = "%s:%d/%s/dataset/%s" % (args['--host'], port, args['--prefix'], dataset)
for i in tqdm(range(0, 15),
unit='s',
Expand Down Expand Up @@ -217,13 +217,13 @@ def static_view(args, loc, viewer):
driver.quit()
display.popen.terminate()
if viewer is not None:
os.killpg(os.getpgid(viewer.pid), 15)
viewer.send_signal(signal.SIGINT)
except Exception as err:
print(err)
driver.quit()
display.popen.terminate()
if viewer is not None:
os.killpg(os.getpgid(viewer.pid), 15)
viewer.send_signal(signal.SIGINT)
return True


Expand All @@ -249,12 +249,12 @@ def interactive_view(args, loc, viewer):
poll = viewer.poll()
driver.quit()
display.popen.terminate()
os.killpg(os.getpgid(viewer.pid), 15)
viewer.send_signal(signal.SIGINT)
except Exception as err:
print(err)
driver.quit()
display.popen.terminate()
os.killpg(os.getpgid(viewer.pid), 15)
viewer.send_signal(signal.SIGINT)
return True


Expand All @@ -276,10 +276,11 @@ def remote_view(args, loc, viewer, port, api_port):
api_port))
while True:
time.sleep(5)
os.killpg(os.getpgid(viewer.pid), 15)
viewer.send_signal(signal.SIGINT)
except Exception as err:
print('remote exception')
print(err)
os.killpg(os.getpgid(viewer.pid), 15)
viewer.send_signal(signal.SIGINT)
return True


Expand All @@ -294,9 +295,12 @@ def main():
remote_view(args, loc, viewer, port, api_port)
else:
static_view(args, loc, viewer)
except Exception as err:
print(err)
os.killpg(os.getpgid(viewer.pid), 15)
except KeyboardInterrupt:
pass
finally:
time.sleep(1)
viewer.send_signal(signal.SIGINT)
time.sleep(1)


if __name__ == '__main__':
Expand Down

0 comments on commit 56ff620

Please sign in to comment.