Added simple tracing

This commit is contained in:
Martin Voldřich 2024-08-27 16:46:27 +02:00
parent 6816a69706
commit 3918a2086b
4 changed files with 34 additions and 12 deletions

2
Cargo.lock generated
View File

@ -260,6 +260,8 @@ dependencies = [
"serde", "serde",
"surrealdb", "surrealdb",
"tokio", "tokio",
"tracing",
"tracing-subscriber",
] ]
[[package]] [[package]]

View File

@ -8,3 +8,5 @@ futures = "0.3.30"
serde = { version = "1.0.207", features = ["derive"] } serde = { version = "1.0.207", features = ["derive"] }
surrealdb = "1.5.4" surrealdb = "1.5.4"
tokio = { version = "1.39.2", features = ["macros", "rt-multi-thread"] } tokio = { version = "1.39.2", features = ["macros", "rt-multi-thread"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"

View File

@ -1,16 +1,22 @@
use bcup::{models::ServerConfiguration, services::BackupServerService}; use bcup::{models::ServerConfiguration, services::BackupServerService};
use futures::StreamExt; use futures::StreamExt;
use surrealdb::{engine::remote::ws::Ws, Notification, Result, Surreal}; use surrealdb::{engine::remote::ws::Ws, Notification, Result, Surreal};
use tracing::{debug, info, Level};
#[tokio::main] #[tokio::main]
async fn main() -> surrealdb::Result<()> { async fn main() -> surrealdb::Result<()> {
let db = Surreal::new::<Ws>("127.0.0.1:8000").await?; tracing_subscriber::fmt()
.with_max_level(Level::DEBUG)
.init();
let db = Surreal::new::<Ws>("127.0.0.1:8000").await?;
db.use_ns("test").use_db("test").await?; db.use_ns("test").use_db("test").await?;
info!("Connected to db");
let service = BackupServerService::new(db.clone()); 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?; let mut stream = db.select("rest_server").live().await?;
while let Some(result) = stream.next().await { while let Some(result) = stream.next().await {
handle(result, &service).await; handle(result, &service).await;
@ -19,7 +25,7 @@ async fn main() -> surrealdb::Result<()> {
Ok(()) Ok(())
} }
async fn handle(result: Result<Notification<ServerConfiguration>>, service: &BackupServerService) { async fn handle(result: Result<Notification<ServerConfiguration>>, service: &BackupServerService) {
println!("Something to handle"); debug!("Handeling incoming changes");
match result { match result {
Ok(notification) => { Ok(notification) => {
let conf = &notification.data; let conf = &notification.data;
@ -32,7 +38,7 @@ async fn handle(result: Result<Notification<ServerConfiguration>>, service: &Bac
} }
async fn process_new_item(conf: &ServerConfiguration, service: &BackupServerService) { 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.spawn_restic_container(conf.directory.clone()).await;
service service

View File

@ -1,4 +1,5 @@
use surrealdb::opt::PatchOp; use surrealdb::{opt::PatchOp, Error};
use tracing::{debug, error};
use crate::{ use crate::{
models::{Record, ServerConfiguration}, models::{Record, ServerConfiguration},
@ -14,20 +15,31 @@ impl BackupServerService {
Self { db } Self { db }
} }
pub async fn mark_data_as_processed(&self, conf: &ServerConfiguration) { pub async fn mark_data_as_processed(&self, conf: &ServerConfiguration) {
println!("Updating status on item id:{:?}", conf.id); debug!("Updating status on item id:{:?}", conf.id);
let updated: Option<Record> = self //let updated: Option<Record> = self
let result: Result<Option<Record>, Error> = self
.db .db
.update(&conf.id) .update(&conf.id)
.patch(PatchOp::replace("/state/now", "processed")) .patch(PatchOp::replace("/state/now", "processed"))
.await .await;
.unwrap();
dbg!(updated); 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) { 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) { pub async fn add_proxy_configuration(&self, path: String) {
println!("[fake] Creating caddy configuration {:?}", path); debug!("[fake] Creating caddy configuration {:?}", path);
} }
} }