1use std::{collections::HashMap, path::Path, time::Duration};
8
9use libupdated::{
10 checks::SystemdIsSystemRunning,
11 consent_handler::AutoConsent,
12 directory_installer::DirectoryInstaller,
13 directory_source::DirectorySource,
14 file_store::FileStore,
15 log_reporter::LogReporter,
16 trigger::Rate,
17 update_workflow::{
18 UpdateOptions, WorkflowError, get_config, monitor_update, update_with_consent_flow,
19 },
20 workflow,
21};
22use tracing::{error, info};
23
24#[workflow]
29pub async fn example_workflow(config: HashMap<String, String>) -> Result<(), WorkflowError> {
30 let update_interval = get_config(&config, "update_interval_sec")?
33 .parse::<u64>()
34 .map(Duration::from_secs)
35 .map_err(|e| WorkflowError::InvalidConfiguration(e.to_string()))?;
36 let mut trigger = Rate::new(
37 update_interval,
38 UpdateOptions {
39 ignore_previously_declined: true,
40 consent_timeout: update_interval,
41 },
42 );
43
44 let mut source = DirectorySource::new(Path::new(get_config(&config, "source_dir")?.as_str()));
46
47 let mut consent_handler = AutoConsent::new(true);
49
50 let installer = DirectoryInstaller::new(get_config(&config, "target_dir")?.as_str());
52
53 let mut store = FileStore::new(get_config(&config, "sota_state_file")?.as_str());
55
56 let mut progress = LogReporter::new("example_workflow");
58
59 let mut check = SystemdIsSystemRunning {};
61
62 info!(target: "example_workflow", "Workflow configuration finished, starting update loop. Configuration: {:?}", config);
63
64 if let Err(e) = monitor_update(&mut source, &mut store, &mut progress, &mut check).await {
66 error!(target: "example_workflow", "Previous update failed: {}", e);
67 }
68
69 loop {
70 if let Err(e) = update_with_consent_flow(
72 &mut trigger,
73 &mut source,
74 &mut consent_handler,
75 &installer,
76 &mut store,
77 &mut progress,
78 )
79 .await
80 {
81 error!(target: "example_workflow", "Update result: {}", e);
82 } else {
83 info!(target: "example_workflow", "Update completed successfully.");
84 }
85 }
86}