agner_actors/
exit_handler.rs

1use std::fmt;
2
3use agner_utils::std_error_pp::StdErrorPP;
4
5use crate::actor_id::ActorID;
6use crate::exit::{Exit, Shutdown, WellKnown};
7
8/// `ExitHandler` is an entity that is notified when an actor exits.
9///
10/// Each actor has an `ExitHandler` associated with it.
11/// It is possible to specify an exit-handler for an actor via
12/// [`SpawnOpts::with_exit_handler`](crate::spawn_opts::SpawnOpts::with_exit_handler).
13pub trait ExitHandler: fmt::Debug + Send + Sync + 'static {
14    fn on_actor_exit(&self, actor_id: ActorID, exit: Exit);
15}
16
17/// An [`ExitHandler`](crate::exit_handler::ExitHandler) that will log abnormal exits.
18#[derive(Debug, Clone, Copy)]
19pub struct LogExitHandler;
20
21/// A no-op [`ExitHandler`](crate::exit_handler::ExitHandler), i.e. it does nothing.
22#[derive(Debug, Clone, Copy)]
23pub struct NoopExitHandler;
24
25impl ExitHandler for LogExitHandler {
26    fn on_actor_exit(&self, actor_id: ActorID, exit: Exit) {
27        match exit {
28            Exit::Standard(WellKnown::Normal | WellKnown::Shutdown(Shutdown(None))) => (),
29            Exit::Standard(WellKnown::Linked(offender, reason)) => {
30                tracing::warn!("[{}] linked {} exited: {}", actor_id, offender, reason.pp());
31            },
32            failure => {
33                tracing::error!("[{}] {}", actor_id, failure.pp())
34            },
35        }
36    }
37}
38
39impl ExitHandler for NoopExitHandler {
40    fn on_actor_exit(&self, _actor_id: ActorID, _exit: Exit) {}
41}