watchexec/crates/lib/src/action/process_holder.rs

71 lines
1.6 KiB
Rust
Raw Normal View History

use std::sync::Arc;
2022-01-28 14:37:01 +01:00
use tokio::sync::RwLock;
2022-01-30 12:05:10 +01:00
use tracing::trace;
2022-01-28 14:37:01 +01:00
use crate::{command::Supervisor, error::RuntimeError, signal::process::SubSignal};
#[derive(Clone, Debug, Default)]
pub struct ProcessHolder(Arc<RwLock<Option<Supervisor>>>);
impl ProcessHolder {
pub async fn is_running(&self) -> bool {
self.0
.read()
.await
.as_ref()
2023-01-06 14:53:49 +01:00
.map_or(false, Supervisor::is_running)
2022-01-28 14:37:01 +01:00
}
pub async fn is_some(&self) -> bool {
self.0.read().await.is_some()
}
pub async fn drop_inner(&self) {
2022-01-30 12:05:10 +01:00
trace!("dropping supervisor");
2022-01-28 14:37:01 +01:00
self.0.write().await.take();
2022-01-30 12:05:10 +01:00
trace!("dropped supervisor");
2022-01-28 14:37:01 +01:00
}
pub async fn replace(&self, new: Supervisor) {
2022-01-30 12:05:10 +01:00
trace!("replacing supervisor");
2022-01-28 14:37:01 +01:00
if let Some(_old) = self.0.write().await.replace(new) {
2022-01-30 12:05:10 +01:00
trace!("replaced supervisor");
// TODO: figure out what to do with old
2022-01-30 12:05:10 +01:00
} else {
trace!("not replaced: no supervisor");
2022-01-28 14:37:01 +01:00
}
}
pub async fn signal(&self, sig: SubSignal) {
if let Some(p) = self.0.read().await.as_ref() {
2022-01-30 12:05:10 +01:00
trace!("signaling supervisor");
2022-01-28 14:37:01 +01:00
p.signal(sig).await;
2022-01-30 12:05:10 +01:00
trace!("signaled supervisor");
} else {
trace!("not signaling: no supervisor");
2022-01-28 14:37:01 +01:00
}
}
pub async fn kill(&self) {
if let Some(p) = self.0.read().await.as_ref() {
2022-01-30 12:05:10 +01:00
trace!("killing supervisor");
2022-01-28 14:37:01 +01:00
p.kill().await;
2022-01-30 12:05:10 +01:00
trace!("killed supervisor");
} else {
trace!("not killing: no supervisor");
2022-01-28 14:37:01 +01:00
}
}
pub async fn wait(&self) -> Result<(), RuntimeError> {
if let Some(p) = self.0.read().await.as_ref() {
2022-01-30 12:05:10 +01:00
trace!("waiting on supervisor");
p.wait().await?;
2022-01-30 12:05:10 +01:00
trace!("waited on supervisor");
} else {
trace!("not waiting: no supervisor");
2022-01-28 14:37:01 +01:00
}
2022-01-30 12:05:10 +01:00
Ok(())
2022-01-28 14:37:01 +01:00
}
}