summaryrefslogtreecommitdiffstats
path: root/aurweb/spawn.py
diff options
context:
space:
mode:
authorFrédéric Mangano-Tarumi <fmang@mg0.fr>2020-04-19 20:11:02 +0200
committerLukas Fleischer <lfleischer@archlinux.org>2021-02-20 17:24:30 +0100
commit8a13500535942a1c99b97ae4de46e5a1c0297cd5 (patch)
tree1a6b785264c9ea60a63a9d46228c038b5e008267 /aurweb/spawn.py
parent23f6dd16a7c8b6f81c229e2307838edd213d6149 (diff)
downloadaur-8a13500535942a1c99b97ae4de46e5a1c0297cd5.tar.gz
aur-8a13500535942a1c99b97ae4de46e5a1c0297cd5.tar.xz
Create aurweb.spawn for spawing the test server
This program makes it easier for developers to spawn the PHP server since it fetches automatically what it needs from the configuration file, rather than having the user explicitly pass arguments to the php executable. When the setup gets more complicated as we introduce Python, aurweb.spawn will keep providing the same interface, while under the hood it is planned to support running multiple sub-processes. Its Python interface provides an way for the test suite to spawn the test server when it needs to perform HTTP requests to the test server. The current implementation is somewhat weak as it doesn’t detect when a child process dies, but this is not supposed to happen often, and it is only meant for aurweb developers. In the long term, aurweb.spawn will eventually become obsolete, and replaced by Docker or Flask’s tools. Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
Diffstat (limited to 'aurweb/spawn.py')
-rw-r--r--aurweb/spawn.py107
1 files changed, 107 insertions, 0 deletions
diff --git a/aurweb/spawn.py b/aurweb/spawn.py
new file mode 100644
index 00000000..5fa646b5
--- /dev/null
+++ b/aurweb/spawn.py
@@ -0,0 +1,107 @@
+"""
+Provide an automatic way of spawing an HTTP test server running aurweb.
+
+It can be called from the command-line or from another Python module.
+
+This module uses a global state, since you can’t open two servers with the same
+configuration anyway.
+"""
+
+
+import atexit
+import argparse
+import subprocess
+import sys
+import time
+import urllib
+
+import aurweb.config
+import aurweb.schema
+
+
+children = []
+verbosity = 0
+
+
+class ProcessExceptions(Exception):
+ """
+ Compound exception used by stop() to list all the errors that happened when
+ terminating child processes.
+ """
+ def __init__(self, message, exceptions):
+ self.message = message
+ self.exceptions = exceptions
+ messages = [message] + [str(e) for e in exceptions]
+ super().__init__("\n- ".join(messages))
+
+
+def spawn_child(args):
+ """Open a subprocess and add it to the global state."""
+ if verbosity >= 1:
+ print(f"Spawning {args}", file=sys.stderr)
+ children.append(subprocess.Popen(args))
+
+
+def start():
+ """
+ Spawn the test server. If it is already running, do nothing.
+
+ The server can be stopped with stop(), or is automatically stopped when the
+ Python process ends using atexit.
+ """
+ if children:
+ return
+ atexit.register(stop)
+ aur_location = aurweb.config.get("options", "aur_location")
+ aur_location_parts = urllib.parse.urlsplit(aur_location)
+ htmldir = aurweb.config.get("options", "htmldir")
+ spawn_child(["php", "-S", aur_location_parts.netloc, "-t", htmldir])
+
+
+def stop():
+ """
+ Stop all the child processes.
+
+ If an exception occurs during the process, the process continues anyway
+ because we don’t want to leave runaway processes around, and all the
+ exceptions are finally raised as a single ProcessExceptions.
+ """
+ global children
+ atexit.unregister(stop)
+ exceptions = []
+ for p in children:
+ try:
+ p.terminate()
+ if verbosity >= 1:
+ print(f"Sent SIGTERM to {p.args}", file=sys.stderr)
+ except Exception as e:
+ exceptions.append(e)
+ for p in children:
+ try:
+ rc = p.wait()
+ if rc != 0 and rc != -15:
+ # rc = -15 indicates the process was terminated with SIGTERM,
+ # which is to be expected since we called terminate on them.
+ raise Exception(f"Process {p.args} exited with {rc}")
+ except Exception as e:
+ exceptions.append(e)
+ children = []
+ if exceptions:
+ raise ProcessExceptions("Errors terminating the child processes:",
+ exceptions)
+
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser(
+ prog='python -m aurweb.spawn',
+ description='Start aurweb\'s test server.')
+ parser.add_argument('-v', '--verbose', action='count', default=0,
+ help='increase verbosity')
+ args = parser.parse_args()
+ verbosity = args.verbose
+ start()
+ try:
+ while True:
+ time.sleep(60)
+ except KeyboardInterrupt:
+ stop()