Skip to content

Commit

Permalink
Renamed of Callbacks's methods and fixed the formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v2d0g committed Nov 9, 2019
1 parent 66f979d commit 42780c8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 61 deletions.
1 change: 0 additions & 1 deletion bastion/examples/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ fn sp(supervisor: Supervisor) -> Supervisor {
println!("(sp ) after_stop");
});


let children_ref = supervisor.children_ref(sp_ch);

supervisor
Expand Down
48 changes: 14 additions & 34 deletions bastion/src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ use std::fmt::{self, Debug, Formatter};
use std::sync::Arc;

pub trait BeforeStart: Fn() + Send + Sync + 'static {}

impl<T> BeforeStart for T where T: Fn() + Send + Sync + 'static {}

pub trait BeforeRestart: Fn() + Send + Sync + 'static {}

impl<T> BeforeRestart for T where T: Fn() + Send + Sync + 'static {}

pub trait AfterRestart: Fn() + Send + Sync + 'static {}

impl<T> AfterRestart for T where T: Fn() + Send + Sync + 'static {}

pub trait AfterStop: Fn() + Send + Sync + 'static {}

impl<T> AfterStop for T where T: Fn() + Send + Sync + 'static {}

#[derive(Default)]
Expand Down Expand Up @@ -54,61 +50,45 @@ impl Callbacks {
self
}

pub fn before_start(&self) -> Option<&dyn BeforeStart> {
if let Some(before_start) = &self.before_start {
Some(&**before_start)
} else {
None
}
pub fn contains_before_start(&self) -> bool {
self.before_start.is_some()
}

pub fn before_restart(&self) -> Option<&dyn BeforeRestart> {
if let Some(before_restart) = &self.before_restart {
Some(&**before_restart)
} else {
None
}
pub fn contains_before_restart(&self) -> bool {
self.before_restart.is_some()
}

pub fn after_restart(&self) -> Option<&dyn AfterRestart> {
if let Some(after_restart) = &self.after_restart {
Some(&**after_restart)
} else {
None
}
pub fn contains_after_restart(&self) -> bool {
self.after_restart.is_some()
}

pub fn after_stop(&self) -> Option<&dyn AfterStop> {
if let Some(after_stop) = &self.after_stop {
Some(&**after_stop)
} else {
None
}
pub fn contains_after_stop(&self) -> bool {
self.after_stop.is_some()
}

pub(crate) fn call_before_start(&self) {
pub(crate) fn before_start(&self) {
if let Some(before_start) = &self.before_start {
before_start()
}
}

pub(crate) fn call_before_restart(&self) {
pub(crate) fn before_restart(&self) {
if let Some(before_restart) = &self.before_restart {
before_restart()
} else {
self.call_after_stop()
self.after_stop()
}
}

pub(crate) fn call_after_restart(&self) {
pub(crate) fn after_restart(&self) {
if let Some(after_restart) = &self.after_restart {
after_restart()
} else {
self.call_before_start()
self.before_start()
}
}

pub(crate) fn call_after_stop(&self) {
pub(crate) fn after_stop(&self) {
if let Some(after_stop) = &self.after_stop {
after_stop()
}
Expand Down
42 changes: 21 additions & 21 deletions bastion/src/supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl Supervisor {

let killed = killed.contains(supervised.id());
if killed {
supervised.callbacks().call_before_restart();
supervised.callbacks().before_restart();
}

let bcast = Broadcast::new(parent.clone());
Expand All @@ -173,9 +173,9 @@ impl Supervisor {
let supervised = supervised.reset(bcast).await.unwrap();
// FIXME: might not keep order
if killed {
supervised.callbacks().call_after_restart();
supervised.callbacks().after_restart();
} else {
supervised.callbacks().call_before_start();
supervised.callbacks().before_start();
}

supervised
Expand Down Expand Up @@ -259,8 +259,8 @@ impl Supervisor {
/// [`SupervisorRef`]: ../struct.SupervisorRef.html
/// [`supervisor_ref`]: #method.supervisor_ref
pub fn supervisor<S>(self, init: S) -> Self
where
S: FnOnce(Supervisor) -> Supervisor,
where
S: FnOnce(Supervisor) -> Supervisor,
{
let parent = Parent::supervisor(self.as_ref());
let bcast = Broadcast::new(parent);
Expand Down Expand Up @@ -312,8 +312,8 @@ impl Supervisor {
/// [`SupervisorRef`]: ../struct.SupervisorRef.html
/// [`supervisor`]: #method.supervisor
pub fn supervisor_ref<S>(&mut self, init: S) -> SupervisorRef
where
S: FnOnce(Supervisor) -> Supervisor,
where
S: FnOnce(Supervisor) -> Supervisor,
{
let parent = Parent::supervisor(self.as_ref());
let bcast = Broadcast::new(parent);
Expand Down Expand Up @@ -374,8 +374,8 @@ impl Supervisor {
/// [`ChildrenRef`]: children/struct.ChildrenRef.html
/// [`children_ref`]: #method.children_ref
pub fn children<C>(self, init: C) -> Self
where
C: FnOnce(Children) -> Children,
where
C: FnOnce(Children) -> Children,
{
let parent = Parent::supervisor(self.as_ref());
let bcast = Broadcast::new(parent);
Expand Down Expand Up @@ -437,8 +437,8 @@ impl Supervisor {
/// [`ChildrenRef`]: children/struct.ChildrenRef.html
/// [`children`]: #method.children
pub fn children_ref<C>(&self, init: C) -> ChildrenRef
where
C: FnOnce(Children) -> Children,
where
C: FnOnce(Children) -> Children,
{
let parent = Parent::supervisor(self.as_ref());
let bcast = Broadcast::new(parent);
Expand Down Expand Up @@ -531,7 +531,7 @@ impl Supervisor {

let killed = killed.contains(supervised.id());
if killed {
supervised.callbacks().call_before_restart();
supervised.callbacks().before_restart();
}

let bcast = Broadcast::new(parent.clone());
Expand All @@ -540,9 +540,9 @@ impl Supervisor {
let supervised = supervised.reset(bcast).await.unwrap();
// FIXME: might not keep order
if killed {
supervised.callbacks().call_after_restart();
supervised.callbacks().after_restart();
} else {
supervised.callbacks().call_before_start();
supervised.callbacks().before_start();
}

supervised
Expand Down Expand Up @@ -589,7 +589,7 @@ impl Supervisor {
Some(supervised) => {
let id = supervised.id().clone();

supervised.callbacks().call_after_stop();
supervised.callbacks().after_stop();
self.stopped.insert(id, supervised);
}
// FIXME
Expand Down Expand Up @@ -650,7 +650,7 @@ impl Supervisor {
// FIXME: panics?
let supervised = launched.await.unwrap();
dbg!();
supervised.callbacks().call_before_restart();
supervised.callbacks().before_restart();

self.bcast.unregister(supervised.id());

Expand All @@ -659,7 +659,7 @@ impl Supervisor {
let id = bcast.id().clone();
// FIXME: panics?
let supervised = supervised.reset(bcast).await.unwrap();
supervised.callbacks().call_after_restart();
supervised.callbacks().after_restart();

self.bcast.register(supervised.bcast());
if self.started {
Expand Down Expand Up @@ -707,11 +707,11 @@ impl Supervisor {
BastionMessage::Deploy(deployment) => {
let supervised = match deployment {
Deployment::Supervisor(supervisor) => {
supervisor.callbacks().call_before_start();
supervisor.callbacks().before_start();
Supervised::supervisor(supervisor)
}
Deployment::Children(children) => {
children.callbacks().call_before_start();
children.callbacks().before_start();
Supervised::children(children)
}
};
Expand All @@ -727,7 +727,7 @@ impl Supervisor {
self.launched
.insert(id.clone(), (self.order.len(), launched));
self.order.push(id);
},
}
// FIXME
BastionMessage::Prune { .. } => unimplemented!(),
BastionMessage::SuperviseWith(strategy) => {
Expand All @@ -742,7 +742,7 @@ impl Supervisor {
// TODO: add a "waiting" list an poll from it instead of awaiting
// FIXME: panics?
let supervised = launched.await.unwrap();
supervised.callbacks().call_after_stop();
supervised.callbacks().after_stop();

self.bcast.unregister(&id);
self.stopped.insert(id, supervised);
Expand Down
10 changes: 5 additions & 5 deletions bastion/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl System {

// TODO: set a limit?
async fn recover(&mut self, mut supervisor: Supervisor) {
supervisor.callbacks().call_before_restart();
supervisor.callbacks().before_restart();

let parent = Parent::system();
let bcast = if supervisor.id() == &NIL_ID {
Expand All @@ -87,7 +87,7 @@ impl System {

let id = bcast.id().clone();
supervisor.reset(bcast).await;
supervisor.callbacks().call_after_restart();
supervisor.callbacks().after_restart();

self.bcast.register(supervisor.bcast());

Expand Down Expand Up @@ -144,7 +144,7 @@ impl System {
self.started = false;

for supervisor in self.stop().await {
supervisor.callbacks().call_after_stop();
supervisor.callbacks().after_stop();
}

return Err(());
Expand All @@ -158,7 +158,7 @@ impl System {
}
BastionMessage::Deploy(deployment) => match deployment {
Deployment::Supervisor(supervisor) => {
supervisor.callbacks().call_before_start();
supervisor.callbacks().before_start();

self.bcast.register(supervisor.bcast());
if self.started {
Expand Down Expand Up @@ -214,7 +214,7 @@ impl System {
if self.restart.remove(&id) {
self.recover(supervisor).await;
} else {
supervisor.callbacks().call_after_stop();
supervisor.callbacks().after_stop();
}

continue;
Expand Down

0 comments on commit 42780c8

Please sign in to comment.