(thread-sleep! 0.05) ;; any real need for delay here?
(let loop ()
(let* ((servers (tt:get-server-info-sorted ttdat dbfname))
(ok (cond
((null? servers) #f) ;; not ok
((equal? (list-ref (car servers) 6) ;; compare the servinfofile
(tt-servinf-file ttdat))
(debug:print-info 0 *default-log-port* "Keep running, I'm the top server.")
(debug:print-info 0 *default-log-port* "Keep running, I'm the top server on "(tt-host ttdat)":"(tt-port ttdat))
(if db-locked-in
#t
(let* ((lockinfo (dbfile:with-no-sync-db nosyncdbpath
(lambda (db)
(db:no-sync-get-lock db dbfname))))
(success (car lockinfo)))
(if success
(begin
(debug:print 0 *default-log-port* "Got server lock for "dbfname)
(set! db-locked-in #t)
#t)
(begin
(debug:print 0 *default-log-port* "Failed to get server lock for "dbfname)
#f)))))
(else
(debug:print-info 0 *default-log-port* "I'm not the lead server: "servers)
(let* ((leadsrv (car servers)))
(match leadsrv
((host port startseconds server-id pid dbfname servinfofile)
(if (tt:ping host port server-id)
#f ;; not the server, but all good, want to exit
(if (and (file-exists? servinfofile)
(> (- (current-seconds)(file-modification-time servinfofile)) 5))
(> (- (current-seconds)(file-modification-time servinfofile)) 15))
(begin
;; can't ping and file has been on disk 5 seconds, go ahead and try to remove it
(debug:print-info 0 *default-log-port* "Removing apparently dead server info file: "servinfofile)
(delete-file* servinfofile)
#t) ;; not the server but the server is not reachable
(begin
(debug:print 0 *default-log-port* "I'm not the server but will try again since "servinfofile" is fresh")
#t)))
#t))))
(else ;; should never get here
(debug:print 0 *default-log-port* "BAD SERVER RECORD: "leadsrv)
(assert #f "Bad server record "leadsrv))))))))
(if ok
;; (if (> *api-process-request-count* 0) ;; have requests in flight
;; (tt-last-access-set! ttdat (current-seconds)))
(tt-last-access-set! ttdat *db-last-access*) ;; bit silly, just use db-last-access