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
|
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
|
-
+
+
-
+
-
+
-
-
+
+
+
-
+
+
-
+
-
-
+
+
-
+
-
+
-
+
-
-
+
+
-
+
-
+
|
hostname TEXT,
username TEXT,
CONSTRAINT monitors_constraint UNIQUE (pid,hostname));")
(sqlite3:execute mdb "CREATE TABLE IF NOT EXISTS servers (id INTEGER PRIMARY KEY,
pid INTEGER,
interface TEXT,
hostname TEXT,
port INTEGER,
pullport INTEGER,
pubport INTEGER,
start_time TIMESTAMP,
priority INTEGER,
state TEXT,
mt_version TEXT,
heartbeat TIMESTAMP,
CONSTRAINT servers_constraint UNIQUE (pid,hostname,port));")
CONSTRAINT servers_constraint UNIQUE (pid,hostname,pullport,pubport));")
(sqlite3:execute mdb "CREATE TABLE IF NOT EXISTS clients (id INTEGER PRIMARY KEY,
server_id INTEGER,
pid INTEGER,
hostname TEXT,
cmdline TEXT,
login_time TIMESTAMP,
logout_time TIMESTAMP DEFAULT -1,
CONSTRAINT clients_constraint UNIQUE (pid,hostname));")
))
mdb))
;;======================================================================
;; Server and client management
;;======================================================================
;; state: 'live, 'shutting-down, 'dead
(define (tasks:server-register mdb pid interface port priority state)
(define (tasks:server-register mdb pid interface pullport pubport priority state)
(sqlite3:execute
mdb
"INSERT OR REPLACE INTO servers (pid,hostname,port,start_time,priority,state,mt_version,heartbeat,interface) VALUES(?,?,?,strftime('%s','now'),?,?,?,strftime('%s','now'),?);"
pid (get-host-name) port priority (conc state) megatest-version interface)
"INSERT OR REPLACE INTO servers (pid,hostname,pullport,pubport,start_time,priority,state,mt_version,heartbeat,interface)
VALUES(?, ?, ?, ?, strftime('%s','now'), ?, ?, ?, strftime('%s','now'),?);"
pid (get-host-name) pullport pubport priority (conc state) megatest-version interface)
(list
(tasks:server-get-server-id mdb (get-host-name) port pid)
(tasks:server-get-server-id mdb (get-host-name) pullport pid)
interface
pullport
port))
pubport))
;; NB// two servers with same pid on different hosts will be removed from the list if pid: is used!
(define (tasks:server-deregister mdb hostname #!key (port #f)(pid #f))
(debug:print-info 11 "server-deregister " hostname ", port " port ", pid " pid)
(define (tasks:server-deregister mdb hostname #!key (pullport #f)(pid #f))
(debug:print-info 11 "server-deregister " hostname ", pullport " pullport ", pid " pid)
(if pid
;; (sqlite3:execute mdb "DELETE FROM servers WHERE pid=?;" pid)
(sqlite3:execute mdb "UPDATE servers SET state='dead' WHERE pid=?;" pid)
(if port
(if pullport
;; (sqlite3:execute mdb "DELETE FROM servers WHERE hostname=? AND port=?;" hostname port)
(sqlite3:execute mdb "UPDATE servers SET state='dead' WHERE hostname=? AND port=?;" hostname port)
(sqlite3:execute mdb "UPDATE servers SET state='dead' WHERE hostname=? AND pullport=?;" hostname pullport)
(debug:print 0 "ERROR: tasks:server-deregister called with neither pid nor port specified"))))
(define (tasks:server-deregister-self mdb hostname)
(tasks:server-deregister mdb hostname pid: (current-process-id)))
(define (tasks:server-get-server-id mdb hostname port pid)
(define (tasks:server-get-server-id mdb hostname pullport pid)
(let ((res #f))
(sqlite3:for-each-row
(lambda (id)
(set! res id))
mdb
(if (and hostname pid)
"SELECT id FROM servers WHERE hostname=? AND pid=?;"
"SELECT id FROM servers WHERE hostname=? AND port=?;")
hostname (if pid pid port))
"SELECT id FROM servers WHERE hostname=? AND pullport=?;")
hostname (if pid pid pullport))
res))
(define (tasks:server-update-heartbeat mdb server-id)
(sqlite3:execute mdb "UPDATE servers SET heartbeat=strftime('%s','now') WHERE id=?;" server-id))
;; alive servers keep the heartbeat field upto date with seconds every 6 or so seconds
(define (tasks:server-alive? mdb server-id #!key (hostname #f)(port #f)(pid #f))
(define (tasks:server-alive? mdb server-id #!key (hostname #f)(pullport #f)(pid #f))
(let* ((server-id (if server-id
server-id
(tasks:server-get-server-id mdb hostname port pid)))
(tasks:server-get-server-id mdb hostname pullport pid)))
(heartbeat-delta 99e9))
(sqlite3:for-each-row
(lambda (delta)
(set! heartbeat-delta delta))
mdb "SELECT strftime('%s','now')-heartbeat FROM servers WHERE id=?;" server-id)
(< heartbeat-delta 10)))
|
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
|
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
|
-
-
-
+
+
+
-
+
-
-
-
-
-
+
+
+
+
+
+
-
-
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
|
;; ping each server in the db and return first found that responds.
;; remove any others. will not necessarily remove all!
(define (tasks:get-best-server mdb)
(let ((res '())
(best #f))
(sqlite3:for-each-row
(lambda (id hostname interface port pid)
(set! res (cons (list hostname interface port pid) res))
(debug:print-info 2 "Found existing server " hostname ":" port " registered in db"))
(lambda (id hostname interface pullport pubport pid)
(set! res (cons (list hostname interface pullport pubport pid) res))
(debug:print-info 2 "Found existing server " hostname ":" pullport " registered in db"))
mdb
"SELECT id,hostname,interface,port,pid FROM servers WHERE state='live' AND mt_version=? ORDER BY start_time ASC LIMIT 1;" megatest-version)
"SELECT id,hostname,interface,pullport,pubport,pid FROM servers WHERE state='live' AND mt_version=? ORDER BY start_time ASC LIMIT 1;" megatest-version)
;; (print "res=" res)
(if (null? res) #f
(let loop ((hed (car res))
(tal (cdr res)))
;; (print "hed=" hed ", tal=" tal)
(let* ((host (car hed))
(iface (cadr hed))
(port (caddr hed))
(pid (cadddr hed))
(alive (open-run-close tasks:server-alive? tasks:open-db #f hostname: host port: port)))
(let* ((host (list-ref hed 0))
(iface (list-ref hed 1))
(pullport (list-ref hed 2))
(pubport (list-ref hed 3))
(pid (list-ref hed 4))
(alive (open-run-close tasks:server-alive? tasks:open-db #f hostname: host pullport: pullport)))
(if alive
(begin
(debug:print-info 2 "Found an existing, alive, server " host ":" port ".")
(list host iface port))
(debug:print-info 2 "Found an existing, alive, server " host ", " pullport " and " pubport ".")
(list host iface pullport pubport))
(begin
(debug:print-info 1 "Removing " host ":" port " from server registry as it appears to be dead")
(tasks:kill-server #f host port pid)
(debug:print-info 1 "Marking " host ":" pullport " as dead in server registry.")
(if port
(open-run-close tasks:server-deregister tasks:open-db host pullport: pullport)
(open-run-close tasks:server-deregister tasks:open-db host pid: pid))
(if (null? tal)
#f
(loop (car tal)(cdr tal))))))))))
(define (tasks:mark-server hostname pullport pid state)
(if port
(open-run-close tasks:server-deregister tasks:open-db hostname port: port)
(open-run-close tasks:server-deregister tasks:open-db hostname pid: pid)))
;; NOTE: NOT PORTED TO WORK WITH pullport/pubport
(define (tasks:kill-server status hostname port pid)
(debug:print-info 1 "Removing defunct server record for " hostname ":" port)
(if port
(open-run-close tasks:server-deregister tasks:open-db hostname port: port)
(open-run-close tasks:server-deregister tasks:open-db hostname pid: pid))
(if status ;; #t means alive
(begin
|
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
-
-
+
+
-
+
|
(thread-sleep! 5) ;; give it five seconds to die peacefully then do a brutal kill
(process-signal pid signal/kill))
(debug:print 0 "WARNING: Can't kill frozen server on remote host " hostname))))))
(define (tasks:get-all-servers mdb)
(let ((res '()))
(sqlite3:for-each-row
(lambda (id pid hostname interface port start-time priority state mt-version)
(set! res (cons (vector id pid hostname interface port start-time priority state mt-version) res)))
(lambda (id pid hostname interface pullport pubport start-time priority state mt-version)
(set! res (cons (vector id pid hostname interface pullport pubport start-time priority state mt-version) res)))
mdb
"SELECT id,pid,hostname,interface,port,start_time,priority,state,mt_version FROM servers ORDER BY start_time DESC;")
"SELECT id,pid,hostname,interface,pullport,pubport,start_time,priority,state,mt_version FROM servers ORDER BY start_time DESC;")
res))
;;======================================================================
;; Tasks and Task monitors
;;======================================================================
|