Index: server.scm ================================================================== --- server.scm +++ server.scm @@ -322,29 +322,75 @@ *my-client-signature*))) ;; wait for server=start-last to be three seconds old ;; (define (server:wait-for-server-start-last-flag areapath) + (let* ((flag-dir (conc areapath "/logs")) + (start-flag (conc flag-dir "/server-start-last")) + ;;; THIS INTERACTS WITH [server] timeout. Suggest using 0.1 or above for timeout (6 seconds) + (reftime (configf:lookup-number *configdat* "server" "idletime" default: 4)) + (server-key (conc (get-host-name) "-" (current-process-id))) + (create-key-file (lambda () + (with-output-to-file start-flag + (lambda () + (print server-key))))) + (check-key-file (lambda () + (let ((res (with-input-from-file start-flag + (lambda () + (read-line))))) + (equal? server-key res)))) + (get-file-age (lambda () + (let* ((fmodtime (file-modification-time start-flag))) + (- (current-seconds) fmodtime))))) + (if (not (directory-exists? flag-dir)) + (begin + (debug:print-info 0 *default-log-port* "Directory " flag-dir " does not exist! Cannot gate.") + #f) + (if (file-exists? start-flag) + (if (check-key-file) ;; is it me? + #t ;; yes, it is me, proceed + (let* ((file-age (get-file-age))) + (if (> file-age reftime) ;; let the previous guy have at least 4 seconds to do their thing + (begin ;; file is old enough, we can try to take it + (create-key-file) ;; take the file and try again + (server:wait-for-server-start-last-flag areapath)) + (let* ((remtime (max 1 (min file-age reftime)))) + (debug:print-info 0 *default-log-port* "Gating server start, waiting remtime="remtime) + (thread-sleep! remtime) + (server:wait-for-server-start-last-flag areapath))))) + (begin + (create-key-file) + (server:wait-for-server-start-last-flag areapath)))))) + + + +;; wait for server=start-last to be three seconds old +;; +(define (server:wait-for-server-start-last-flag-old areapath) (let* ((start-flag (conc areapath "/logs/server-start-last")) ;;; THIS INTERACTS WITH [server] timeout. Suggest using 0.1 or above for timeout (6 seconds) (reftime (configf:lookup-number *configdat* "server" "idletime" default: 4)) - (server-key (conc (get-host-name) "-" (current-process-id)))) + (server-key (conc (get-host-name) "-" (current-process-id))) + (create-key-file (lambda () + (with-output-to-file start-flag + (lambda () + (print server-key))))) + (check-key-file (lambda () + (let ((res (with-input-from-file start-flag + (lambda () + (read-line))))) + (equal? server-key res))))) ;; (thread-sleep! (/ (random 500) 1000)) ;; I don't think this made a difference (if (file-exists? start-flag) (let* ((fmodtime (file-modification-time start-flag)) (delta (- (current-seconds) fmodtime)) (all-go (> delta reftime))) (if (and all-go (begin - (with-output-to-file start-flag - (lambda () - (print server-key))) + (create-key-file) (thread-sleep! 0.25) - (let ((res (with-input-from-file start-flag - (lambda () - (read-line))))) - (equal? server-key res)))) + (check-key-file))) #t ;; (system (conc "touch " start-flag)) ;; lazy but safe (begin (debug:print-info 0 *default-log-port* "Gating server start, last start: " fmodtime ", delta: " delta ", reftime: " reftime ", all-go=" all-go) (thread-sleep! reftime)