From 3918a2086ba5373f72e32c6ab9a7e88bbcd06ecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Vold=C5=99ich?= Date: Tue, 27 Aug 2024 16:46:27 +0200 Subject: [PATCH] Added simple tracing --- Cargo.lock | 2 ++ Cargo.toml | 2 ++ src/main.rs | 14 ++++++++++---- src/services.rs | 28 ++++++++++++++++++++-------- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c242658..89bdf0f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -260,6 +260,8 @@ dependencies = [ "serde", "surrealdb", "tokio", + "tracing", + "tracing-subscriber", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 0d43bf7..d7d9fcd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,5 @@ futures = "0.3.30" serde = { version = "1.0.207", features = ["derive"] } surrealdb = "1.5.4" tokio = { version = "1.39.2", features = ["macros", "rt-multi-thread"] } +tracing = "0.1.40" +tracing-subscriber = "0.3.18" diff --git a/src/main.rs b/src/main.rs index db62886..771bae5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,22 @@ use bcup::{models::ServerConfiguration, services::BackupServerService}; use futures::StreamExt; use surrealdb::{engine::remote::ws::Ws, Notification, Result, Surreal}; +use tracing::{debug, info, Level}; #[tokio::main] async fn main() -> surrealdb::Result<()> { - let db = Surreal::new::("127.0.0.1:8000").await?; + tracing_subscriber::fmt() + .with_max_level(Level::DEBUG) + .init(); + let db = Surreal::new::("127.0.0.1:8000").await?; db.use_ns("test").use_db("test").await?; + info!("Connected to db"); + let service = BackupServerService::new(db.clone()); - println!("Prepared to listen"); + info!("Listening on table `rest_server` for changes"); let mut stream = db.select("rest_server").live().await?; while let Some(result) = stream.next().await { handle(result, &service).await; @@ -19,7 +25,7 @@ async fn main() -> surrealdb::Result<()> { Ok(()) } async fn handle(result: Result>, service: &BackupServerService) { - println!("Something to handle"); + debug!("Handeling incoming changes"); match result { Ok(notification) => { let conf = ¬ification.data; @@ -32,7 +38,7 @@ async fn handle(result: Result>, service: &Bac } async fn process_new_item(conf: &ServerConfiguration, service: &BackupServerService) { - println!("The following item has to be processed {:?}", conf); + debug!("Processing the following item id:`{:?}`", &conf.id); service.spawn_restic_container(conf.directory.clone()).await; service diff --git a/src/services.rs b/src/services.rs index cc34fad..e0d04be 100644 --- a/src/services.rs +++ b/src/services.rs @@ -1,4 +1,5 @@ -use surrealdb::opt::PatchOp; +use surrealdb::{opt::PatchOp, Error}; +use tracing::{debug, error}; use crate::{ models::{Record, ServerConfiguration}, @@ -14,20 +15,31 @@ impl BackupServerService { Self { db } } pub async fn mark_data_as_processed(&self, conf: &ServerConfiguration) { - println!("Updating status on item id:{:?}", conf.id); - let updated: Option = self + debug!("Updating status on item id:{:?}", conf.id); + //let updated: Option = self + let result: Result, Error> = self .db .update(&conf.id) .patch(PatchOp::replace("/state/now", "processed")) - .await - .unwrap(); - dbg!(updated); + .await; + + match result { + Ok(_record) => { + debug!("Status for item id:`{:?}` has been updated", &conf.id); + } + Err(err) => { + error!( + "Could not update status for item {:?}. Err:{:?}", + &conf.id, err + ); + } + } } pub async fn spawn_restic_container(&self, path: String) { - println!("[fake] Spawning container for path {:?}", path); + debug!("[fake] Spawning container for path {:?}", path); } pub async fn add_proxy_configuration(&self, path: String) { - println!("[fake] Creating caddy configuration {:?}", path); + debug!("[fake] Creating caddy configuration {:?}", path); } }