[projects/pld-builder.new] request_handler_server: restart into new code by itself

arekm arekm at pld-linux.org
Thu Nov 12 11:43:38 CET 2026


commit 8ebb35a506911b8dc7fca146e36c1d818eb19014
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date:   Thu Nov 12 11:41:15 2026 +0100

    request_handler_server: restart into new code by itself
    
    The server runs for months in tmux, while make-request.sh --pull
    updates the code under it; until someone restarted it by hand it went
    on handling requests and writing queue.html with the old code. It now
    looks at its files once a second and, when they change, waits two
    seconds for the update to finish, waits for the requests under way and
    execs itself again: same process, same tmux window, new code, config,
    acl and blacklist.

 PLD_Builder/request_handler_server.py | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
---
diff --git a/PLD_Builder/request_handler_server.py b/PLD_Builder/request_handler_server.py
index 7d6c404..2420aaa 100644
--- a/PLD_Builder/request_handler_server.py
+++ b/PLD_Builder/request_handler_server.py
@@ -1,9 +1,12 @@
 import argparse
+import glob
+import os
 import re
 import signal
 import socket
 import ssl
 import sys
+import time
 import traceback
 from http import HTTPStatus
 from http.server import BaseHTTPRequestHandler, HTTPServer
@@ -48,6 +51,18 @@ class ForkingHTTPServer(ForkingMixIn, HTTPServer):
                 pass
         super().shutdown_request(request)
 
+def code_changed_at():
+    """When the files the server runs last changed; style.css and script.js go into queue.html"""
+    here = os.path.dirname(os.path.abspath(__file__))
+    newest = 0
+    for f in glob.glob(os.path.join(here, "*.py")) + [os.path.join(here, "style.css"), os.path.join(here, "script.js")]:
+        try:
+            newest = max(newest, os.stat(f).st_mtime)
+        except FileNotFoundError:
+            # removed by an update under way, which leaves its mark on the other files too
+            pass
+    return newest
+
 def about(filename):
     # only file_sender names what it posts; make-request.sh does not
     return filename + ": " if filename else ""
@@ -134,8 +149,16 @@ def main(srv_ssl=False):
 
     log.notice(f'request_handler_server: started on [{host}:{port}], ssl={srv_ssl}...')
     server.timeout = 1.0
+    started = code_changed_at()
     while _shutdown_signum is None:
         server.handle_request()
+        changed = code_changed_at()
+        # an update (make-request.sh --pull) takes effect without a restart by hand; the wait lets
+        # a git pull finish, as a half updated set of modules may not import
+        if changed != started and time.time() - changed > 2:
+            log.notice('request_handler_server: code updated, restarting')
+            server.server_close()
+            os.execv(sys.executable, sys.orig_argv)
     log.notice(f'request_handler_server: {signal.Signals(_shutdown_signum).name} received, shutting down server')
     server.server_close()
 
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/projects/pld-builder.new.git/commitdiff/8ebb35a506911b8dc7fca146e36c1d818eb19014



More information about the pld-cvs-commit mailing list