Megatest

Diff
Login

Differences From Artifact [9c5f03fda2]:

To Artifact [2abeb8436f]:


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
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







-
-
+
+
+











+
+
-
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+




-
+


+
+

-
-
-
+
+
+
+
+
+







;; Make the dbstruct, call for main db at least once
;; sync disk db to inmem
;;
;; called in http-transport and replicated in rmt.scm for *local* access. 
;;
(define (db:setup run-id)
  (assert *toppath* "FATAL: db:setup called before toppath is available.")
  (let* ((dbstruct (make-dbr:dbstruct)))
    (db:get-dbdat dbstruct *toppath* run-id)
  (let* ((dbstruct (make-dbr:dbstruct))
	 (db-file  (db:run-id->path *toppath* run-id)))
    (db:get-dbdat dbstruct *toppath* db-file)
    (set! *dbstruct-db* dbstruct)
    dbstruct))

;;======================================================================
;; setting/getting a lock on the db for only one server per db
;;
;;  NOTE:
;;       These operate directly on the disk file, NOT on the inmemory db
;;       The lockname is the filename (can have many to one, run-id to fname 
;;======================================================================

;; called before db is open?
;;
(define (db:get-iam-server-lock dbstruct apath run-id)
(define (db:get-iam-server-lock dbh dbfname)
  (let* ((dbh     (db:get-ddb apath dbstruct run-id))
	 (dbfname (db:run-id->path run-id)))
    (sqlite3:with-transaction
     dbh
     (lambda ()
       (let* ((locked (db:get-locker dbh dbfname)))
	 (if (not locked)
	     (db:take-lock dbh dbfname)))))))
  (sqlite3:with-transaction
   dbh
   (lambda ()
     (let* ((locked (db:get-locker dbh dbfname)))
       (if (not locked)
	   (db:take-lock dbh dbfname)
	   #f)))))
	     
;; (exn sqlite3) 
(define (db:get-locker dbh dbfname)
  (condition-case
   (sqlite3:first-row dbh "SELECT owner_id,owner_host,event_time FROM locks WHERE lockname=%;" dbfname)
   (sqlite3:first-row dbh "SELECT owner_pid,owner_host,event_time FROM locks WHERE lockname=%;" dbfname)
   (exn (sqlite3) #f)))

;; should never fail because it is run in a transaction with a test for the lock
;;
(define (db:take-lock dbh dbfname)
  (condition-case
   (sqlite3:first-row dbh "INSERT INTO locks lockname,owner_id,owner_host VALUES (?,?,?);" dbfname (current-process-id) (get-host-name))
   (exn (sqlite3) #f)))
  ;; (condition-case
  ;;  (begin
     (sqlite3:execute dbh "INSERT INTO locks (lockname,owner_pid,owner_host) VALUES (?,?,?);" dbfname (current-process-id) (get-host-name))
   ;;   #t)
     ;; (exn (sqlite3) #f)))
     #t)

(define (db:release-lock dbh dbfname)
  (sqlite3:execute dbh "DELETE FROM locks WHERE lockname=?;" dbfname))

;;======================================================================
;; SQLITE3 HELPERS
;;======================================================================