use std::process::ExitCode;
#[cfg(feature = "cli")]
use clap::Parser;
#[cfg(feature = "systemd")]
use sd_notify::{NotifyState, notify, watchdog_enabled};
use tokio::{
signal::unix::{SignalKind, signal},
task::JoinSet,
};
use tokio_util::sync::CancellationToken;
use tracing::{error, info};
#[cfg(feature = "systemd")]
use std::time::Duration;
use crate::{LOG, update_workflow::WorkflowPlugin};
#[cfg(feature = "cli")]
const VERSION: &str = env!("VERGEN_GIT_DESCRIBE");
#[cfg(feature = "cli")]
const BUILD_DATE: &str = env!("VERGEN_BUILD_DATE");
#[cfg(feature = "cli")]
const TARGET_TRIPLE: &str = env!("VERGEN_CARGO_TARGET_TRIPLE");
pub struct Updated {
workflow_name: String,
config_file: String,
join_set: JoinSet<Result<(), ExitCode>>,
ct: CancellationToken,
}
#[cfg(feature = "cli")]
#[derive(Parser)]
#[command(name = "updated")]
#[command(version = VERSION)]
#[command(about = "SOTA workflow runner", long_about = None)]
struct Cli {
#[arg(short, long)]
workflow: String,
#[arg(short, long)]
config: String,
}
impl Updated {
pub fn new(workflow_name: String, config_file: String) -> Self {
let join_set = JoinSet::new();
let ct = CancellationToken::new();
Self {
workflow_name,
config_file,
join_set,
ct,
}
}
#[cfg(feature = "cli")]
pub fn from_cli() -> Self {
let cli = Cli::parse();
Self::new(cli.workflow, cli.config)
}
#[cfg(feature = "systemd")]
pub fn spawn_watchdog_task(&mut self) {
let mut usec: u64 = 0;
if watchdog_enabled(false, &mut usec) {
info!(target: LOG, "Systemd watchdog is enabled with a {} µs timeout. Sending keep alive.", usec);
self.join_set.spawn(Self::keep_alive(
self.ct.clone(),
Duration::from_micros(usec / 2),
));
} else {
info!(target: LOG, "Systemd watchdog is not enabled.");
}
}
pub fn spawn_signal_handler(&mut self) {
let ct_clone = self.ct.clone();
self.join_set.spawn(async move {
let result = Self::os_signals(&ct_clone).await;
ct_clone.cancel();
result
});
}
pub async fn run(mut self) -> ExitCode {
let ct_clone = self.ct.clone();
let workflow_name = self.workflow_name.clone();
let config_file = self.config_file.clone();
self.join_set.spawn(async move {
match Self::init(&ct_clone, workflow_name, config_file).await {
Ok(()) => Ok(()),
Err(code) => {
ct_clone.cancel();
Err(code)
}
}
});
#[cfg(feature = "systemd")]
notify(false, &[NotifyState::Ready]).expect("Could not send READY=1 to systemd.");
let mut stopping = false;
for result in self.join_set.join_all().await.iter() {
if !stopping {
#[cfg(feature = "systemd")]
notify(false, &[NotifyState::Stopping])
.expect("Failed to send STOPPING=1 to systemd.");
stopping = true;
}
match result {
Err(code) => return *code,
_ => continue,
}
}
ExitCode::SUCCESS
}
pub fn available_workflow_names() -> Vec<&'static str> {
inventory::iter::<WorkflowPlugin>
.into_iter()
.map(|w| w.name)
.collect()
}
pub async fn init(
ct: &CancellationToken,
workflow_name: String,
config_file: String,
) -> Result<(), ExitCode> {
#[cfg(feature = "cli")]
info!(
target: LOG,
version = VERSION,
date = BUILD_DATE,
target = TARGET_TRIPLE,
"{}.",
clap::crate_description!()
);
let workflow_fn = inventory::iter::<WorkflowPlugin>
.into_iter()
.find(|&w| w.name == workflow_name)
.ok_or_else(|| {
error!(target: LOG, "Workflow not found: {}", workflow_name);
ExitCode::FAILURE
})?
.func;
let config: std::collections::HashMap<String, String> = if config_file.ends_with(".json") {
let content = tokio::fs::read_to_string(&config_file).await.map_err(|e| {
error!(target: LOG, "Could not parse workflow config as json file: {}", e);
ExitCode::FAILURE
})?;
serde_json::from_str(&content).expect("Could not parse workflow config file.")
} else {
dotenvy::from_filename_override(&config_file).map_err(|e| {
error!(target: LOG, "Could not parse workflow config as env file: {}", e);
ExitCode::FAILURE
})?;
let mut config = std::collections::HashMap::new();
for (k, v) in dotenvy::vars() {
config.insert(k.to_lowercase(), v.to_string());
}
config
};
tokio::select! {
_ = ct.cancelled() => {
info!(target: LOG, "Shutting down.");
}
result = workflow_fn(config) => {
match result {
Ok(exit_code) => {
info!(target: LOG, "Update workflow finished with exit code {exit_code:?}.");
if exit_code != ExitCode::SUCCESS {
return Err(exit_code);
}
},
Err(e) => {
error!(target: LOG, "Update workflow failed: {}", e);
return Err(ExitCode::FAILURE);
}
};
}
};
Ok(())
}
#[cfg(feature = "systemd")]
async fn keep_alive(
ct: CancellationToken,
watchdog_duration: Duration,
) -> Result<(), ExitCode> {
use tokio::time::sleep;
use tracing::trace;
loop {
tokio::select! {
_ = ct.cancelled() => {
break;
}
_ = sleep(watchdog_duration) => {
trace!(target: LOG, "Sending systemd keep alive.");
notify(false, &[NotifyState::Watchdog]).expect("Could not send WATCHDOG=1 to systemd.");
}
}
}
Ok(())
}
async fn wait_for_signal(ct: &CancellationToken) -> Result<(), std::io::Error> {
let mut sighup = signal(SignalKind::hangup())?;
let mut sigint = signal(SignalKind::interrupt())?;
let mut sigquit = signal(SignalKind::quit())?;
let mut sigterm = signal(SignalKind::terminate())?;
tokio::select! {
_ = sighup.recv() => {
info!(target: LOG, signal = "SIGHUP", "Signal received.");
}
_ = sigint.recv() => {
info!(target: LOG, signal = "SIGINT", "Signal received.");
}
_ = sigquit.recv() => {
info!(target: LOG, signal = "SIGQUIT", "Signal received.");
}
_ = sigterm.recv() => {
info!(target: LOG, signal = "SIGTERM", "Signal received.");
}
_ = ct.cancelled() => {}
}
Ok(())
}
pub async fn os_signals(ct: &CancellationToken) -> Result<(), ExitCode> {
Self::wait_for_signal(ct).await.map_err(|e| {
error!(target: LOG, "Failed to register signal handler: {}", e);
ExitCode::FAILURE
})?;
Ok(())
}
}