mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-04-04 06:19:44 +00:00
47 lines
1.4 KiB
Rust
47 lines
1.4 KiB
Rust
use crate::prelude::*;
|
|
use crate::service::start_stop::StartStop;
|
|
use crate::service::transition::TransitionKind;
|
|
use crate::service::{Service, ServiceActor};
|
|
use crate::util::actor::{BackgroundJobs, Handler};
|
|
|
|
struct Start;
|
|
#[async_trait::async_trait]
|
|
impl Handler<Start> for ServiceActor {
|
|
type Response = ();
|
|
async fn handle(&mut self, _: Start, _: &mut BackgroundJobs) -> Self::Response {
|
|
self.0.persistent_container.state.send_modify(|x| {
|
|
x.desired_state = StartStop::Start;
|
|
});
|
|
self.0.synchronized.notified().await
|
|
}
|
|
}
|
|
impl Service {
|
|
pub async fn start(&self) -> Result<(), Error> {
|
|
self.actor.send(Start).await
|
|
}
|
|
}
|
|
|
|
struct Stop;
|
|
#[async_trait::async_trait]
|
|
impl Handler<Stop> for ServiceActor {
|
|
type Response = ();
|
|
async fn handle(&mut self, _: Stop, _: &mut BackgroundJobs) -> Self::Response {
|
|
let mut transition_state = None;
|
|
self.0.persistent_container.state.send_modify(|x| {
|
|
x.desired_state = StartStop::Stop;
|
|
if x.transition_state.as_ref().map(|x| x.kind()) == Some(TransitionKind::Restarting) {
|
|
transition_state = std::mem::take(&mut x.transition_state);
|
|
}
|
|
});
|
|
if let Some(restart) = transition_state {
|
|
restart.abort().await;
|
|
}
|
|
self.0.synchronized.notified().await
|
|
}
|
|
}
|
|
impl Service {
|
|
pub async fn stop(&self) -> Result<(), Error> {
|
|
self.actor.send(Stop).await
|
|
}
|
|
}
|