liveBook · Manning

Restart-agent

As explained in Clojure: The Essential Reference

restart-agent is a Clojure function that clears an agent's error condition and resumes processing, optionally discarding pending actions.

restart-agent clears an agent’s error condition, restores a supplied value, and allows queued processing to continue. It is used after an agent action fails. section 14.6

Use restart-agent to:

  • Reset the agent to a chosen state after inspecting its failure with agent-error. section 14.6
  • Resume processing after an error, optionally retaining or discarding pending actions. section 14.6
  • Avoid restarting an agent that is already running; doing so raises an error. section 14.6

Recover an agent after failure

After (send-off a #(/ % 0)), inspect the failure with (agent-error a), then call (restart-agent a 2). The agent can subsequently process another action; calling restart-agent again while it is healthy raises “Agent does not need a restart.” section 14.6

(restart-agent a 2) ; #1
(send-off a #(/ % 2)) ; #2
@a
(restart-agent a 2) ; #3
;; Execution error at user/eval339 (REPL:1).
;; Agent does not need a restart

Shows recovery from division by zero, subsequent action processing, and the healthy-agent error case. section 14.6

How Clojure: The Essential Reference covers it

How deep it goes
Demonstrates reset, resumption, pending-work handling, and the error raised when restarting a healthy agent.
Examples
Uses a division-by-zero action, agent-error inspection, restart-agent a 2, and a subsequent successful action.
What it assumes
Assumes agents, asynchronous sequential actions, dereferencing, and agent error inspection.
What you can do after
Apply restart-agent to reset a failed agent and resume its queued processing.
How it connects
Connects agent-error, set-error-handler!, and error-mode with agent failure recovery.

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.