forked from webyrd/mediKanren
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rkt
697 lines (639 loc) · 28.6 KB
/
server.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#lang racket/base
(require
"common.rkt"
"trapi.rkt"
"logging2.rkt"
"lw-reasoning.rkt"
"open-api/api-query.rkt"
"ingest-pipeline-status.rkt"
racket/file racket/function racket/list racket/hash
(except-in racket/match ==)
racket/port
racket/pretty
racket/format
racket/date
racket/runtime-path
racket/string
json
web-server/servlet
web-server/servlet-env
web-server/managers/none
web-server/private/gzip
xml
web-server/safety-limits
racket/sandbox
)
(define query-time-limit (make-parameter 600))
(define (alist-ref alist key default)
(define kv (assoc key alist))
(if kv (cdr kv) default))
(print-as-expression #f)
(pretty-print-abbreviate-read-macros #f)
(define argv (current-command-line-arguments))
(define argv-optional '#(CONFIG_FILE))
(when (not (<= (vector-length argv) (vector-length argv-optional)))
(error (format "optional arguments ~s; given ~s" argv-optional argv)))
;; Loading will occur at first use if not explicitly forced like this.
(load-config #t)
;; (load-databases #t)
(define-runtime-path path:root ".")
(define (path/root relative-path) (build-path path:root relative-path))
(define schema.json.txt
(file->string (path/root "open-api/TranslatorReasonersAPI.json")))
(define schema.yaml.txt
(file->string (path/root "open-api/TranslatorReasonersAPI.yaml")))
(define schema.json
(call-with-input-file (path/root "open-api/TranslatorReasonersAPI.json")
read-json))
;; (pretty-print (list 'openapi: (hash-ref schema.json 'openapi)))
;; (pretty-print (list 'info: (hash-ref schema.json 'info)))
;; (pretty-print (list 'externalDocs: (hash-ref schema.json 'externalDocs)))
;; (pretty-print (list 'tags: (hash-ref schema.json 'tags)))
;; (pretty-print (list 'paths: (hash-keys (hash-ref schema.json 'paths))))
;; (pretty-print (list 'components: (hash-keys (hash-ref schema.json 'components))))
(define (xexpr->html-string xe)
(string-append "<!doctype html>" (xexpr->string xe)))
(define mime:text (string->bytes/utf-8 "text/plain;charset=utf-8"))
(define mime:html (string->bytes/utf-8 "text/html; charset=utf-8"))
(define mime:js (string->bytes/utf-8 "text/javascript;charset=utf-8"))
(define mime:json (string->bytes/utf-8 "application/json; charset=utf-8"))
(define index.js "
window.addEventListener('load', function(){
var find_concepts_form = document.getElementById('find-concepts-form');
var find_concepts_text = document.getElementById('find-concepts-text');
var find_concepts_submit = document.getElementById('find-concepts-submit');
var find_concepts_result = document.getElementById('find-concepts-result');
var find_concepts_result_clear = document.getElementById('find-concepts-result-clear');
var find_categories_form = document.getElementById('find-categories-form');
var find_categories_text = document.getElementById('find-categories-text');
var find_categories_submit = document.getElementById('find-categories-submit');
var find_categories_result = document.getElementById('find-categories-result');
var find_categories_result_clear = document.getElementById('find-categories-result-clear');
var find_predicates_form = document.getElementById('find-predicates-form');
var find_predicates_text = document.getElementById('find-predicates-text');
var find_predicates_submit = document.getElementById('find-predicates-submit');
var find_predicates_result = document.getElementById('find-predicates-result');
var find_predicates_result_clear = document.getElementById('find-predicates-result-clear');
var query_result = document.getElementById('query-result');
var query_form = document.getElementById('query-form');
var query_text = document.getElementById('query-text');
var query_submit = document.getElementById('query-submit');
function pretty_json(json_text) {
try { return JSON.stringify(JSON.parse(json_text), null, 2); }
catch (_) { return json_text; }
}
function show(element, result) { element.textContent = pretty_json(result); }
function find_concepts_show(result) { show(find_concepts_result, result); }
function find_categories_show(result) { show(find_categories_result, result); }
function find_predicates_show(result) { show(find_predicates_result, result); }
function find_concepts_clear_result() { find_concepts_result.textContent = ''; }
function find_categories_clear_result() { find_categories_result.textContent = ''; }
function find_predicates_clear_result() { find_predicates_result.textContent = ''; }
function find_concepts(data) {
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function(event){ find_concepts_show(xhr.responseText); });
xhr.addEventListener('error', function(event){ find_concepts_show('POST error'); });
xhr.open('POST', '/find-concepts');
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
xhr.send(data);
}
function find_categories(data) {
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function(event){ find_categories_show(xhr.responseText); });
xhr.addEventListener('error', function(event){ find_categories_show('POST error'); });
xhr.open('POST', '/find-categories');
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
xhr.send(data);
}
function find_predicates(data) {
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function(event){ find_predicates_show(xhr.responseText); });
xhr.addEventListener('error', function(event){ find_predicates_show('POST error'); });
xhr.open('POST', '/find-predicates');
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
xhr.send(data);
}
find_concepts_form.addEventListener('submit', function(event){
event.preventDefault();
find_concepts('\"'+find_concepts_text.value+'\"');
}, false);
find_categories_form.addEventListener('submit', function(event){
event.preventDefault();
find_categories('\"'+find_categories_text.value+'\"');
}, false);
find_predicates_form.addEventListener('submit', function(event){
event.preventDefault();
find_predicates('\"'+find_predicates_text.value+'\"');
}, false);
find_concepts_result_clear.addEventListener('click', function(){
find_concepts_clear_result();
}, false);
find_categories_result_clear.addEventListener('click', function(){
find_categories_clear_result();
}, false);
find_predicates_result_clear.addEventListener('click', function(){
find_predicates_clear_result();
}, false);
function query(data) {
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function(event){ query_show(xhr.responseText); });
xhr.addEventListener('error', function(event){ query_show('POST error'); });
xhr.open('POST', '/pmi/v2/query');
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
xhr.send(data);
}
function query_show(result) { show(query_result, result); }
function query_clear_result() { query_result.textContent = ''; }
query_text.textContent = pretty_json(query_text.textContent);
query_form.addEventListener('submit', function(event){
event.preventDefault();
query_result.textContent = 'Querying...';
query(query_text.value);
}, false);
query_result_clear.addEventListener('click', function(){
query_clear_result();
}, false);
});")
(define (not-found.html uri)
`(html (head (title "mediKanren: 404"))
(body (h1 "What are you looking for?")
(p "There was nothing found at")
(pre ,uri))))
(define index.html
`(html (head (title "mediKanren Reasoner API")
(script ((src "/index.js"))))
(body (h1 "mediKanren Reasoner API")
(p (a ((href "https://github.com/NCATS-Tangerine/NCATS-ReasonerStdAPI"))
"NCATS Biomedical Translator Reasoners Standard API"))
(ul (li (a ((href "/schema.yaml")) "schema.yaml"))
(li (a ((href "/schema.json")) "schema.json")))
(div ((style "display:none"))
(form ((method "post") (id "find-concepts-form"))
(div (input ((type "text")
(id "find-concepts-text")
(value "UMLS:C0935989"))))
(div (button ((type "submit") (id "find-concepts-submit"))
"Find concepts")))
(div (button ((id "find-concepts-result-clear")) "Clear"))
(div (pre ((id "find-concepts-result")) "Concepts will appear here."))
(form ((method "post") (id "find-categories-form"))
(div (input ((type "text")
(id "find-categories-text")
(value "gene"))))
(div (button ((type "submit") (id "find-categories-submit"))
"Find categories")))
(div (button ((id "find-categories-result-clear")) "Clear"))
(div (pre ((id "find-categories-result")) "Categories will appear here."))
(form ((method "post") (id "find-predicates-form"))
(div (input ((type "text")
(id "find-predicates-text")
(value "negatively_regulates"))))
(div (button ((type "submit") (id "find-predicates-submit"))
"Find predicates")))
(div (button ((id "find-predicates-result-clear")) "Clear"))
(div (pre ((id "find-predicates-result")) "Predicates will appear here."))
(p (a ((href "/predicates")) "GET /predicates")))
(form ((method "post") (action "/pmi/v2/query") (id "query-form"))
(div (textarea
((id "query-text")(rows "40") (cols "60"))
#<<EOS
{
"message":{
"query_graph":{
"nodes":{
"n0":{
"ids":["UMLS:C0221347"],
"categories":["biolink:PhenotypicFeature"]
},
"n1":{
"categories":["biolink:NamedThing"]
}
},
"edges":{
"e01":{
"subject":"n0",
"object":"n1"
}
}
}
}
}
EOS
))
(div (button ((type "submit") (id "query-submit"))
"POST /pmi/v2/query")))
(div (pre ((id "query-result")) "Result will appear here.")))))
(define hash-empty (hash))
(define (str v) (if (string? v) v (error (format "invalid string:" v))))
(define (olift v) (if (hash? v) v (error (format "invalid object:" v))))
(define (slift v) (cond ((pair? v) v)
((string? v) (list v))
((null? v) '())
(else (error (format "invalid string or list of strings:" v)))))
(define (alist->attributes alist)
;; TODO: provide standard types for
;; * negated, publications, provided_by
;; * ngd_score, association_type, id
;; * update_date, publications_info
;; * is_defined_by, pmids, n_pmids, SEMMED_PRED
(map (lambda (kv) (hash 'name (car kv)
'type "miscellaneous"
'value (cdr kv)))
alist))
;; (define (concept->result c)
;; (define attrs
;; (alist->attributes
;; (cons (cons "mediKanren-source" (symbol->string (concept->dbname c)))
;; (concept->props c))))
;; (cons (string->symbol (concept->curie c))
;; (hash 'name (concept->name c)
;; 'category (cdr (concept->category c))
;; 'attributes attrs)))
;; (define (edge->result e)
;; (define id (string-append (symbol->string (edge->dbname e)) "."
;; (number->string (edge->eid e))))
;; (define props (make-immutable-hash (edge->props e)))
;; (define relation (hash-ref props "relation" #f))
;; (define attrs
;; (alist->attributes
;; (hash->list
;; (foldl (lambda (k ps) (hash-remove ps k)) props
;; '("relation" "subject" "object"
;; "simplified_relation" "simplified_edge_label")))))
;; (define obj
;; (hash 'predicate (cdr (edge->pred e))
;; 'subject (concept->curie (edge->subject e))
;; 'object (concept->curie (edge->object e))
;; 'attributes attrs))
;; (cons (string->symbol id) (if relation
;; (hash-set obj 'relation relation)
;; obj)))
(define (message->response msg)
(define broad-response (if (or
(hash-ref msg 'disable_external_requests #f)
(not (cfg:config-ref 'trapi-enable-external-requests?)))
(hash 'response hash-empty 'status #"disabled" 'headers '())
(time (api-query (string-append url.broad path.query)
(hash 'message msg)))))
(define broad-results (hash-ref broad-response 'response))
(define broad-results-count (length (hash-ref (hash-ref broad-results 'message hash-empty) 'results '())))
(define broad-error-message (hash-ref broad-results 'detail #f)) ; not sure if this is stable - it isn't TRAPI
(lognew-info
(hasheq
'event "broad-response"
'status (bytes->string/utf-8 (hash-ref broad-response 'status))
'headers (map bytes->string/utf-8 (hash-ref broad-response 'headers))
'result-size broad-results-count))
;; (log-info log-key (format "Broad results: ~s" broad-results))
;; (with-handlers ((exn:fail? (lambda (exn)
;; (hash 'error (exn-message exn)))))
(lognew-info
(hasheq 'event "query_received"
'msg msg))
(with-handlers ((exn:fail:resource?
(lambda (exn)
(lognew-error (format "Error: ~a" exn))
(error "Max query time exceded"))))
(call-with-limits (query-time-limit) #f
(lambda ()
(let-values (((result cpu real gc) (time-apply (lambda () (trapi-response msg)) '())))
(let* ((local-results (car result))
(length-local (length (hash-ref local-results 'results '()))))
(lognew-info
(hasheq
'event "query_finished"
'cpu-time cpu
'real-time real
'num-results length-local))
(values (hash-set*
(merge-results
(list (hash-ref (olift broad-results) 'message hash-empty)
local-results)))
(list (hash 'level "INFO"
'message (format "Query time: ~ams" cpu))
(if broad-error-message
(hash 'level "WARNING"
'message (format "MolePro error: ~s" broad-error-message))
(hash 'level "INFO"
'message (format "MolePro results: ~a" broad-results-count)))
(hash 'level "INFO"
'message (format "MolePro response status: ~s" (hash-ref broad-response 'status)))
))))))))
(define (merge-results rs)
(let loop ((rs rs) (results '()) (nodes '()) (edges '()))
(cond ((null? rs) (hash 'results results
'knowledge_graph (hash 'nodes (make-immutable-hash nodes)
'edges (make-immutable-hash edges))))
(else (define r (car rs))
(define kg (hash-ref r 'knowledge_graph hash-empty))
(loop (cdr rs)
(append (hash-ref r 'results '()) results)
(append (hash->list (hash-ref kg 'nodes hash-empty))
nodes)
(append (hash->list (hash-ref kg 'edges hash-empty))
edges))))))
;; (define (predicates)
;; ;; TODO: at greater expense, we could restrict each list of predicates
;; ;; to those reflected by existing edges for the corresponding categories.
;; (define cs (run* (c) (categoryo c)))
;; (define ps (run* (p) (predicateo p)))
;; (define dbs (map car cs))
;; (make-immutable-hash
;; (append*
;; (map (lambda (db)
;; (define dcs
;; (filter-not
;; not (map (lambda (c) (and (eq? (car c) db) (cddr c)))
;; cs)))
;; (define dps
;; (filter-not
;; not (map (lambda (p) (and (eq? (car p) db) (cddr p)))
;; ps)))
;; (define dcps
;; (make-immutable-hash
;; (map (lambda (c) (cons (string->symbol c) dps)) dcs)))
;; (map (lambda (c) (cons (string->symbol c) dcps)) dcs))
;; dbs))))
;; (define predicates-cached (string->bytes/utf-8 (jsexpr->string (predicates))))
;; (define predicates-cached-gzip (gzip/bytes predicates-cached))
(define (query-impl jsdata)
(cond ((or (eof-object? jsdata) (not (hash? jsdata))) 'null)
(else (let* ((data (olift jsdata))
(request-msg (olift (hash-ref data 'message hash-empty))))
(let-values (((message logs) (message->response request-msg)))
(let ((length-local (length (hash-ref message 'results))))
(hash 'message message
'query_graph (hash-ref request-msg 'query_graph '#hash())
'status "Success"
'description (format "Success. ~s result~a." length-local (if (= length-local 1) "" "s"))
'logs logs)))))))
(define ((query requestid0) jsdata)
(parameterize ((requestid requestid0))
(query-impl jsdata)))
;; Load balancer health checks can be very numerous and drown out useful logs
(define (squelch-noisy-request-log? req)
(define path (url->string (request-uri req)))
(cond
((equal? path "/health") #t)
(else #f)))
; Loosely adapted from:
; https://stackoverflow.com/questions/53911162/how-to-show-http-status-code-in-web-server-logs
(define (headers->hasheq hs)
(for/hasheq ([h (in-list hs)])
(values (string->symbol (~a (header-field h)))
(~a (header-value h)))))
(define (dict-request-fields req)
(hasheq 'method (~a (request-method req))
'ip (request-client-ip req)
'path (url->string (request-uri req))
'headers (headers->hasheq (request-headers/raw req))))
(define (logwrap-lazy-impl handler req t0 requestid0)
(define resp
(parameterize ((requestid requestid0))
(handler req)))
(unless (squelch-noisy-request-log? req)
(lognew-info
(hasheq 'request (dict-request-fields req))))
(struct-copy response resp (output
(lambda (fd)
(define tmp
(parameterize ((requestid requestid0))
((response-output resp) fd)))
(define t1 (current-milliseconds))
(define dur (- t1 t0))
;; Let's use "structured logging" here to make it easier to search,
;; and do things like create CloudWatch metrics from CloudWatch Logs
;; filters (they have a syntax to extract things from JSON.)
(unless (squelch-noisy-request-log? req)
(lognew-info
(hasheq 'request (dict-request-fields req)
'response (hasheq 'code (response-code resp)
'headers (headers->hasheq (response-headers resp))
'duration dur))))
tmp))))
(define ((logwrap-lazy handler) req)
(define t0 (current-milliseconds))
(define requestid0 (+ (* t0 1000) (random 1000)))
(logwrap-lazy-impl handler req t0 requestid0))
(define (accepts-gzip? req)
(member "gzip" (map string-trim
(string-split (alist-ref (request-headers req)
'accept-encoding "") ","))))
(define (respond code message headers mime-type body)
(response/full code (string->bytes/utf-8 message)
(current-seconds) mime-type headers
(list body)))
(define (OK req extra-headers mime-type body (body-gzipped? #f))
(define gzip? (accepts-gzip? req))
(define headers (if gzip? (cons (make-header #"Content-Encoding" #"gzip")
extra-headers)
extra-headers))
(define bytes.body (if (string? body) (string->bytes/utf-8 body) body))
(define payload (if (and gzip? (not body-gzipped?)) (gzip/bytes bytes.body)
bytes.body))
(respond 200 "OK" headers mime-type payload))
(define (not-found req)
(respond 404 "Not Found" '() mime:html
(string->bytes/utf-8
(xexpr->html-string
(not-found.html (url->string (request-uri req)))))))
(define (OK/jsexpr f req)
(define input (bytes->jsexpr (request-post-data/raw req)))
(define result (job (thunk (f input))))
(if (job-failure? result)
(respond 400 "Bad Request" '() mime:text
(string->bytes/utf-8 (job-failure-message result)))
(OK req '() mime:json (jsexpr->string result))))
(define (/index req)
(OK req '() mime:html (xexpr->html-string index.html)))
(define (/schema.json req) (OK req '() mime:text schema.json.txt))
(define (/schema.yaml req) (OK req '() mime:text schema.yaml.txt))
;; (define (/predicates req) (if (accepts-gzip? req)
;; (OK req '() mime:json predicates-cached-gzip #t)
;; (OK req '() mime:json predicates-cached #f)))
;; (define (/query req) (OK/jsexpr query req))
(define (group-by-db xs)
(foldl (lambda (x db=>id)
(hash-update db=>id (car x) (lambda (xs) (cons (cdr x) xs)) '()))
(hash) xs))
(define (pretty-synonyms ss)
(if (and (pair? ss) (not (cdar ss))) '()
(make-immutable-hash
(map (lambda (kv) (cons (string->symbol (car kv)) (cdr kv))) ss))))
;; (define (find-concepts/any str)
;; (hash 'synonyms (pretty-synonyms (curie-synonyms/names str))
;; 'concepts
;; (make-immutable-hash
;; (hash-map
;; (group-by-db
;; (map (lambda (c)
;; (define r (concept->result c))
;; (define attrs (hash-ref (cdr r) 'attributes))
;; (define dbnames
;; (map (lambda (attr) (hash-ref attr 'value))
;; (filter (lambda (attr) (equal? (hash-ref attr 'name)
;; "mediKanren-source"))
;; attrs)))
;; (define dbname (if (null? dbnames) 'unknown
;; (string->symbol (car dbnames))))
;; (cons dbname r))
;; (append (find-concepts #t (list str))
;; (find-concepts #f (list str)))))
;; (lambda (db cs) (cons db (make-immutable-hash cs)))))))
;; (define ((find/db-id find) data)
;; (group-by-db (map (lambda (x) (cons (car x) (cddr x))) (find (list data)))))
(define (/health req)
(OK req '() mime:json (jsexpr->string '#hasheq()))
#;(let-values (((result cpu real gc) (time-apply (lambda () (run 1 () (triple "NCIT:C18585" "biolink:actively_involved_in" "NCIT:C45399"))) '())))
(let ((response
(if (null? result)
(hash 'status "corrupt"
'reason "no data"
'real_time real
'cpu_time cpu)
(hash 'status "online"
'real_time real
'cpu_time cpu))))
(OK req '() mime:json (jsexpr->string response)))))
(define (/index.js req) (OK req '() mime:js index.js))
;; (define (/health req) (OK health req))
(define (/query req)
(OK/jsexpr (query (requestid)) req))
;; (define (/v2/find-concepts req) (OK/jsexpr find-concepts/any req))
;; (define (/v2/find-categories req) (OK/jsexpr (find/db-id find-categories) req))
;; (define (/v2/find-predicates req) (OK/jsexpr (find/db-id find-predicates) req))
(struct job-failure (message))
(define (work-safely work)
(define custodian.work (make-custodian))
(define result
;; current-custodian will collect all file handles opened during work
(parameterize ((current-custodian custodian.work))
(with-handlers ((exn:fail?
(lambda (v)
((error-display-handler) (exn-message v) v)
(job-failure (exn-message v))))
((lambda _ #t)
(lambda (v)
(define message
(string-append "unknown error: "
(with-output-to-string (thunk (write v)))))
(pretty-write message)
(job-failure message))))
(work))))
(custodian-shutdown-all custodian.work) ; close all file handles opened during work
result)
;; Run multiple jobs concurrently
;(define (job work) (work-safely work))
;; Run multiple jobs sequentially
(define (job work)
(define job-response (make-channel))
(channel-put job-request (cons job-response work))
(channel-get job-response))
(define job-request (make-channel))
(define worker
(thread
(thunk (let loop ()
(match-define (cons job-response work) (channel-get job-request))
(channel-put job-response (work-safely work))
(loop)))))
(define (start)
(define-values (dispatch _)
(dispatch-rules
(("") #:method "get" /index)
(("index.js") #:method "get" /index.js)
(("schema.json") #:method "get" /schema.json)
(("schema.yaml") #:method "get" /schema.yaml)
;; (("predicates") #:method "get" /predicates)
;; Legacy endpoint - unversioned - TRAPI 1.0 - TODO: Delete
(("query") #:method "post" /query)
;; Current (versioned) endpoint for NCATS - TRAPI 1.1
(("v2" "query") #:method "post" /query)
;; Endpoint for Andy's webapp
(("pmi" "v2" "query") #:method "post" /query)
;; TODO: restore text search
;; (("pmi" "v2" "query" "find-concepts") #:method "post" /v2/find-concepts)
;; (("pmi" "v2" "query" "find-categories") #:method "post" /v2/find-categories)
;; (("pmi" "v2" "query" "find-predicates") #:method "post" /v2/find-predicates)
(("pmi" "v2" "ingest-pipeline" "status") #:method "get" /ingest-pipeline-status)
(("health") #:method "get" /health)
(else not-found)))
(serve/servlet (logwrap-lazy dispatch)
;; none-manager for better performance:
;; only possible because we're not using web continuations.
#:manager (create-none-manager #f)
#:servlet-regexp #rx""
#:listen-ip #f ;; comment this to disable external connection
#:port 8384
#:launch-browser? #f
#:safety-limits (make-safety-limits #:response-send-timeout (* 2 (query-time-limit))
#:response-timeout (* 2 (query-time-limit)))
;; Avoid (query-time-limit) getting enforced
;; in multiple locations.
))
(module+ main (start))
;; Notes for exercising multi-threaded file handle usage to stress-test work-safely:
;; Start a long query, then repeatedly run short queries while the long query runs.
;; Query results should be deterministic and produce no errors.
;; A long query for semmed:
#|
{
"message": {
"query_graph": {
"nodes": {
"n0": {
"ids": [
"UMLS:C0520909"
]
},
"n1": {
"categories": [
"disease_or_phenotypic_feature"
]
},
"n2": {
"categories": [
"gene"
]
}
},
"edges": {
"e01": {
"subject": "n0",
"object": "n1"
},
"e21": {
"subject": "n2",
"object": "n1"
}
}
}
}
}
|#
;; A short query for semmed:
#|
{
"message": {
"query_graph": {
"nodes": {
"n0": {
"ids": [
"UMLS:C0520909"
]
},
"n1": {
"categories": [
"disease_or_phenotypic_feature"
]
}
},
"edges": {
"e01": {
"subject": "n0",
"object": "n1"
}
}
}
}
}
|#