use tokio::sync::watch;
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;
use redup_abstractions::{Dependency, DependencyActivity, DependencyInfo, DependencyState};
use crate::dependency::builder::DependencyBuilder;
use crate::dependency::runner::{Runner, RunnerStart, spawn_supervisor};
use crate::dependency::spec::DependencySpec;
use crate::dependency::watch_settings::WatchSettings;
use crate::error::Error;
pub(crate) struct Dependencies {
module: &'static str,
settings: WatchSettings,
names: Vec<String>,
tasks: Vec<DependencyTask>,
}
struct DependencyTask {
name: String,
token: CancellationToken,
supervisor: JoinHandle<()>,
state: watch::Receiver<DependencyInfo>,
}
#[derive(Debug, Clone)]
pub(crate) struct DependencyHandle {
pub(crate) name: String,
pub(crate) state: watch::Receiver<DependencyInfo>,
}
impl DependencyHandle {
pub(crate) fn info(&self) -> DependencyInfo {
self.state.borrow().clone()
}
}
impl Dependencies {
pub(crate) fn new(module: &'static str, settings: WatchSettings) -> Self {
Self {
module,
settings,
names: Vec::new(),
tasks: Vec::new(),
}
}
pub(crate) fn builder(&mut self, name: impl Into<String>) -> DependencyBuilder<'_> {
DependencyBuilder::new(self, name)
}
pub(crate) fn register(&mut self, spec: DependencySpec) -> Result<Dependency, Error> {
if spec.name.is_empty() {
return Err(Error::Dependency {
module: self.module,
name: spec.name,
reason: "must not be empty",
});
}
if self.names.contains(&spec.name) {
return Err(Error::Dependency {
module: self.module,
name: spec.name,
reason: "is already registered in the module",
});
}
if spec.probe.is_none() && spec.init.is_none() {
return Err(Error::Dependency {
module: self.module,
name: spec.name,
reason: "has neither a probe nor an initialization",
});
}
let mode = spec.mode.unwrap_or(self.settings.mode());
let activity = if mode == crate::watch_mode::WatchMode::Placebo {
DependencyActivity::Placebo
} else {
DependencyActivity::Active
};
let (info, changes) = watch::channel(DependencyInfo::new(
DependencyState::Pending,
activity,
true,
None,
));
let token = CancellationToken::new();
let runner = Runner::new(RunnerStart {
module: self.module,
name: spec.name.clone(),
probe: spec.probe,
init: spec.init,
mode,
settings: self.settings,
info: info.clone(),
changes: changes.clone(),
token: token.clone(),
});
let inner = tokio::spawn(runner.run());
let supervisor = spawn_supervisor(inner, info, self.module, spec.name.clone());
self.names.push(spec.name.clone());
let dependency = Dependency::new(self.module, spec.name.clone(), changes.clone());
self.tasks.push(DependencyTask {
name: spec.name,
token,
supervisor,
state: changes,
});
Ok(dependency)
}
pub(crate) fn handles(&self) -> Vec<DependencyHandle> {
self.tasks
.iter()
.map(|task| DependencyHandle {
name: task.name.clone(),
state: task.state.clone(),
})
.collect()
}
pub(crate) async fn shutdown(&mut self) {
let tasks = std::mem::take(&mut self.tasks);
for task in tasks {
task.token.cancel();
if tokio::time::timeout(self.settings.timeout(), task.supervisor)
.await
.is_err()
{
tracing::warn!(
module = self.module,
dependency = task.name.as_str(),
"dependency task did not stop within the timeout; detached",
);
}
}
}
}