Skip to content

Commit

Permalink
Shutdown kernels on exit and SIGTERM
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpio authored and SylvainCorlay committed Feb 12, 2019
1 parent 0e2ecac commit f66556d
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion voila/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from zmq.eventloop import ioloop
import os
import shutil
import signal
import tempfile
import logging
import gettext
Expand Down Expand Up @@ -131,6 +132,10 @@ def _default_root_dir(self):
else:
return getcwd()

def initialize(self, argv=None):
super(Voila, self).initialize(argv)
signal.signal(signal.SIGTERM, self._handle_signal_stop)

def parse_command_line(self, argv=None):
super(Voila, self).parse_command_line(argv)
self.notebook_path = self.extra_args[0] if len(self.extra_args) == 1 else None
Expand All @@ -150,6 +155,10 @@ def parse_command_line(self, argv=None):
if self.notebook_path and not os.path.exists(self.notebook_path):
raise ValueError('Notebook not found: %s' % self.notebook_path)

def _handle_signal_stop(self, sig, frame):
self.log.info('Handle signal %s.' % sig)
self.ioloop.add_callback_from_signal(self.ioloop.stop)

def start(self):
self.connection_dir = tempfile.mkdtemp(
prefix='voila_',
Expand Down Expand Up @@ -245,10 +254,15 @@ def listen(self):
self.app.listen(self.port)
self.log.info('Voila listening on port %s.' % self.port)

self.ioloop = tornado.ioloop.IOLoop.current()
try:
tornado.ioloop.IOLoop.current().start()
self.ioloop.start()
except KeyboardInterrupt:
self.log.info('Stopping...')
finally:
shutil.rmtree(self.connection_dir)
kernel_manager.shutdown_all()
shutil.rmtree(connection_dir)


main = Voila.launch_instance

0 comments on commit f66556d

Please sign in to comment.