(restart-agent a new-state & options)
When an agent is failed, changes the agent state to new-state and then un-fails the agent so that sends are allowed again. If a :clear-actions true option is given, any actions queued on the agent that were being held while it was failed will be discarded, otherwise those held actions will proceed. The new-state must pass the validator if any, or restart will throw an exception and the agent will remain failed with its old state and error. Watchers, if any, will NOT be notified of the new state. Throws an exception if the agent is not failed.
(deftest t-rstart
(future (println "running in a thread..."))
(let [agt (agent 0)
; This doesn't work
h01 (fn [a e]
(println :10 "agent error found:" )
(println :11 "restarting agent...")
(restart-agent a 100)
(Thread/sleep 100)
(println :12 "agent restarted, state=" @a))
; This works. Need to call restart-agent in a separate thread
h02 (fn [a e]
(println :20 "agent error found:" )
(future
(println :21 "restarting agent...")
(restart-agent a 200)
(println :22 "agent restarted, state=" @a))) ;=> 200
]
(set-error-handler! agt h02)
(send agt inc)
(Thread/sleep 100) (println :01 @agt) ;=> 1
(Thread/sleep 100) (send agt #(/ % 0))
(Thread/sleep 100) (println :02 @agt) ;=> 200
(Thread/sleep 100) (send agt inc)
(Thread/sleep 100) (println :03 @agt) ;=> 201
))
; Output
; running in a thread...
; :01 1
; :20 agent error found:
; :21 restarting agent...
; :22 agent restarted, state= 200
; :02 200
; :03 201