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
|
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
-
-
+
-
-
|
(define (tasks:hostinfo-get-port vec) (vector-ref vec 2))
(define (tasks:hostinfo-get-pubport vec) (vector-ref vec 3))
(define (tasks:hostinfo-get-transport vec) (vector-ref vec 4))
(define (tasks:hostinfo-get-pid vec) (vector-ref vec 5))
(define (tasks:hostinfo-get-hostname vec) (vector-ref vec 6))
(define (tasks:server-lock-slot mdb run-id)
(let loop ((res #f)
(num-tries 0))
(if (and (< num-tries 5)
(not res))
(begin
(tasks:server-clean-out-old-records-for-run-id mdb run-id)
(if (< (tasks:num-in-available-state mdb run-id) 4)
(tasks:server-set-available mdb run-id))
(thread-sleep! 2) ;; Try removing this. It may not be needed.
(loop (tasks:server-am-i-the-server? mdb run-id)
(tasks:server-clean-out-old-records-for-run-id mdb run-id)
(if (< (tasks:num-in-available-state mdb run-id) 4)
(begin
(tasks:server-set-available mdb run-id)
(thread-sleep! 2) ;; Try removing this. It may not be needed.
(tasks:server-am-i-the-server? mdb run-id))
(+ num-tries 1)))
res)))
#f))
;; register that this server may come online (first to register goes though with the process)
(define (tasks:server-set-available mdb run-id)
(sqlite3:execute
mdb
"INSERT INTO servers (pid,hostname,port,pubport,start_time, priority,state,mt_version,heartbeat, interface,transport,run_id)
VALUES(?, ?, ?, ?, strftime('%s','now'), ?, ?, ?,-1,?, ?, ?);"
|
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
-
+
-
-
+
+
+
+
|
(set! res num-in-queue))
mdb
"SELECT count(id) FROM servers WHERE run_id=?;"
run-id)
res))
(define (tasks:server-clean-out-old-records-for-run-id mdb run-id)
(sqlite3:execute mdb "DELETE FROM servers WHERE state in ('available','shutting-down') AND (strftime('%s','now') - start_time) > 10 AND run_id=?;" run-id)
(sqlite3:execute mdb "DELETE FROM servers WHERE state in ('available','shutting-down') AND (strftime('%s','now') - start_time) > 10 AND run_id=?;" run-id))
(if (server:check-if-running run-id)
(sqlite3:execute mdb "DELETE FROM servers WHERE run_id=?;" run-id)))
(define (tasks:server-force-clean-running-records-for-run-id mdb run-id)
(sqlite3:execute mdb "DELETE FROM servers WHERE state = 'running' AND run_id=?;" run-id))
(define (tasks:server-force-clean-run-record mdb run-id iface port)
(sqlite3:execute mdb "DELETE FROM servers WHERE state = 'running' AND run_id=? AND interface=? AND port=?;"
run-id iface port))
(define (tasks:server-set-state! mdb server-id state)
(sqlite3:execute mdb "UPDATE servers SET state=? WHERE id=?;" state server-id))
(define (tasks:server-delete-record! mdb server-id)
(sqlite3:execute mdb "DELETE FROM servers WHERE id=?;" server-id))
|