use std::{collections::HashMap, path::Path, time::Duration};
use libupdated::{
checks::SystemdIsSystemRunning,
consent_handler::AutoConsent,
directory_installer::DirectoryInstaller,
directory_source::DirectorySource,
file_store::FileStore,
log_reporter::LogReporter,
trigger::Rate,
update_workflow::{
UpdateOptions, WorkflowError, get_config, monitor_update, update_with_consent_flow,
},
workflow,
};
use tracing::{error, info};
#[workflow]
pub async fn example_workflow(config: HashMap<String, String>) -> Result<(), WorkflowError> {
let update_interval = get_config(&config, "update_interval_sec")?
.parse::<u64>()
.map(Duration::from_secs)
.map_err(|e| WorkflowError::InvalidConfiguration(e.to_string()))?;
let mut trigger = Rate::new(
update_interval,
UpdateOptions {
ignore_previously_declined: true,
consent_timeout: update_interval,
},
);
let mut source = DirectorySource::new(Path::new(get_config(&config, "source_dir")?.as_str()));
let mut consent_handler = AutoConsent::new(true);
let installer = DirectoryInstaller::new(get_config(&config, "target_dir")?.as_str());
let mut store = FileStore::new(get_config(&config, "sota_state_file")?.as_str());
let mut progress = LogReporter::new("example_workflow");
let mut check = SystemdIsSystemRunning {};
info!(target: "example_workflow", "Workflow configuration finished, starting update loop. Configuration: {:?}", config);
if let Err(e) = monitor_update(&mut source, &mut store, &mut progress, &mut check).await {
error!(target: "example_workflow", "Previous update failed: {}", e);
}
loop {
if let Err(e) = update_with_consent_flow(
&mut trigger,
&mut source,
&mut consent_handler,
&installer,
&mut store,
&mut progress,
)
.await
{
error!(target: "example_workflow", "Update result: {}", e);
} else {
info!(target: "example_workflow", "Update completed successfully.");
}
}
}