/
keep_alive.clj
37 lines (30 loc) · 1.14 KB
/
keep_alive.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
(ns de.otto.tesla.stateful.keep-alive
"This component is responsible for keeping the system alive by creating a non-deamonized noop thread."
(:require [clojure.tools.logging :as log]
[com.stuartsierra.component :as component])
(:import (java.util.concurrent CountDownLatch)))
(defn exit-keep-alive []
(log/info "<- stopping keepalive thread: " (.getName (Thread/currentThread))))
(defn enter-keep-alive []
(log/info "-> starting keepalive thread: " (.getName (Thread/currentThread))))
(defn wait-for-count-down-latch [cdl]
(enter-keep-alive)
(.await cdl)
(exit-keep-alive))
(defn start-keep-alive-thread [cd-latch]
(doto (Thread. ^Runnable (partial wait-for-count-down-latch cd-latch) "tesla-ms-keep-alive")
(.start)))
(defrecord KeepAlive [cd-latch]
component/Lifecycle
(start [self]
(log/info "-> starting keepalive")
(let [cd-latch (CountDownLatch. 1)]
(assoc self
:thread (start-keep-alive-thread cd-latch)
:cd-latch cd-latch)))
(stop [self]
(log/info "<- stopping keepalive")
(.countDown cd-latch)
(dissoc self :thread)))
(defn new-keep-alive []
(map->KeepAlive {}))