From cc08434acc30f78912e934e379f9e9f6b7826273 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Fri, 5 Apr 2024 13:13:41 +0100 Subject: [PATCH 1/7] Lower level --- src-tauri/src/shared.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/shared.rs b/src-tauri/src/shared.rs index 2ed9639ab..732f851dd 100644 --- a/src-tauri/src/shared.rs +++ b/src-tauri/src/shared.rs @@ -1,11 +1,11 @@ -use log::info; +use log::debug; use platform_dirs::AppDirs; pub fn get_data_path() -> String { // Get data path from {localappdata}\timmo001\systembridge let app_dirs = AppDirs::new(Some("timmo001"), true).unwrap(); let data_path = app_dirs.data_dir.to_str().unwrap().to_string(); - info!("Data path: {}", data_path); + debug!("Data path: {}", data_path); let path = format!("{}/systembridge", data_path); From b35b078f50967d8813b71ff5407811b4479df8c5 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Fri, 5 Apr 2024 13:13:55 +0100 Subject: [PATCH 2/7] Move calls --- src-tauri/src/backend.rs | 4 +--- src-tauri/src/main.rs | 12 ++++++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src-tauri/src/backend.rs b/src-tauri/src/backend.rs index 83733f079..893e42e69 100644 --- a/src-tauri/src/backend.rs +++ b/src-tauri/src/backend.rs @@ -10,9 +10,7 @@ use crate::{ pub const BACKEND_HOST: &str = "127.0.0.1"; -pub async fn setup_backend() { - info!("Setting up backend server.."); - +pub async fn keep_backend_alive() { // Get settings let settings: Settings = get_settings(); diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index c63bea682..396e95813 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -18,7 +18,7 @@ use tokio::runtime::Runtime; use tokio::time::interval; use crate::{ - backend::{setup_backend, stop_backend}, + backend::{keep_backend_alive, stop_backend}, gui::setup_gui, resources::start_application, shared::get_data_path, @@ -85,17 +85,21 @@ async fn run() { info!("Backend is disabled"); } else { // Setup the backend server + info!("Setting up backend server.."); + let _handle = thread::spawn(move || { let rt = Runtime::new().unwrap(); rt.block_on(async { + // Keep the backend server alive + keep_backend_alive().await; // Check backend server is running every 60 seconds let mut interval: tokio::time::Interval = interval(Duration::from_secs(60)); loop { - // Setup the backend server - setup_backend().await; - info!("Waiting for 60 seconds before checking the backend server again"); interval.tick().await; + + // Keep the backend server alive + keep_backend_alive().await; } }); }); From 3925ed47a91c2f0ba71f3b4194b9126fd5511532 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Fri, 5 Apr 2024 13:22:07 +0100 Subject: [PATCH 3/7] Add thread to log --- src-tauri/src/main.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 396e95813..c46d47d56 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -131,9 +131,10 @@ fn setup_logger() -> Result<(), fern::InitError> { let stdout_config = fern::Dispatch::new() .format(move |out, message, record| { out.finish(format_args!( - "[{} {} {}] {}", + "[{} {} {} {}] {}", humantime::format_rfc3339(std::time::SystemTime::now()), colors.color(record.level()), + std::thread::current().name().unwrap_or("unnamed"), record.target(), message )) @@ -143,9 +144,10 @@ fn setup_logger() -> Result<(), fern::InitError> { let file_config = fern::Dispatch::new() .format(move |out, message, record| { out.finish(format_args!( - "[{} {} {}] {}", + "[{} {} {} {}] {}", humantime::format_rfc3339(std::time::SystemTime::now()), record.level(), + std::thread::current().name().unwrap_or("unnamed"), record.target(), message )) From 5a90187bfe0f884fd9e22b47a1d07fa21a7934bf Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Fri, 5 Apr 2024 13:22:22 +0100 Subject: [PATCH 4/7] Split wait --- src-tauri/src/main.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index c46d47d56..74b90c406 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -9,7 +9,7 @@ mod shared; mod websocket; use fern::colors::{Color, ColoredLevelConfig}; -use log::{info, LevelFilter}; +use log::{debug, info, LevelFilter}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use std::thread; @@ -93,10 +93,12 @@ async fn run() { // Keep the backend server alive keep_backend_alive().await; // Check backend server is running every 60 seconds - let mut interval: tokio::time::Interval = interval(Duration::from_secs(60)); + let mut interval: tokio::time::Interval = interval(Duration::from_secs(30)); loop { info!("Waiting for 60 seconds before checking the backend server again"); interval.tick().await; + debug!("30 seconds passed"); + interval.tick().await; // Keep the backend server alive keep_backend_alive().await; From 24ffe6b3a786fb2b6ab8950dcaf1bc3485c57e71 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Fri, 5 Apr 2024 13:22:31 +0100 Subject: [PATCH 5/7] Flip --- src-tauri/src/backend.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/backend.rs b/src-tauri/src/backend.rs index 893e42e69..325b69339 100644 --- a/src-tauri/src/backend.rs +++ b/src-tauri/src/backend.rs @@ -18,10 +18,10 @@ pub async fn keep_backend_alive() { // Check if the backend server is running let backend_active = check_backend(base_url.clone()).await; - if !backend_active.is_ok() { + if backend_active.is_err() { // Start the backend server let backend_start = start_backend().await; - if !backend_start.is_ok() { + if backend_start.is_err() { info!("Failed to start the backend server"); std::process::exit(1); } From c81a4cf98b39b71acf5584de5badb9c12105c319 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Fri, 5 Apr 2024 13:31:43 +0100 Subject: [PATCH 6/7] Refactor log message formatting in main.rs --- src-tauri/src/main.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 74b90c406..916daab7e 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -133,10 +133,14 @@ fn setup_logger() -> Result<(), fern::InitError> { let stdout_config = fern::Dispatch::new() .format(move |out, message, record| { out.finish(format_args!( - "[{} {} {} {}] {}", + "{} {} ({}) [{}] {}", humantime::format_rfc3339(std::time::SystemTime::now()), colors.color(record.level()), - std::thread::current().name().unwrap_or("unnamed"), + std::thread::current().name().unwrap_or( + &format!("{:?}", std::thread::current().id()) + .replace("ThreadId(", "") + .replace(")", "") + ), record.target(), message )) @@ -149,7 +153,11 @@ fn setup_logger() -> Result<(), fern::InitError> { "[{} {} {} {}] {}", humantime::format_rfc3339(std::time::SystemTime::now()), record.level(), - std::thread::current().name().unwrap_or("unnamed"), + std::thread::current().name().unwrap_or( + &format!("{:?}", std::thread::current().id()) + .replace("ThreadId(", "") + .replace(")", "") + ), record.target(), message )) From 1bbabf6649befb2a3d5b9e07486b80c169dbc9fd Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Fri, 5 Apr 2024 13:36:31 +0100 Subject: [PATCH 7/7] Update interval to 60 seconds in main.rs --- src-tauri/src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 916daab7e..8508ff1c3 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -93,12 +93,12 @@ async fn run() { // Keep the backend server alive keep_backend_alive().await; // Check backend server is running every 60 seconds - let mut interval: tokio::time::Interval = interval(Duration::from_secs(30)); + let mut interval: tokio::time::Interval = interval(Duration::from_secs(60)); + interval.tick().await; loop { info!("Waiting for 60 seconds before checking the backend server again"); interval.tick().await; - debug!("30 seconds passed"); - interval.tick().await; + debug!("Checking backend server.."); // Keep the backend server alive keep_backend_alive().await;