|
| 1 | +#[macro_use] |
| 2 | +extern crate log; |
| 3 | + |
| 4 | +use snafu::ResultExt; |
| 5 | +use std::{collections::HashMap}; |
| 6 | + |
| 7 | +const API_PENDING_URI_BASE: &str = "/v2/tx"; |
| 8 | +const API_COMMIT_URI_BASE: &str = "/tx/commit"; |
| 9 | + |
| 10 | +pub mod error { |
| 11 | + use http::StatusCode; |
| 12 | + use snafu::Snafu; |
| 13 | + |
| 14 | + /// Potential errors during user data management. |
| 15 | + #[derive(Debug, Snafu)] |
| 16 | + #[snafu(visibility(pub(super)))] |
| 17 | + pub enum SettingsCommitterError { |
| 18 | + #[snafu(display("Error sending {} to {}: {}", method, uri, source))] |
| 19 | + APIRequest { |
| 20 | + method: String, |
| 21 | + uri: String, |
| 22 | + #[snafu(source(from(apiclient::Error, Box::new)))] |
| 23 | + source: Box<apiclient::Error>, |
| 24 | + }, |
| 25 | + |
| 26 | + #[snafu(display("Error {} when sending {} to {}: {}", code, method, uri, response_body))] |
| 27 | + APIResponse { |
| 28 | + method: String, |
| 29 | + uri: String, |
| 30 | + code: StatusCode, |
| 31 | + response_body: String, |
| 32 | + }, |
| 33 | + } |
| 34 | +} |
| 35 | +pub use error::SettingsCommitterError; |
| 36 | +pub type Result<T> = std::result::Result<T, error::SettingsCommitterError>; |
| 37 | + |
| 38 | +/// Checks pending settings and logs them. We don't want to prevent a |
| 39 | +/// commit if there's a blip in retrieval or parsing of the pending |
| 40 | +/// settings. We know the system won't be functional without a commit, |
| 41 | +/// but we can live without logging what was committed. |
| 42 | +async fn check_pending_settings<S: AsRef<str>>(socket_path: S, transaction: &str) { |
| 43 | + let uri = format!("{API_PENDING_URI_BASE}?tx={transaction}"); |
| 44 | + |
| 45 | + debug!("GET-ing {uri} to determine if there are pending settings"); |
| 46 | + let get_result = apiclient::raw_request(socket_path.as_ref(), &uri, "GET", None).await; |
| 47 | + let response_body = match get_result { |
| 48 | + Ok((code, response_body)) => { |
| 49 | + if !code.is_success() { |
| 50 | + warn!("Got {code} when sending GET to {uri}: {response_body}"); |
| 51 | + return; |
| 52 | + } |
| 53 | + response_body |
| 54 | + } |
| 55 | + Err(err) => { |
| 56 | + warn!("Failed to GET pending settings from {uri}: {err}"); |
| 57 | + return; |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + let pending_result: serde_json::Result<HashMap<String, serde_json::Value>> = |
| 62 | + serde_json::from_str(&response_body); |
| 63 | + match pending_result { |
| 64 | + Ok(pending) => { |
| 65 | + debug!("Pending settings for tx {}: {:?}", transaction, &pending); |
| 66 | + } |
| 67 | + Err(err) => { |
| 68 | + warn!("Failed to parse response from {uri}: {err}"); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/// Commits pending settings to live. |
| 74 | +async fn commit_pending_settings<S: AsRef<str>>(socket_path: S, transaction: &str) -> Result<()> { |
| 75 | + let uri = format!("{API_COMMIT_URI_BASE}?tx={transaction}"); |
| 76 | + debug!("POST-ing to {uri} to move pending settings to live"); |
| 77 | + |
| 78 | + if let Err(e) = apiclient::raw_request(socket_path.as_ref(), &uri, "POST", None).await { |
| 79 | + match e { |
| 80 | + // Some types of response errors are OK for this use. |
| 81 | + apiclient::Error::ResponseStatus { code, body, .. } => { |
| 82 | + if code.as_u16() == 422 { |
| 83 | + info!("settings-committer found no settings changes to commit"); |
| 84 | + return Ok(()); |
| 85 | + } else { |
| 86 | + return error::APIResponseSnafu { |
| 87 | + method: "POST", |
| 88 | + uri, |
| 89 | + code, |
| 90 | + response_body: body, |
| 91 | + } |
| 92 | + .fail(); |
| 93 | + } |
| 94 | + } |
| 95 | + // Any other type of error means we couldn't even make the request. |
| 96 | + _ => { |
| 97 | + return Err(e).context(error::APIRequestSnafu { |
| 98 | + method: "POST", |
| 99 | + uri, |
| 100 | + }); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + Ok(()) |
| 105 | +} |
| 106 | + |
| 107 | +pub async fn commit(socket_path: &str, transaction: &str) -> Result<()> { |
| 108 | + if log_enabled!(log::Level::Debug) { |
| 109 | + // We log the pending settings at Debug, so only fetch them if they won't be filtered. |
| 110 | + info!("Checking pending settings."); |
| 111 | + check_pending_settings(socket_path, transaction).await; |
| 112 | + } |
| 113 | + |
| 114 | + info!("Committing settings."); |
| 115 | + commit_pending_settings(socket_path, transaction).await?; |
| 116 | + |
| 117 | + Ok(()) |
| 118 | +} |
0 commit comments