Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Server class octoprint_daemon-aware, make sure terminated() gets… #1330

Merged
merged 1 commit into from May 12, 2016
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Make Server class octoprint_daemon-aware, make sure terminated() gets…

… invoked on it upon SIGTERM and update respective test
  • Loading branch information...
kevans91 committed May 6, 2016
commit 75f8facb058cb5a260535ba8c8c138da3b46117f
@@ -11,7 +11,7 @@

from octoprint.cli import pass_octoprint_ctx, bulk_options, standard_options

def run_server(basedir, configfile, host, port, debug, allow_root, logging_config, verbosity):
def run_server(basedir, configfile, host, port, debug, allow_root, logging_config, verbosity, octoprint_daemon = None):
"""Initializes the environment and starts up the server."""

from octoprint import init_platform, __display_version__
@@ -38,7 +38,7 @@ def log_startup(_):
after_logging=log_startup)

from octoprint.server import Server
octoprint_server = Server(settings=settings, plugin_manager=plugin_manager, host=host, port=port, debug=debug, allow_root=allow_root)
octoprint_server = Server(settings=settings, plugin_manager=plugin_manager, host=host, port=port, debug=debug, allow_root=allow_root, octoprint_daemon=octoprint_daemon)
octoprint_server.run()

#~~ server options
@@ -114,7 +114,7 @@ def __init__(self, pidfile, basedir, configfile, host, port, debug, allow_root,
self._verbosity = verbosity

def run(self):
run_server(self._basedir, self._configfile, self._host, self._port, self._debug, self._allow_root, self._logging_config, self._verbosity)
run_server(self._basedir, self._configfile, self._host, self._port, self._debug, self._allow_root, self._logging_config, self._verbosity, self)

octoprint_daemon = OctoPrintDaemon(pid, octoprint_ctx.basedir, octoprint_ctx.configfile,
host, port, debug, allow_root, logging, octoprint_ctx.verbosity)
@@ -31,9 +31,6 @@ def _daemonize(self):
pid = str(os.getpid())
self.set_pid(pid)

# register listener for SIGTERM
signal.signal(signal.SIGTERM, self._on_sigterm)

def _double_fork(self):
try:
pid = os.fork()
@@ -71,10 +68,8 @@ def _redirect_io(self):
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())

def _on_sigterm(self, _signo, _stack_frame):
"""Signal handler for SIGTERM, deletes the pidfile."""
def terminated(self):
self.remove_pidfile()
sys.exit(0)

def start(self):
"""Start the daemon."""
@@ -113,13 +113,14 @@ def load_user(id):


class Server(object):
def __init__(self, settings=None, plugin_manager=None, host="0.0.0.0", port=5000, debug=False, allow_root=False):
def __init__(self, settings=None, plugin_manager=None, host="0.0.0.0", port=5000, debug=False, allow_root=False, octoprint_daemon=None):
self._settings = settings
self._plugin_manager = plugin_manager
self._host = host
self._port = port
self._debug = debug
self._allow_root = allow_root
self._octoprint_daemon = octoprint_daemon
self._server = None

self._logger = None
@@ -515,6 +516,11 @@ def on_shutdown():
octoprint.plugin.call_plugin(octoprint.plugin.ShutdownPlugin,
"on_shutdown",
sorting_context="ShutdownPlugin.on_shutdown")

if self._octoprint_daemon is not None:
self._logger.info("Cleaning up daemon pidfile")
self._octoprint_daemon.terminated()

self._logger.info("Goodbye!")
atexit.register(on_shutdown)

@@ -155,25 +155,16 @@ def test_daemonize(self, mock_signal, mock_getpid):
self.daemon._double_fork.assert_called_once_with()
self.daemon._redirect_io.assert_called_once_with()
self.daemon.set_pid.assert_called_once_with(str(pid))
mock_signal.assert_called_once_with(signal.SIGTERM, self.daemon._on_sigterm)

@mock.patch("sys.exit")
def test_on_sigterm(self, mock_exit):
def test_terminated(self):
# setup
self.daemon.remove_pidfile = mock.MagicMock()

mock_exit.side_effect = ExpectedExit

# test
try:
self.daemon._on_sigterm("foo", "bar")
self.fail("Expected an exit")
except ExpectedExit:
pass
self.daemon.terminated()

# assert
self.daemon.remove_pidfile.assert_called_once_with()
mock_exit.assert_called_once_with(0)

def test_start(self):
# setup
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.