320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
(let ((sig (server:mk-signature)))
(set! *my-client-signature* sig)
*my-client-signature*)))
;; wait for server=start-last to be three seconds old
;;
(define (server:wait-for-server-start-last-flag 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))))
;; (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)))
(thread-sleep! 0.25)
(let ((res (with-input-from-file start-flag
(lambda ()
(read-line)))))
(equal? server-key res))))
#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)
(server:wait-for-server-start-last-flag areapath)))))))
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
<
<
|
<
<
|
<
|
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
(let ((sig (server:mk-signature)))
(set! *my-client-signature* sig)
*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)))
(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
(create-key-file)
(thread-sleep! 0.25)
(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)
(server:wait-for-server-start-last-flag areapath)))))))
|