Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion xcube/server/webservers/tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import concurrent.futures
import functools
import logging
import signal
import sys
import traceback
import urllib.parse
from typing import (Any, Optional, Sequence, Union, Callable, Type,
Expand Down Expand Up @@ -64,6 +66,7 @@ def __init__(self,
io_loop: Optional[tornado.ioloop.IOLoop] = None):
self._application = application or tornado.web.Application()
self._io_loop = io_loop
self._server: Optional[tornado.web.HTTPServer] = None
self.configure_logging()

@property
Expand Down Expand Up @@ -149,7 +152,9 @@ def start(self, ctx: Context):
address = config["address"]
tornado_settings = config.get("tornado", {})

self.application.listen(port, address=address, **tornado_settings)
self._server = self.application.listen(port,
address=address,
**tornado_settings)

address_ = "127.0.0.1" if address == "0.0.0.0" else address
# TODO: get test URL template from configuration
Expand All @@ -158,9 +163,13 @@ def start(self, ctx: Context):
LOG.info(f"Try {test_url}")
LOG.info(f"Press CTRL+C to stop service")

self.configure_signals()

self.io_loop.start()

def stop(self, ctx: Context):
if self._server is not None:
self._server.stop()
self.io_loop.stop()

def call_later(self,
Expand Down Expand Up @@ -188,6 +197,14 @@ def run_in_executor(
*args
)

def configure_signals(self):
def sig_handler(signum, frame):
self.io_loop.add_callback_from_signal(self.stop)

signal.signal(signal.SIGTERM, sig_handler)
signal.signal(signal.SIGINT, sig_handler)
signal.signal(signal.SIGABRT, sig_handler)

@staticmethod
def configure_logging():
# Configure Tornado loggers to use root handlers, so we
Expand Down