-
Notifications
You must be signed in to change notification settings - Fork 47
/
agents.clj
42 lines (30 loc) · 1 KB
/
agents.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
(ns com.brunobonacci.mulog.agents
(:require [com.brunobonacci.mulog.buffer
:refer [ring-buffer] :as rb])
(:import [java.util.concurrent ScheduledThreadPoolExecutor
TimeUnit ScheduledFuture Future ThreadFactory]))
(defn scheduled-thread-pool
[core-pool-size]
(ScheduledThreadPoolExecutor.
^int core-pool-size
^ThreadFactory
(reify ThreadFactory
(^Thread newThread [this ^Runnable r]
(let [t (Thread. r)]
(.setName t (str "mu/log-task-" (.getId t)))
(.setDaemon t true)
t)))))
(def timer-pool
(scheduled-thread-pool 2))
(defn recurring-task
[delay-millis task]
(let [^ScheduledFuture ftask
(.scheduleAtFixedRate
^ScheduledThreadPoolExecutor timer-pool
(fn [] (try (task) (catch Exception x))) ;; TODO log
delay-millis delay-millis TimeUnit/MILLISECONDS)]
(fn [] (.cancel ftask true))))
(defn buffer-agent
[capacity]
(agent (rb/ring-buffer capacity)
:error-mode :continue))