︙ | | | ︙ | |
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
(mutex-lock! *http-mutex*)
(set! *http-requests-in-progress* (+ 1 *http-requests-in-progress*)))
;; Send "cmd" with json payload "params" to serverdat and receive result
;;
(define (http-transport:client-api-send-receive run-id serverdat cmd params #!key (numretries 30))
(let* ((fullurl (if (list? serverdat)
(cadddr serverdat) ;; this is the uri for /api
(begin
(debug:print 0 "FATAL ERROR: http-transport:client-api-send-receive called with no server info")
(exit 1))))
(res #f))
(handle-exceptions
exn
(if (> numretries 0)
|
|
|
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
(mutex-lock! *http-mutex*)
(set! *http-requests-in-progress* (+ 1 *http-requests-in-progress*)))
;; Send "cmd" with json payload "params" to serverdat and receive result
;;
(define (http-transport:client-api-send-receive run-id serverdat cmd params #!key (numretries 30))
(let* ((fullurl (if (list? serverdat)
(list-ref serverdat 4) ;; (cadddr serverdat) ;; this is the uri for /api
(begin
(debug:print 0 "FATAL ERROR: http-transport:client-api-send-receive called with no server info")
(exit 1))))
(res #f))
(handle-exceptions
exn
(if (> numretries 0)
|
︙ | | | ︙ | |
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
(debug:print-info 11 "got res=" res)
res)))))
;;
;; connect
;;
(define (http-transport:client-connect iface port)
(let* ((uri-dat (make-request method: 'POST uri: (uri-reference (conc "http://" iface ":" port "/ctrl"))))
;; (uri-dat (make-request method: 'GET uri: (uri-reference (conc "http://" iface ":" port "/ctrl"))))
(uri-api-dat (make-request method: 'POST uri: (uri-reference (conc "http://" iface ":" port "/api"))))
;; (uri-api-dat (make-request method: 'GET uri: (uri-reference (conc "http://" iface ":" port "/api"))))
(server-dat (list iface port uri-dat uri-api-dat)))
;; (login-res (server:ping-server run-id server-dat))) ;; login-no-auto-client-setup server-dat run-id)))
server-dat))
;; (if (and (list? login-res)
;; (car login-res))
;; (begin
;; (hash-table-set! *runremote* run-id server-dat)
;; (debug:print-info 2 "Logged in and connected to " iface ":" port)
|
>
|
|
|
|
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
(debug:print-info 11 "got res=" res)
res)))))
;;
;; connect
;;
(define (http-transport:client-connect iface port)
(let* ((api-url (conc "http://" iface ":" port "/api"))
(uri-dat (make-request method: 'POST uri: (uri-reference (conc "http://" iface ":" port "/ctrl"))))
;; (uri-dat (make-request method: 'GET uri: (uri-reference (conc "http://" iface ":" port "/ctrl"))))
(uri-api-dat (make-request method: 'POST uri: api-url)) ;; (uri-reference (conc "http://" iface ":" port "/api"))))
;; (uri-api-dat (make-request method: 'GET uri: (uri-reference (conc "http://" iface ":" port "/api"))))
(server-dat (list iface port uri-dat uri-api-dat api-url)))
;; (login-res (server:ping-server run-id server-dat))) ;; login-no-auto-client-setup server-dat run-id)))
server-dat))
;; (if (and (list? login-res)
;; (car login-res))
;; (begin
;; (hash-table-set! *runremote* run-id server-dat)
;; (debug:print-info 2 "Logged in and connected to " iface ":" port)
|
︙ | | | ︙ | |
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
|
))))
(let loop ((count 0)
(server-state 'available))
;; Use this opportunity to sync the inmemdb to db
(let ((start-time (current-milliseconds))
(sync-time #f)
(rem-time #f))
(if *inmemdb* (db:sync-touched *inmemdb* force-sync: #t))
(set! sync-time (- (current-milliseconds) start-time))
(set! rem-time (quotient (- 4000 sync-time) 1000))
(debug:print 0 "SYNC: time= " sync-time ", rem-time=" rem-time)
(if (and (<= rem-time 4)
(> rem-time 0))
(thread-sleep! rem-time)
(thread-sleep! 4))) ;; fallback for if the math is changed ...
;;
;; set_running after our first pass through
;;
(if (eq? server-state 'available)
(tasks:server-set-state! tdb server-id "running"))
(if (< count 1) ;; 3x3 = 9 secs aprox
(loop (+ count 1) 'running))
;; Check that iface and port have not changed (can happen if server port collides)
(mutex-lock! *heartbeat-mutex*)
(set! sdat *server-info*)
(mutex-unlock! *heartbeat-mutex*)
|
>
<
<
<
<
>
>
>
>
>
|
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
|
))))
(let loop ((count 0)
(server-state 'available))
;; Use this opportunity to sync the inmemdb to db
(let ((start-time (current-milliseconds))
(sync-time #f)
(rem-time #f))
(if *inmemdb* (db:sync-touched *inmemdb* force-sync: #t))
(set! sync-time (- (current-milliseconds) start-time))
(set! rem-time (quotient (- 4000 sync-time) 1000))
(debug:print 0 "SYNC: time= " sync-time ", rem-time=" rem-time)
;;
;; set_running after our first pass through
;;
(if (eq? server-state 'available)
(tasks:server-set-state! tdb server-id "running"))
(if (and (<= rem-time 4)
(> rem-time 0))
(thread-sleep! rem-time)
(thread-sleep! 4))) ;; fallback for if the math is changed ...
(if (< count 1) ;; 3x3 = 9 secs aprox
(loop (+ count 1) 'running))
;; Check that iface and port have not changed (can happen if server port collides)
(mutex-lock! *heartbeat-mutex*)
(set! sdat *server-info*)
(mutex-unlock! *heartbeat-mutex*)
|
︙ | | | ︙ | |
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
;; no_traffic
;;
(if (and *server-run*
(> (+ last-access server-timeout)
(current-seconds)))
(begin
(debug:print-info 0 "Server continuing, seconds since last db access: " (- (current-seconds) last-access))
(loop 0 server-state))
(begin
(debug:print-info 0 "Starting to shutdown the server.")
;; need to delete only *my* server entry (future use)
(set! *time-to-exit* #t)
(if *inmemdb* (db:sync-touched *inmemdb* force-sync: #t))
;;
|
>
>
>
>
>
>
>
|
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
;; no_traffic
;;
(if (and *server-run*
(> (+ last-access server-timeout)
(current-seconds)))
(begin
(debug:print-info 0 "Server continuing, seconds since last db access: " (- (current-seconds) last-access))
;;
;; Consider implementing some smarts here to re-insert the record or kill self is
;; the db indicates so
;;
;; (if (tasks:server-am-i-the-server? tdb run-id)
;; (tasks:server-set-state! tdb server-id "running"))
;;
(loop 0 server-state))
(begin
(debug:print-info 0 "Starting to shutdown the server.")
;; need to delete only *my* server entry (future use)
(set! *time-to-exit* #t)
(if *inmemdb* (db:sync-touched *inmemdb* force-sync: #t))
;;
|
︙ | | | ︙ | |