Skip to content

Commit

Permalink
Expose ability to extract supervisor from actor cell
Browse files Browse the repository at this point in the history
Resolves #294
  • Loading branch information
slawlor committed Dec 5, 2024
1 parent c0663b7 commit 1b57549
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ractor/src/actor/actor_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,14 @@ impl ActorCell {
self.inner.tree.stop_all_children(reason);
}

/// Tries to retrieve this actor's supervisor.
///
/// Returns [None] if this actor has no supervisor at the given instance or
/// [Some(ActorCell)] supervisor if one is configured.
pub fn try_get_superivisor(&self) -> Option<ActorCell> {
self.inner.tree.try_get_supervisor()
}

/// Stop any children of this actor, and wait for their collective exit, optionally
/// threading the optional reason to all children
///
Expand Down
5 changes: 5 additions & 0 deletions ractor/src/actor/supervision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ impl SupervisionTree {
*(self.supervisor.lock().unwrap()) = None;
}

/// Try and retrieve the set supervisor
pub(crate) fn try_get_supervisor(&self) -> Option<ActorCell> {
self.supervisor.lock().unwrap().clone()
}

/// Terminate all your supervised children and unlink them
/// from the supervision tree since the supervisor is shutting down
/// and can't deal with superivison events anyways
Expand Down
4 changes: 4 additions & 0 deletions ractor/src/actor/tests/supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ async fn test_supervision_panic_in_post_startup() {
.await
.expect("Child panicked on startup");

let maybe_sup = child_ref.try_get_superivisor();
assert!(maybe_sup.is_some());
assert_eq!(maybe_sup.map(|a| a.get_id()), Some(supervisor_ref.get_id()));

let (_, _) = tokio::join!(s_handle, c_handle);

assert_eq!(child_ref.get_id().pid(), flag.load(Ordering::Relaxed));
Expand Down

0 comments on commit 1b57549

Please sign in to comment.