move to fn for instrument

This commit is contained in:
Robin Appelman 2022-05-24 13:16:03 +02:00
commit 210d39fd3d

View file

@ -1,5 +1,5 @@
use demostf_client::{ApiClient, Demo, ListOrder, ListParams}; use demostf_client::{ApiClient, Demo, ListOrder, ListParams};
use main_error::MainError; use main_error::{MainError, MainResult};
use md5::Context; use md5::Context;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::{copy, create_dir_all, remove_file, write, File}; use std::fs::{copy, create_dir_all, remove_file, write, File};
@ -9,7 +9,7 @@ use std::time::Duration;
use thiserror::Error; use thiserror::Error;
use time::OffsetDateTime; use time::OffsetDateTime;
use tokio::time::timeout; use tokio::time::timeout;
use tracing::{error, info, info_span, instrument, warn}; use tracing::{error, info, instrument, warn};
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum Error { pub enum Error {
@ -70,14 +70,30 @@ async fn main() -> Result<(), MainError> {
let source_path = generate_path(&source_root, name); let source_path = generate_path(&source_root, name);
let target_path = generate_path(&target_root, name); let target_path = generate_path(&target_root, name);
let _span = info_span!(
"name",
demo = demo.id,
source_path = display(source_path.display()),
target_path = display(target_path.display()),
)
.entered();
move_demo(
&client,
&demo,
source_path,
target_path,
&target_backend,
&api_key,
)
.await?;
}
Ok(())
}
#[instrument(skip_all, fields(demo = demo.id, target_path = display(target_path.display()), source_path = display(source_path.display())))]
async fn move_demo(
client: &ApiClient,
demo: &Demo,
source_path: PathBuf,
target_path: PathBuf,
target_backend: &str,
api_key: &str,
) -> MainResult {
if !source_path.is_file() { if !source_path.is_file() {
warn!("source not found, re-downloading"); warn!("source not found, re-downloading");
re_download(&client, &target_path, &demo).await?; re_download(&client, &target_path, &demo).await?;
@ -116,11 +132,11 @@ async fn main() -> Result<(), MainError> {
if let Err(err) = client if let Err(err) = client
.set_url( .set_url(
demo.id, demo.id,
&target_backend, target_backend,
&demo.path, &demo.path,
&demo.url, &demo.url,
calculated_hash, calculated_hash,
&api_key, api_key,
) )
.await .await
{ {
@ -129,8 +145,6 @@ async fn main() -> Result<(), MainError> {
return Err(err.into()); return Err(err.into());
} }
remove_file(source_path)?; remove_file(source_path)?;
}
Ok(()) Ok(())
} }