Add initial_state parameter to ServiceCore::init.

This commit is contained in:
Alejandro Cabeza Romero 2024-12-23 19:01:30 +01:00
parent 0453500626
commit 9230ac119a
No known key found for this signature in database
GPG Key ID: DA3D14AE478030FD
8 changed files with 34 additions and 13 deletions

View File

@ -21,7 +21,7 @@ pub struct ServiceHandle<S: ServiceData> {
/// Handle to overwatch
overwatch_handle: OverwatchHandle,
settings: SettingsUpdater<S::Settings>,
initial_state: S::State,
pub initial_state: S::State,
}
/// Service core resources
@ -42,6 +42,7 @@ pub struct ServiceRunner<S: ServiceData> {
service_state: ServiceStateHandle<S>,
state_handle: StateHandle<S::State, S::StateOperator>,
lifecycle_handle: LifecycleHandle,
initial_state: S::State,
}
impl<S: ServiceData> ServiceHandle<S> {
@ -91,7 +92,7 @@ impl<S: ServiceData> ServiceHandle<S> {
// add relay channel to handle
self.outbound_relay = Some(outbound_relay);
let settings = self.settings.notifier().get_updated_settings();
let operator = S::StateOperator::from_settings::<S::Settings>(settings);
let operator = S::StateOperator::from_settings(settings);
let (state_handle, state_updater) =
StateHandle::<S::State, S::StateOperator>::new(self.initial_state.clone(), operator);
@ -109,6 +110,7 @@ impl<S: ServiceData> ServiceHandle<S> {
service_state,
state_handle,
lifecycle_handle,
initial_state: self.initial_state.clone(),
}
}
}
@ -132,10 +134,11 @@ where
service_state,
state_handle,
lifecycle_handle,
initial_state,
} = self;
let runtime = service_state.overwatch_handle.runtime().clone();
let service = S::init(service_state)?;
let service = S::init(service_state, initial_state)?;
runtime.spawn(service.run());
runtime.spawn(state_handle.run());

View File

@ -43,7 +43,10 @@ pub trait ServiceData {
#[async_trait]
pub trait ServiceCore: Sized + ServiceData {
/// Initialize the service with the given state
fn init(service_state: ServiceStateHandle<Self>) -> Result<Self, super::DynError>;
fn init(
service_state: ServiceStateHandle<Self>,
initial_state: Self::State,
) -> Result<Self, super::DynError>;
/// Service main loop
async fn run(mut self) -> Result<(), super::DynError>;

View File

@ -31,7 +31,7 @@ pub trait StateOperator {
/// The type of state that the operator can handle
type StateInput: ServiceState;
/// Operator initialization method. Can be implemented over some subset of settings
fn from_settings<Settings>(settings: Settings) -> Self;
fn from_settings(settings: <Self::StateInput as ServiceState>::Settings) -> Self;
/// Asynchronously perform an operation for a given state
async fn run(&mut self, state: Self::StateInput);
}
@ -57,7 +57,7 @@ impl<T> Clone for NoOperator<T> {
impl<StateInput: ServiceState> StateOperator for NoOperator<StateInput> {
type StateInput = StateInput;
fn from_settings<Settings>(_settings: Settings) -> Self {
fn from_settings(_settings: <Self::StateInput as ServiceState>::Settings) -> Self {
NoOperator(PhantomData)
}
@ -230,7 +230,7 @@ mod test {
impl StateOperator for PanicOnGreaterThanTen {
type StateInput = UsizeCounter;
fn from_settings<Settings>(_settings: Settings) -> Self {
fn from_settings(_settings: <Self::StateInput as ServiceState>::Settings) -> Self {
Self
}

View File

@ -25,7 +25,10 @@ impl ServiceData for CancellableService {
#[async_trait::async_trait]
impl ServiceCore for CancellableService {
fn init(service_state: ServiceStateHandle<Self>) -> Result<Self, DynError> {
fn init(
service_state: ServiceStateHandle<Self>,
_initial_state: Self::State,
) -> Result<Self, DynError> {
Ok(Self { service_state })
}

View File

@ -39,7 +39,10 @@ impl<T: Send> ServiceCore for GenericService<T>
where
T: Debug + 'static + Sync,
{
fn init(state: ServiceStateHandle<Self>) -> Result<Self, overwatch_rs::DynError> {
fn init(
state: ServiceStateHandle<Self>,
_initial_state: Self::State,
) -> Result<Self, overwatch_rs::DynError> {
Ok(Self {
state,
_phantom: std::marker::PhantomData,

View File

@ -28,7 +28,10 @@ impl ServiceData for PrintService {
#[async_trait]
impl ServiceCore for PrintService {
fn init(state: ServiceStateHandle<Self>) -> Result<Self, overwatch_rs::DynError> {
fn init(
state: ServiceStateHandle<Self>,
_initial_state: Self::State,
) -> Result<Self, overwatch_rs::DynError> {
Ok(Self { state })
}

View File

@ -29,7 +29,10 @@ impl ServiceData for SettingsService {
#[async_trait]
impl ServiceCore for SettingsService {
fn init(state: ServiceStateHandle<Self>) -> Result<Self, overwatch_rs::DynError> {
fn init(
state: ServiceStateHandle<Self>,
_initial_state: Self::State,
) -> Result<Self, overwatch_rs::DynError> {
Ok(Self { state })
}

View File

@ -50,7 +50,7 @@ pub struct CounterStateOperator;
impl StateOperator for CounterStateOperator {
type StateInput = CounterState;
fn from_settings<Settings>(_settings: Settings) -> Self {
fn from_settings(_settings: <Self::StateInput as ServiceState>::Settings) -> Self {
CounterStateOperator
}
@ -75,7 +75,10 @@ impl ServiceData for UpdateStateService {
#[async_trait]
impl ServiceCore for UpdateStateService {
fn init(state: ServiceStateHandle<Self>) -> Result<Self, overwatch_rs::DynError> {
fn init(
state: ServiceStateHandle<Self>,
_initial_state: Self::State,
) -> Result<Self, overwatch_rs::DynError> {
Ok(Self { state })
}