-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plug services lifecycle (Shutdown and Kill) (#27)
* Plug lifecycle channel * Pipe lifecycle to services. * Add explaining doc on handle clone * Fix tests * Add missing break on overwatch kill * Added shutdown service test * Clippy happy * Use try_from instead of from for checking duplicated service ids on ServicesLifeCycleHandle * Added docs
- Loading branch information
1 parent
6e6678b
commit ac28d01
Showing
10 changed files
with
314 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[workspace] | ||
|
||
resolver = "2" | ||
members = [ | ||
"overwatch-rs", | ||
"overwatch-derive", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// std | ||
use std::borrow::Cow; | ||
use std::collections::HashMap; | ||
use std::default::Default; | ||
use std::error::Error; | ||
// crates | ||
use tokio::sync::broadcast::Sender; | ||
// internal | ||
use crate::services::life_cycle::{FinishedSignal, LifecycleHandle, LifecycleMessage}; | ||
use crate::services::ServiceId; | ||
use crate::DynError; | ||
|
||
/// Grouper handle for the `LifecycleHandle` of each spawned service. | ||
#[derive(Clone)] | ||
pub struct ServicesLifeCycleHandle { | ||
handlers: HashMap<ServiceId, LifecycleHandle>, | ||
} | ||
|
||
impl ServicesLifeCycleHandle { | ||
pub fn empty() -> Self { | ||
Self { | ||
handlers: Default::default(), | ||
} | ||
} | ||
|
||
/// Send a `Shutdown` message to the specified service | ||
/// | ||
/// # Arguments | ||
/// | ||
/// `service` - The `ServiceId` of the target service | ||
/// `sender` - A sender side of a broadcast channel. A return signal when finished handling the | ||
/// message will be sent. | ||
pub fn shutdown( | ||
&self, | ||
service: ServiceId, | ||
sender: Sender<FinishedSignal>, | ||
) -> Result<(), DynError> { | ||
self.handlers | ||
.get(service) | ||
.unwrap() | ||
.send(LifecycleMessage::Shutdown(sender))?; | ||
Ok(()) | ||
} | ||
|
||
/// Send a `Kill` message to the specified service (`ServiceId`) | ||
/// | ||
/// # Arguments | ||
/// | ||
/// `service` - The `ServiceId` of the target service | ||
pub fn kill(&self, service: ServiceId) -> Result<(), DynError> { | ||
self.handlers | ||
.get(service) | ||
.unwrap() | ||
.send(LifecycleMessage::Kill) | ||
} | ||
|
||
/// Send a `Kill` message to all services registered in this handle | ||
pub fn kill_all(&self) -> Result<(), DynError> { | ||
for service_id in self.services_ids() { | ||
self.kill(service_id)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Get all services ids registered in this handle | ||
pub fn services_ids(&self) -> impl Iterator<Item = ServiceId> + '_ { | ||
self.handlers.keys().copied() | ||
} | ||
} | ||
|
||
impl<const N: usize> TryFrom<[(ServiceId, LifecycleHandle); N]> for ServicesLifeCycleHandle { | ||
// TODO: On errors refactor extract into a concrete error type with `thiserror` | ||
type Error = Box<dyn Error + Send + Sync>; | ||
|
||
fn try_from(value: [(ServiceId, LifecycleHandle); N]) -> Result<Self, Self::Error> { | ||
let mut handlers = HashMap::new(); | ||
for (service_id, handle) in value { | ||
if handlers.contains_key(service_id) { | ||
return Err(Box::<dyn Error + Send + Sync>::from(Cow::Owned(format!( | ||
"Duplicated serviceId: {service_id}" | ||
)))); | ||
} | ||
handlers.insert(service_id, handle); | ||
} | ||
Ok(Self { handlers }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.