supervisor/lib.rs
1#[macro_use] extern crate log;
2
3use std::thread;
4
5/// Spawns a thread and which will restart the work_fn when it produces a Result
6pub fn supervise<F: 'static>(work_fn: F) where F: Fn() -> Result<&'static str, &'static str> + Send {
7 thread::spawn(move || {
8 loop {
9 let result = work_fn();
10
11 if let Err(e) = result {
12 error!("Error: {}", e);
13 }
14 }
15 });
16}
17
18#[test]
19fn supervise_spawn() {
20 supervise(move || {
21 for i in 0 .. 5 {
22 if i == 4 {
23 return Err("failed work reason");
24 }
25 }
26 Ok("finished")
27 });
28}