[projects/pld-builder.new] loop: end on a code update

arekm arekm at pld-linux.org
Sat Nov 28 22:31:17 CET 2026


commit 003d220471af63efabae4ebd47f5c97ab90f3f12
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date:   Sat Nov 28 21:49:44 2026 +0100

    loop: end on a code update
    
    After make-request --pull the process cron started went on with the old
    code for up to a minute: a request then was handled by it.

 PLD_Builder/loop.py                   | 27 ++++++++++++++++++++++++++-
 PLD_Builder/request_handler_server.py | 18 ++----------------
 PLD_Builder/srpm_builder.py           |  3 +++
 3 files changed, 31 insertions(+), 17 deletions(-)
---
diff --git a/PLD_Builder/loop.py b/PLD_Builder/loop.py
index c29410a..504db31 100644
--- a/PLD_Builder/loop.py
+++ b/PLD_Builder/loop.py
@@ -1,5 +1,6 @@
 # vi: ts=8 sts=4 sw=4 et
 
+import glob
 import os
 import sys
 import time
@@ -7,6 +8,28 @@ import time
 import wrap
 
 
+def code_changed_at():
+    """When the code of PLD_Builder 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
+
+
+# as the process imported it, at its start
+loaded = code_changed_at()
+
+
+def code_updated():
+    """Whether the code changed since this process imported it: only a new process runs the new code"""
+    return code_changed_at() != loaded
+
+
 def run_loop(fnc, secs = 5, max = 60):
     def run():
         pid = os.fork()
@@ -23,7 +46,9 @@ def run_loop(fnc, secs = 5, max = 60):
                 sys.exit(10)
 
     start = time.time()
-    while time.time() - start < max:
+    # each child has the modules this process imported: an update (make-request --pull) takes
+    # effect with the next start by cron, so none is run on the code from before it
+    while time.time() - start < max and not code_updated():
         last = time.time()
         run()
         took = time.time() - last
diff --git a/PLD_Builder/request_handler_server.py b/PLD_Builder/request_handler_server.py
index 2420aaa..09d5bab 100644
--- a/PLD_Builder/request_handler_server.py
+++ b/PLD_Builder/request_handler_server.py
@@ -1,5 +1,4 @@
 import argparse
-import glob
 import os
 import re
 import signal
@@ -13,6 +12,7 @@ from http.server import BaseHTTPRequestHandler, HTTPServer
 from socketserver import ForkingMixIn
 
 import log
+import loop
 import request_handler
 from acl import acl
 from blacklist import blacklist
@@ -51,18 +51,6 @@ 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 ""
@@ -149,13 +137,11 @@ 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:
+        if loop.code_updated() and time.time() - loop.code_changed_at() > 2:
             log.notice('request_handler_server: code updated, restarting')
             server.server_close()
             os.execv(sys.executable, sys.orig_argv)
diff --git a/PLD_Builder/srpm_builder.py b/PLD_Builder/srpm_builder.py
index 1882efc..09eaa86 100644
--- a/PLD_Builder/srpm_builder.py
+++ b/PLD_Builder/srpm_builder.py
@@ -191,6 +191,9 @@ def main():
         status.push(f"srpm: handling request from {r.requester}")
         handle_request(r)
         status.pop()
+        # the request was an update (make-request --pull): the rest of the queue waits for the new code
+        if loop.code_updated():
+            return
 
 if __name__ == '__main__':
     argparse.ArgumentParser(description='Build src.rpms for the queued requests.').parse_args()
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/projects/pld-builder.new.git/commitdiff/003d220471af63efabae4ebd47f5c97ab90f3f12



More information about the pld-cvs-commit mailing list