8000 Fix 2 instances of backend being created by timmo001 · Pull Request #2991 · timmo001/system-bridge · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix 2 instances of backend being created #2991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src-tauri/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ 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();

let base_url = format!("http://{}:{}", BACKEND_HOST, settings.api.port.to_string());

// 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);
}
Expand Down
30 changes: 23 additions & 7 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -85,17 +85,23 @@ 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));
interval.tick().await;
loop {
// Setup the backend server
setup_backend().await;

info!("Waiting for 60 seconds before checking the backend server again");
interval.tick().await;
debug!("Checking backend server..");

// Keep the backend server alive
keep_backend_alive().await;
}
});
});
Expand Down Expand Up @@ -127,9 +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(
&format!("{:?}", std::thread::current().id())
.replace("ThreadId(", "")
.replace(")", "")
),
record.target(),
message
))
Expand All @@ -139,9 +150,14 @@ 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(
&format!("{:?}", std::thread::current().id())
.replace("ThreadId(", "")
.replace(")", "")
),
record.target(),
message
))
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/shared.rs
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
0