1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
|
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
;; (db:delay-if-busy dbdat)
(sqlite3:execute db "VACUUM;")
dead-runs))
;;======================================================================
;; no-sync.db - small bits of data to be shared between servers
;;======================================================================
;; if we are not a server create a db handle. this is not finalized
;; so watch for problems. I'm still not clear if it is needed to manually
;; finalize sqlite3 dbs with the sqlite3 egg.
;;
(define (db:no-sync-db db-in)
(mutex-lock! *db-access-mutex*)
(let ((res (if db-in
db-in
(let ((db (db:open-no-sync-db)))
(set! *no-sync-db* db)
db))))
(mutex-unlock! *db-access-mutex*)
res))
(define (db:get-dbsync-path)
(case (rmt:transport-mode)
((http)(common:make-tmpdir-name *toppath* ""))
((tcp) (conc *toppath*"/.mtdb"))
((nfs) (conc *toppath*"/.mtdb"))
(else "/tmp/dunno-this-gonna-exist")))
(define (db:no-sync-add-job db-in host-type vars-list exekey cmdline)
(sqlite3:execute (db:no-sync-db db-in) "INSERT INTO jobs_queue (host_type,vars,exekey,cmdline,state,event_time,last_update) VALUES (?,?,?,?,?,?,?);"
host-type
(with-output-to-string
(lambda ()
(write vars-list)))
exekey cmdline "waiting" (current-seconds)(current-seconds)))
;; find next job (waiting longest) that matches host-type - future, we'll find jobs that fit if no exact match
(define (db:no-sync-take-job db-in host-type)
(let* ((db (db:no-sync-db db-in))
(stmt1 "SELECT id,host_type,vars,exekey,cmdline,state,event_time,last_update FROM jobs_queue WHERE host_type=? AND state != 'taken' ORDER BY event_time ASC;")
(stmt1h (sqlite3:prepare db stmt1))
(stmt2 "UPDATE jobs_queue SET state='taken',last_update=? WHERE id=?;")
(stmt2h (sqlite3:prepare db stmt2))
(res (sqlite3:with-transaction
db
(lambda ()
(let* ((matching-jobs (sqlite3:fold-row
(lambda (res . row) ;; id host-type vars exekey state event-time last-update)
(cons row res))
'()
stmt1h
host-type)))
(if (null? matching-jobs)
#f
(let ((choosen-one (let loop ((tal matching-jobs)
(res #f)) ;; put bestest one in here
(if (null? tal)
res
(let ((curr (car tal))
(rem (cdr tal)))
curr) ;; here we will compare with res, if better candidate the loop with curr else loop with res
))))
(if choosen-one ;; we need to mark it as taken
(sqlite3:execute stmt2h (current-seconds) (car choosen-one)))
choosen-one)))))))
(sqlite3:finalize! stmt1h) ;; it'd be nice to cache these and finalize on exit.
(sqlite3:finalize! stmt2h)
res))
;; clean out old jobs in queue, i.e. taken and event_time > 24 hrs ago
;;
(define (db:no-sync-job-records-clean db)
(sqlite3:execute (db:no-sync-db db) "DELETE FROM jobs_queue WHERE state='taken' AND event_time < ?;" (- (current-seconds)(* 24 3600))))
(define (db:no-sync-get/default db-in var default)
(let ((db (db:no-sync-db db-in))
(res default))
(sqlite3:for-each-row
(lambda (val)
(set! res val))
(db:no-sync-db db)
"SELECT val FROM no_sync_metadat WHERE var=?;"
var)
(if res
(let ((newres (if (string? res)
(string->number res)
#f)))
(if newres
newres
res))
res)))
;; This is needed for api.scm
(define (db:open-no-sync-db)
(dbfile:open-no-sync-db (db:get-dbsync-path)))
;; why get the keys from the db? why not get from the *configdat*
;; using keys:config-get-fields?
|