aector/supervision/
simple_restart_strategy.rs

1use crate::actor::{Actor, ExitReason};
2use crate::actor::Backup;
3use crate::supervision::supervision::{SuperVisionAction, SupervisionStrategy};
4use crate::supervision::supervision::SuperVisionAction::{Exit, Restart};
5
6/// Implements a simple restart strategy where the supervised actor is instantly restarted unless
7/// the actor requested the stop itself, in which case the actor is stopped and removed from
8/// the actor system.
9pub struct SimpleRestartStrategy {}
10
11impl SimpleRestartStrategy {
12    pub fn new() -> Box<Self> {
13        Box::new(Self {})
14    }
15}
16
17impl<S: Send + Clone> SupervisionStrategy<S> for SimpleRestartStrategy {
18    fn apply(&mut self, exit_reason: ExitReason, backup: &Backup<S>, actor: &mut Actor<S>) -> SuperVisionAction {
19        match exit_reason {
20            ExitReason::Kill => {
21                println!("ActorSys: actor died on purpose");
22                return Exit;
23            }
24            ExitReason::Restart => {
25                println!("ActorSys: actor requested a restart. Restarting actor with initial state and behavior");
26                actor.apply_backup(&backup);
27                return Restart;
28            },
29            ExitReason::Error => {
30                println!("ActorSys: actor ran into error or triggered restart. Restarting actor with initial state and behavior");
31                actor.apply_backup(&backup);
32                return Restart;
33            }
34        }
35    }
36}