Skip to content

Instantly share code, notes, and snippets.

@r2p2
Created October 17, 2010 12:57
Show Gist options
  • Save r2p2/630825 to your computer and use it in GitHub Desktop.
Save r2p2/630825 to your computer and use it in GitHub Desktop.
Nur ein paar Gedanken, wie eine WFE in Clojure umgesetzt werden könnte, runter programmiert. Wenn Agents tatsächlich so eingesetzt werden sollen, könnte ich mich wohl nur schwer mit ihnen anfreunden.
(defn start-process-by-agent [process-agent]
(send-off process-agent (fn [process-data] "running")))
(defn stop-process-by-agent [process-agent]
(send-off process-agent (fn [process-data] "stopped")))
(defn init [wfe-id]
(println "initiating wfe")
(println "TODO: load unfinished processes")
(agent (hash-map
:process-list (hash-map)
:state "stopped"
:next-pid 1)))
(defn start [wfe-agent]
(send wfe-agent (fn [wfe-data]
(let [processes (get wfe-data :process-list)]
(do
(map start-process-by-agent (get processes :process-agent))
(assoc wfe-data :state "running"))))))
(defn stop [wfe-agent]
(send wfe-agent (fn [wfe-data]
(let [processes (get wfe-data :process-list)]
(do
(map stop-process-by-agent (get processes :process-agent))
(assoc wfe-data :state "stopping"))))))
(defn start-process [wfe-agent workflow]
(send wfe-agent (fn [wfe-data]
(let [processes (get wfe-data :process-list)
process-agent (agent (hash-map
:state "started"
:work-item {}))
process-id (get wfe-data :next-pid)
new-process-list (assoc processes process-id process-agent)]
(assoc wfe-data
:process-list new-process-list
:next-pid (inc process-id))))))
(defn engine-state [wfe-agent]
(do
(await wfe-agent)
(get (deref wfe-agent) :state)))
(defn number-of-processes [wfe-agent]
(do
(await wfe-agent)
(count (get (deref wfe-agent) :process-list))))
(defn testrun []
(let [engine (init "foo")]
(do
(start engine)
(println (engine-state engine))
(println (number-of-processes engine))
(start-process engine "blubb")
(println (number-of-processes engine))
(stop engine)
(println (number-of-processes engine))
(println (engine-state engine)))))
(testrun)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment