Recovering from failure
Crash a worker, wait for its supervisor to restart it, and send it more work. Compare its count before and after.
Try a change
Change the worker's initial count from 0 to 100. Run again and see which value returns after the restart.
recovery.hew
actor Worker {
var count: i64 = 0;
receive fn add(n: i64) -> i64 {
count = count + n;
count
}
receive fn crash() {
panic("a deliberate failure");
}
}
supervisor Service {
strategy: one_for_one;
intensity: 3 within 60s;
child worker: Worker;
}
fn main() {
let service = spawn Service;
let worker = service.worker;
let before = worker.add(5).expect("worker ready");
println(f"Before the crash: {before}");
match worker.crash() {
.Ok(_) => println("The worker finished without crashing."),
.Err(_) => println("The failed call returned an error."),
}
restarted(worker);
let fresh = worker.add(0).expect("worker restarted");
println(f"After restart: {fresh}");
let next = worker.add(2).expect("worker accepts new work");
println(f"After new work: {next}");
stop(service);
stopped(service);
}Press Escape to leave the editor. runs your code.
Output
Run the example to see its output.
Expected from the original example
Before the crash: 5 The failed call returned an error. After restart: 0 After new work: 2
What the restart changes
crash() deliberately panics. You’ll see a diagnostic in the output and an error returned to the caller. The match reports the error without stopping the caller. Then restarted(worker) waits for recovery before the program sends more work.
The supervisor creates a fresh worker from its initial fields. Its previous in-memory count is gone. Restarting the actor does not replay the failed call: the final increment is new work sent by this program.
Restart policy and limits
one_for_one restarts the failed child. intensity: 3 within 60s allows three restarts in that window. A fourth crash exhausts the restart budget and the child becomes unavailable. Waiting with restarted(worker) still finishes in that case, and later calls still return a Result.
Build on the example
hew.run lets you work with source files and tests. Copy or download your edited code to continue there. To arrange actors on a canvas and inspect the generated code, try the Hew Studio beta.
Run it on your machine
Install Hew, then download an example and run it from the folder where you saved it.
macOS / Linux
hew build recovery.hew -o recovery && ./recoveryWindows PowerShell
hew build recovery.hew -o recovery.exe
.\recovery.exe