/
core.clj
143 lines (108 loc) · 3.17 KB
/
core.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(ns flow-combine.core
(:require [clojure.core.async :refer [chan >!! <!! close!]]
[onyx.plugin.core-async :refer [take-segments!]]
[onyx.api]))
(def workflow
[[:in :identity]
[:identity :out]])
(defn my-identity [{:keys [n] :as segment}]
segment)
(def capacity 1000)
(def input-chan (chan capacity))
(def output-chan (chan capacity))
(def batch-size 10)
(def catalog
[{:onyx/name :in
:onyx/plugin :onyx.plugin.core-async/input
:onyx/type :input
:onyx/medium :core.async
:onyx/max-peers 1
:onyx/batch-size batch-size
:onyx/doc "Reads segments from a core.async channel"}
{:onyx/name :identity
:onyx/fn :flow-combine.core/my-identity
:onyx/type :function
:onyx/batch-size batch-size
:parameterized.core/k 42}
{:onyx/name :out
:onyx/plugin :onyx.plugin.core-async/output
:onyx/type :output
:onyx/medium :core.async
:onyx/max-peers 1
:onyx/batch-size batch-size
:onyx/doc "Writes segments to a core.async channel"}])
(def flow-conditions
[{:flow/from :identity
:flow/to [:out]
:flow/predicate [:not :flow-combine.core/odd-segment?]}
{:flow/from :identity
:flow/to [:out]
:flow/predicate :flow-combine.core/equal-to-5?}])
(defn equal-to-5? [event old {:keys [n]} all-new]
(= n 5))
(defn odd-segment? [event old {:keys [n]} all-new]
(odd? n))
(def input-segments
[{:n -3}
{:n -2}
{:n -1}
{:n 0}
{:n 1}
{:n 2}
{:n 3}
{:n 4}
{:n 5}
{:n 6}
{:n 7}
{:n 8}
{:n 9}
:done])
(doseq [segment input-segments]
(>!! input-chan segment))
(close! input-chan)
(def id (java.util.UUID/randomUUID))
(def env-config
{:zookeeper/address "127.0.0.1:2188"
:zookeeper/server? true
:zookeeper.server/port 2188
:onyx/id id})
(def peer-config
{:zookeeper/address "127.0.0.1:2188"
:onyx/id id
:onyx.peer/job-scheduler :onyx.job-scheduler/balanced
:onyx.messaging/impl :aeron
:onyx.messaging/peer-port 40200
:onyx.messaging/bind-addr "localhost"})
(def env (onyx.api/start-env env-config))
(def peer-group (onyx.api/start-peer-group peer-config))
(def n-peers (count (set (mapcat identity workflow))))
(def v-peers (onyx.api/start-peers n-peers peer-group))
(defn inject-in-ch [event lifecycle]
{:core.async/chan input-chan})
(defn inject-out-ch [event lifecycle]
{:core.async/chan output-chan})
(def in-calls
{:lifecycle/before-task-start inject-in-ch})
(def out-calls
{:lifecycle/before-task-start inject-out-ch})
(def lifecycles
[{:lifecycle/task :in
:lifecycle/calls :flow-combine.core/in-calls}
{:lifecycle/task :in
:lifecycle/calls :onyx.plugin.core-async/reader-calls}
{:lifecycle/task :out
:lifecycle/calls :flow-combine.core/out-calls}
{:lifecycle/task :out
:lifecycle/calls :onyx.plugin.core-async/writer-calls}])
(onyx.api/submit-job
peer-config
{:catalog catalog
:workflow workflow :lifecycles lifecycles
:flow-conditions flow-conditions
:task-scheduler :onyx.task-scheduler/balanced})
(def results (take-segments! output-chan))
(clojure.pprint/pprint results)
(doseq [v-peer v-peers]
(onyx.api/shutdown-peer v-peer))
(onyx.api/shutdown-peer-group peer-group)
(onyx.api/shutdown-env env)