# HG changeset patch
# User jfp <jf.pieronne@laposte.net>
# Date 1683997793 -7200
#      Sat May 13 19:09:53 2023 +0200
# Node ID 57dc6804934f061dc3a3b91e33f0d5ade2433d8e
# Parent  ec5280befeef044a94c4eed41e8e50864e1a03ab
Start 1s timer only if necessary

diff --git a/supervisord.py b/supervisord.py
--- a/supervisord.py
+++ b/supervisord.py
@@ -44,6 +44,15 @@
 TRUTHY_STRINGS = ('yes', 'true', 'on', '1')
 FALSY_STRINGS = ('no', 'false', 'off', '0')
 
+timer_delay = starlet.bintim('0 0:00:01.00')[1]
+timer_astctx = vmsast.AstContext(vmsast.M_WAKE | vmsast.M_QUEUE)
+
+
+def start_timer():
+    global timer_delay, timer_astctx
+    timer_astctx.reset()
+    starlet.setimr(daytim=timer_delay, reqidt=timer_astctx)
+
 
 class RestartWhenExitUnexpected:
     pass
@@ -637,6 +646,7 @@
 
 
 def run(chan: int, chancmd: int, chancmd_r: int):
+    global timer_delay, timer_astctx
     astparam: AstParam
     with (
         mbxqio.MBXQIO(channel=chan) as fterm,
@@ -650,11 +660,10 @@
             if pgm.autostart:
                 pgm.create_process(False)
 
-        timer_astctx = vmsast.AstContext(vmsast.M_WAKE | vmsast.M_QUEUE)
         timer_astctx.param = AstParam(AstParamType.TIMER)
-        timer_delay = starlet.bintim('0 0:00:01.00')[1]
         starlet.setimr(daytim=timer_delay, reqidt=timer_astctx)
 
+        timer_in_progress = True
         while True:
             ret_ast_context: vmsast.AstContext
 
@@ -675,8 +684,11 @@
             elif astparam.ptype == AstParamType.CMD:
                 c_astctxt = qio_cmd(fcmd)  # noqa: F841
             elif astparam.ptype == AstParamType.TIMER:
-                timer_astctx.reset()
-                starlet.setimr(daytim=timer_delay, reqidt=timer_astctx)
+                timer_in_progress = False
+
+            if timer_queue.not_empty and not timer_in_progress:
+                timer_in_progress = True
+                start_timer()
 
 
 def main():