-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.clj
49 lines (43 loc) · 1.95 KB
/
daemon.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
(ns memento.redis.poll.daemon
"Daemon thread namespace.
On every thread wakeup we try to fetch results that are being awaited from redis.
There are longer, rarer jobs to do:
- maintain expiry of load markers (generally about each second)
- secondary indexes cleanup (generally every 4 seconds)"
(:require [memento.redis.loader :as loader]
[memento.redis.sec-index :as sec-index]
[taoensso.timbre :as log]))
(def sleep-interval
"Time in ms between thread wakeups."
(Long/parseLong (System/getProperty "memento.redis.daemon_interval" "40")))
(def load-markers-interval 1000)
(def sec-index-interval
"Time in ms to perform secondary index cleanups (removes entries pointing to
non-existent keys)"
(Long/parseLong (System/getProperty "memento.redis.sec_index_interval" "4071")))
(defn perform-step
"Perform maintenance steps, given a map of last time each type was done."
[action-timestamps]
(let [current (System/currentTimeMillis)
load-markers? (< load-markers-interval (- current (:load-markers action-timestamps 0)))
sec-index? (< sec-index-interval (- current (:sec-index action-timestamps 0)))
new-timestamps (cond-> action-timestamps
load-markers? (assoc :load-markers current :sec-index current))]
(try
(loader/maintenance-step loader/maint load-markers?)
(when sec-index?
(sec-index/maintenance-step sec-index/all-indexes))
new-timestamps
(catch Exception e
(log/warn e "Error running maintenance step")
new-timestamps))))
(defonce daemon-thread
(delay
(doto
(Thread. ^Runnable (fn []
(loop [action-timestamps {}]
(Thread/sleep ^long sleep-interval)
(recur (perform-step action-timestamps))))
"Memento Daemon")
(.setDaemon true)
(.start))))