diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-12-31 11:02:32 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-12-31 11:02:32 +0200 |
commit | 8c4729010511d7bd491675522378507c2af0b583 (patch) | |
tree | 2cbff9a84d0c401828ebd7c9129068600b3cb9ea /subplot/daemon.py | |
parent | 89c2c522682ea0490f3512e43bf855b254cd924f (diff) | |
download | obnam2-8c4729010511d7bd491675522378507c2af0b583.tar.gz |
chore: update vendored subplot librarires: runcmd, daemon
Diffstat (limited to 'subplot/daemon.py')
-rw-r--r-- | subplot/daemon.py | 92 |
1 files changed, 0 insertions, 92 deletions
diff --git a/subplot/daemon.py b/subplot/daemon.py deleted file mode 100644 index 4d60fd0..0000000 --- a/subplot/daemon.py +++ /dev/null @@ -1,92 +0,0 @@ -############################################################################# -# Start and stop daemons, or background processes. - - -import logging -import os -import signal -import time - - -# Start a process in the background. -def start_daemon(ctx, name, argv): - runcmd_run = globals()["runcmd_run"] - runcmd_exit_code_is = globals()["runcmd_exit_code_is"] - runcmd_get_exit_code = globals()["runcmd_get_exit_code"] - runcmd_get_stderr = globals()["runcmd_get_stderr"] - runcmd_prepend_to_path = globals()["runcmd_prepend_to_path"] - - logging.debug(f"Starting daemon {name}") - logging.debug(f" ctx={ctx.as_dict()}") - logging.debug(f" name={name}") - logging.debug(f" argv={argv}") - - if "daemon" not in ctx.as_dict(): - ctx["daemon"] = {} - assert name not in ctx["daemon"] - this = ctx["daemon"][name] = { - "pid-file": f"{name}.pid", - "stderr": f"{name}.stderr", - "stdout": f"{name}.stdout", - } - # Debian up to 10 installs `daemonize` to /usr/sbin, which isn't part of - # the minimal environment that Subplot sets up. - runcmd_prepend_to_path(ctx, "/usr/sbin") - runcmd_run( - ctx, - [ - "daemonize", - "-c", - os.getcwd(), - "-p", - this["pid-file"], - "-e", - this["stderr"], - "-o", - this["stdout"], - ] - + argv, - ) - - # Wait for a bit for daemon to start and maybe find a problem and die. - time.sleep(3) - exit = runcmd_get_exit_code(ctx) - stderr = runcmd_get_stderr(ctx) - if exit != 0: - logging.error(f"obnam-server stderr: {stderr}") - - runcmd_exit_code_is(ctx, 0) - this["pid"] = int(open(this["pid-file"]).read().strip()) - assert process_exists(this["pid"]) - - logging.debug(f"Started daemon {name}") - logging.debug(f" ctx={ctx.as_dict()}") - - -# Stop a daemon. -def stop_daemon(ctx, name): - logging.debug(f"Stopping daemon {name}") - logging.debug(f" ctx={ctx.as_dict()}") - logging.debug(f" ctx['daemon']={ctx.as_dict()['daemon']}") - - this = ctx["daemon"][name] - terminate_process(this["pid"], signal.SIGKILL) - - -# Does a process exist? -def process_exists(pid): - try: - os.kill(pid, 0) - except ProcessLookupError: - return False - return True - - -# Terminate process. -def terminate_process(pid, signalno): - logging.debug(f"Terminating process {pid} with signal {signalno}") - try: - os.kill(pid, signalno) - except ProcessLookupError: - logging.debug("Process did not actually exist (anymore?)") - pass |