Skip to content
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

Notify startup fail #304

Merged
merged 15 commits into from
Aug 12, 2023
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 38 additions & 25 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(clippy::comparison_chain, clippy::type_complexity)]

use crate::error::ErrorKind;
use crate::event_broadcaster::EventBroadcaster;
use crate::migration::migrate;
use crate::prelude::{
Expand Down Expand Up @@ -373,12 +374,12 @@ pub struct Args {

pub async fn run(
args: Args,
) -> (
) -> Result<(
impl Future<Output = ()>,
AppState,
tracing_appender::non_blocking::WorkerGuard,
tokio::sync::oneshot::Sender<()>,
) {
), Error> {
Ynng marked this conversation as resolved.
Show resolved Hide resolved
let _ = color_eyre::install().map_err(|e| {
error!("Failed to install color_eyre: {}", e);
});
Expand All @@ -387,19 +388,24 @@ pub async fn run(
} else {
PathBuf::from(match std::env::var("LODESTONE_PATH") {
Ok(v) => v,
Err(_) => home::home_dir()
.unwrap_or_else(|| {
std::env::current_dir().expect("what kinda os are you running lodestone on???")
})
.join(".lodestone")
.to_str()
.unwrap()
.to_string(),
Err(_) => {
match home::home_dir()
.unwrap_or_else(|| {
std::env::current_dir().expect("what kinda os are you running lodestone on???")
})
.join(".lodestone")
.to_str() {
Some(p) => p.to_string(),
None => return Err(Error { kind: ErrorKind::Internal, source: Report::msg("Failed to get home dir") }),
}
}
})
};
init_paths(lodestone_path.clone());
info!("Lodestone path: {}", lodestone_path.display());
std::env::set_current_dir(&lodestone_path).unwrap();
std::env::set_current_dir(&lodestone_path).map_err(|_|
Error { kind: ErrorKind::Internal, source: Report::msg("Failed to set current dir"), }
)?;
let guard = setup_tracing();
if args.is_desktop {
info!("Lodestone Core running in Tauri");
Expand All @@ -415,7 +421,9 @@ pub async fn run(
let lock_file =
std::fs::File::create(lockfile_path.as_path()).expect("failed to create lockfile");
if lock_file.try_lock_exclusive().is_err() {
panic!("Another instance of lodestone might be running");
return Err(Error {
kind: ErrorKind::Internal, source: Report::msg("Another instance of lodestone is running"),
});
}

let _ = migrate(&lodestone_path).map_err(|e| {
Expand All @@ -427,15 +435,15 @@ pub async fn run(

let mut users_manager = UsersManager::new(tx.clone(), HashMap::new(), path_to_users().clone());

users_manager.load_users().await.unwrap();
users_manager.load_users().await?;

let mut global_settings = GlobalSettings::new(
path_to_global_settings().clone(),
tx.clone(),
GlobalSettingsData::default(),
);

global_settings.load_from_file().await.unwrap();
global_settings.load_from_file().await?;

let first_time_setup_key = if !users_manager.as_ref().iter().any(|(_, user)| user.is_owner) {
let key = rand_alphanumeric(16);
Expand All @@ -456,13 +464,12 @@ pub async fn run(
let macro_executor = MacroExecutor::new(tx.clone(), tokio::runtime::Handle::current());
let instances = restore_instances(&path_to_instances, tx.clone(), macro_executor.clone())
.await
.map_err(|e| {
error!(
"Failed to restore instances: {}, lodestone will now crash...",
e
);
})
.unwrap();
.map_err(|_| {
Error {
kind: ErrorKind::Internal,
source: Report::msg("failed to restore instances"),
}
})?;

let mut allocated_ports = HashSet::new();
for instance_entry in instances.iter() {
Expand All @@ -488,11 +495,17 @@ pub async fn run(
"sqlite://{}/data.db",
path_to_stores().display()
))
.unwrap()
.map_err(|_| { Error {
kind: ErrorKind::Internal,
source: Report::msg("Failed to create sqlite connection options"),
}})?
.create_if_missing(true),
)
.await
.unwrap(),
.map_err(|_| { Error {
kind: ErrorKind::Internal,
source: Report::msg("Failed to create sqlite pool"),
}})?,
};

init_app_state(shared_state.clone());
Expand Down Expand Up @@ -574,7 +587,7 @@ pub async fn run(
.await;
let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();

(
Ok((
{
let shared_state = shared_state.clone();
async move {
Expand Down Expand Up @@ -720,5 +733,5 @@ pub async fn run(
shared_state,
guard,
shutdown_tx,
)
))
}
2 changes: 1 addition & 1 deletion core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ use lodestone_core::Args;
#[tokio::main]
async fn main() {
let args = Args::parse();
lodestone_core::run(args).await.0.await;
lodestone_core::run(args).await.unwrap().0.await;
}
2 changes: 2 additions & 0 deletions dashboard/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dashboard/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tauri-plugin-localhost = "0.1.0"
portpicker = "0.1"
lodestone_core = { path = "../../core" }
tokio = { version = "1.21.1", features = ["macros", "rt"] }
notify-rust = "4"
tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }

[features]
Expand Down
42 changes: 35 additions & 7 deletions dashboard/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use tauri::{utils::config::AppUrl, WindowUrl};
use tauri::{CustomMenuItem, SystemTray, SystemTrayEvent, SystemTrayMenu};

use notify_rust::Notification;
#[derive(Clone, serde::Serialize)]
struct Payload {
args: Vec<String>,
Expand Down Expand Up @@ -50,12 +51,33 @@

#[tokio::main]
async fn main() {
let (core_fut, app_state, _guard, shutdown_tx) = lodestone_core::run(lodestone_core::Args {
let run_result = lodestone_core::run(lodestone_core::Args {
is_cli: false,
is_desktop: true,
lodestone_path: None,
})
.await;

let (core_fut, app_state, _guard, shutdown_tx);
match run_result {
Ok((fut, state, guard, tx)) => {
core_fut = fut;
app_state = state;
_guard = guard;
shutdown_tx = tx;
}
Err(e) => {
Notification::new()
.summary("Lodestone failed to start")
.body(&format!("Oh no! Looks like Lodestone was unable to start. Error: {}", e))
.auto_icon()
.show()
.expect("Failed to show notification");

return
}
}

let shutdown_tx = std::sync::Mutex::new(Some(shutdown_tx));
tokio::spawn(async {
core_fut.await;
Expand All @@ -81,7 +103,7 @@

let tray_menu = SystemTrayMenu::new().add_item(quit);

builder
if let Err(e) = builder
.plugin(tauri_plugin_single_instance::init(|app, argv, cwd| {
app.emit_all("single-instance", Payload { args: argv, cwd }).unwrap();
}))
Expand All @@ -92,13 +114,13 @@
get_owner_jwt,
get_first_time_setup_key
])
.on_window_event(|event| match event.event() {
tauri::WindowEvent::CloseRequested { api, .. } => {
event.window().hide().unwrap();
api.prevent_close();
}
_ => {}
})

Check warning on line 123 in dashboard/src-tauri/src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`

warning: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let` --> dashboard/src-tauri/src/main.rs:117:34 | 117 | .on_window_event(|event| match event.event() { | __________________________________^ 118 | | tauri::WindowEvent::CloseRequested { api, .. } => { 119 | | event.window().hide().unwrap(); 120 | | api.prevent_close(); 121 | | } 122 | | _ => {} 123 | | }) | |_________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_match = note: `#[warn(clippy::single_match)]` on by default help: try this | 117 ~ .on_window_event(|event| if let tauri::WindowEvent::CloseRequested { api, .. } = event.event() { 118 + event.window().hide().unwrap(); 119 + api.prevent_close(); 120 ~ }) |
.system_tray(SystemTray::new().with_menu(tray_menu))
.on_system_tray_event(move |app, event| match event {
SystemTrayEvent::MenuItemClick { id, .. } => {
Expand All @@ -109,14 +131,20 @@
app.exit(0);
}
}

SystemTrayEvent::LeftClick { .. } => {
let window = app.get_window("main").unwrap();
window.show().unwrap();
window.set_focus().unwrap();
}
_ => {}
})
.run(context)
.expect("error while running tauri application");
_ => {}
})
.run(context)
{
Notification::new()
.summary("Lodestone failed to start")
.body(&format!("Oh no! Looks like Lodestone was unable to start. Error: {}", e))
.auto_icon()
.show()
.expect("Failed to show notification");
}
}
Loading