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
|
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
|
-
+
-
+
-
+
-
+
|
(declare (unit portlogger))
(declare (uses db))
;; lsof -i
(define (portlogger:open-db fname)
(define (portlogger:open-db fname area-dat)
(let* ((avail (tasks:wait-on-journal fname 5 remove: #t)) ;; wait up to about 10 seconds for the journal to go away
(exists (file-exists? fname))
(db (if avail
(sqlite3:open-database fname)
(begin
(system (conc "rm -f " fname))
(sqlite3:open-database fname))))
(handler (make-busy-timeout 136000))
(canwrite (file-write-access? fname)))
;; (db-init (lambda ()
;; (sqlite3:execute
;; db
;; "CREATE TABLE IF NOT EXISTS ports (
;; port INTEGER PRIMARY KEY,
;; state TEXT DEFAULT 'not-used',
;; fail_count INTEGER DEFAULT 0,
;; update_time TIMESTAMP DEFAULT (strftime('%s','now')) );"))))
(sqlite3:set-busy-handler! db handler)
(db:set-sync db) ;; (sqlite3:execute db "PRAGMA synchronous = 0;")
(db:set-sync db area-dat) ;; (sqlite3:execute db "PRAGMA synchronous = 0;")
;; (if (not exists) ;; needed with IF NOT EXISTS?
(sqlite3:execute
db
"CREATE TABLE IF NOT EXISTS ports (
port INTEGER PRIMARY KEY,
state TEXT DEFAULT 'not-used',
fail_count INTEGER DEFAULT 0,
update_time TIMESTAMP DEFAULT (strftime('%s','now')) );")
db))
(define (portlogger:open-run-close proc . params)
(define (portlogger:open-run-close proc area-dat . params)
(let* ((fname (conc "/tmp/." (current-user-name) "-portlogger.db"))
(avail (tasks:wait-on-journal fname 10))) ;; wait up to about 10 seconds for the journal to go away
(handle-exceptions
exn
(begin
;; (release-dot-lock fname)
(debug:print 0 "ERROR: portlogger:open-run-close failed. " proc " " params)
(debug:print 0 " message: " ((condition-property-accessor 'exn 'message) exn))
(debug:print 0 "exn=" (condition->list exn))
(if (file-exists? fname)(delete-file fname)) ;; brutally get rid of it
(print-call-chain (current-error-port)))
(let* (;; (lock (obtain-dot-lock fname 2 9 10))
(db (portlogger:open-db fname))
(db (portlogger:open-db fname area-dat))
(res (apply proc db params)))
(sqlite3:finalize! db)
;; (release-dot-lock fname)
res))))
;; (fold-row PROC INIT DATABASE SQL . PARAMETERS)
(define (portlogger:take-port db portnum)
|
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
-
-
+
+
|
(sqlite3:fold-row
(lambda (var curr)
(or curr var curr))
#f
db
"SELECT (port) FROM ports WHERE state='released' LIMIT 1;")))
(define (portlogger:find-port db)
(let* ((lowport (let ((val (configf:lookup *configdat* "server" "lowport")))
(define (portlogger:find-port db area-dat)
(let* ((lowport (let ((val (configf:lookup (megatest:area-configdat area-dat) "server" "lowport")))
(if (and val
(string->number val))
(string->number val)
32768)))
(portnum (or (portlogger:get-prev-used-port db)
(+ lowport ;; top of registered ports is 49152 but lets use ports in the registered range
(random (- 64000 lowport))))))
|