Skip to content

Commit

Permalink
Merge pull request #282 from autonomys/restart-on-crash
Browse files Browse the repository at this point in the history
Restart on crash
  • Loading branch information
nazar-pc authored Aug 9, 2024
2 parents c8a163c + d814315 commit e7da401
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 21 deletions.
1 change: 1 addition & 0 deletions res/translations/en/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ main_menu_exit = Exit
status_bar_message_configuration_is_invalid = Configuration is invalid: {$error}
status_bar_message_restart_is_needed_for_configuration = Application restart is needed for configuration changes to take effect
status_bar_message_failed_to_save_configuration = Failed to save configuration changes: {$error}
status_bar_message_restarted_after_crash = Space Acres automatically restarted after crash, check application and system logs for details
status_bar_button_restart = Restart
status_bar_button_ok = Ok
Expand Down
2 changes: 2 additions & 0 deletions res/translations/rs/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ main_menu_exit = Exit
status_bar_message_configuration_is_invalid = Konfiguracija je nevažeća: {$error}
status_bar_message_restart_is_needed_for_configuration = Potreban je restart aplikacije za primenu promena u konfiguraciji
status_bar_message_failed_to_save_configuration = Nije uspelo čuvanje promena konfiguracije: {$error}
# TODO: Translate
status_bar_message_restarted_after_crash = Space Acres automatically restarted after crash, check application and system logs for details
status_bar_button_restart = Ponovo pokreni
status_bar_button_ok = U redu
Expand Down
2 changes: 2 additions & 0 deletions res/translations/zh-CN/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ main_menu_exit = Exit
status_bar_message_configuration_is_invalid = 配置不可用: {$error}
status_bar_message_restart_is_needed_for_configuration = 重启以使配置修改生效
status_bar_message_failed_to_save_configuration = 保存配置修改失败: {$error}
# TODO: Translate
status_bar_message_restarted_after_crash = Space Acres automatically restarted after crash, check application and system logs for details
status_bar_button_restart = 重启
status_bar_button_ok = 正常
Expand Down
47 changes: 28 additions & 19 deletions src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl View {
}

#[derive(Debug, Default, Eq, PartialEq)]
enum StatusBarNotification {
enum StatusBarContents {
#[default]
None,
Warning {
Expand All @@ -117,7 +117,7 @@ enum StatusBarNotification {
Error(String),
}

impl StatusBarNotification {
impl StatusBarContents {
fn is_none(&self) -> bool {
matches!(self, Self::None)
}
Expand Down Expand Up @@ -162,6 +162,7 @@ pub struct AppInit {
pub app_data_dir: Option<PathBuf>,
pub exit_status_code: Rc<Cell<AppStatusCode>>,
pub minimize_on_start: bool,
pub crash_notification: bool,
pub run_backend: fn() -> RunBackendResult,
}

Expand All @@ -181,7 +182,7 @@ pub struct App {
#[no_eq]
current_view: View,
current_raw_config: Option<RawConfig>,
status_bar_notification: StatusBarNotification,
status_bar_contents: StatusBarContents,
#[do_not_track]
backend_action_sender: mpsc::Sender<BackendAction>,
#[do_not_track]
Expand Down Expand Up @@ -394,29 +395,29 @@ impl AsyncComponent for App {
gtk::Box {
set_halign: gtk::Align::Center,
set_spacing: 10,
#[track = "model.changed_status_bar_notification()"]
set_visible: !model.status_bar_notification.is_none(),
#[track = "model.changed_status_bar_contents()"]
set_visible: !model.status_bar_contents.is_none(),

gtk::Label {
#[track = "model.changed_status_bar_notification()"]
set_css_classes: &[model.status_bar_notification.css_class()],
#[track = "model.changed_status_bar_notification()"]
set_label: model.status_bar_notification.message(),
#[track = "model.changed_status_bar_contents()"]
set_css_classes: &[model.status_bar_contents.css_class()],
#[track = "model.changed_status_bar_contents()"]
set_label: model.status_bar_contents.message(),
},

gtk::Button {
add_css_class: "suggested-action",
connect_clicked => AppInput::Restart,
set_label: &T.status_bar_button_restart(),
#[track = "model.changed_status_bar_notification()"]
set_visible: model.status_bar_notification.restart_button(),
#[track = "model.changed_status_bar_contents()"]
set_visible: model.status_bar_contents.restart_button(),
},

gtk::Button {
connect_clicked => AppInput::CloseStatusBarWarning,
set_label: &T.status_bar_button_ok(),
#[track = "model.changed_status_bar_notification()"]
set_visible: model.status_bar_notification.ok_button(),
#[track = "model.changed_status_bar_contents()"]
set_visible: model.status_bar_contents.ok_button(),
},
},
},
Expand Down Expand Up @@ -450,6 +451,7 @@ impl AsyncComponent for App {
app_data_dir,
exit_status_code,
minimize_on_start,
crash_notification,
run_backend,
} = init;
let RunBackendResult {
Expand Down Expand Up @@ -572,7 +574,15 @@ impl AsyncComponent for App {
let model = Self {
current_view: View::Loading,
current_raw_config: None,
status_bar_notification: StatusBarNotification::None,
status_bar_contents: if crash_notification {
StatusBarContents::Warning {
message: T.status_bar_message_restarted_after_crash().to_string(),
ok: true,
restart: false,
}
} else {
StatusBarContents::None
},
backend_action_sender,
new_version,
loading_view,
Expand Down Expand Up @@ -743,7 +753,7 @@ impl AsyncComponent for App {
sender.input(AppInput::ShutDown);
}
AppInput::CloseStatusBarWarning => {
self.set_status_bar_notification(StatusBarNotification::None);
self.set_status_bar_contents(StatusBarContents::None);
}
AppInput::HideWindow => {
root.hide();
Expand Down Expand Up @@ -874,7 +884,6 @@ impl App {
match notification {
BackendNotification::Loading(step) => {
self.set_current_view(View::Loading);
self.set_status_bar_notification(StatusBarNotification::None);
self.loading_view.emit(LoadingInput::BackendLoading(step));
}
BackendNotification::ConfigurationFound { raw_config } => {
Expand All @@ -901,7 +910,7 @@ impl App {
reconfiguration: false,
});
}
self.set_status_bar_notification(StatusBarNotification::Warning {
self.set_status_bar_contents(StatusBarContents::Warning {
message: T
.status_bar_message_configuration_is_invalid(error.to_string())
.as_str()
Expand All @@ -912,7 +921,7 @@ impl App {
}
BackendNotification::ConfigSaveResult(result) => match result {
Ok(()) => {
self.set_status_bar_notification(StatusBarNotification::Warning {
self.set_status_bar_contents(StatusBarContents::Warning {
message: T
.status_bar_message_restart_is_needed_for_configuration()
.to_string(),
Expand All @@ -921,7 +930,7 @@ impl App {
});
}
Err(error) => {
self.set_status_bar_notification(StatusBarNotification::Error(
self.set_status_bar_contents(StatusBarContents::Error(
T.status_bar_message_failed_to_save_configuration(error.to_string())
.to_string(),
));
Expand Down
32 changes: 30 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use std::path::{Path, PathBuf};
use std::process::{ExitCode, Termination};
use std::rc::Rc;
use std::thread::available_parallelism;
use std::time::{Duration, Instant};
use std::{env, fs, io, process};
use subspace_farmer::utils::run_future_in_dedicated_thread;
use subspace_proof_of_space::chia::ChiaTable;
Expand All @@ -50,6 +51,7 @@ const LOG_READ_BUFFER: usize = ByteSize::mib(1).as_u64() as usize;
/// If `true`, this means supervisor will not be able to capture logs from child application and logger needs to be in
/// the child process itself, while supervisor will not attempt to read stdout/stderr at all
const WINDOWS_SUBSYSTEM_WINDOWS: bool = cfg!(all(windows, not(debug_assertions)));
const MIN_RUNTIME_DURATION_FOR_AUTORESTART: Duration = Duration::from_secs(30);

type PosTableLegacy = ChiaTableLegacy;
type PosTable = ChiaTable;
Expand Down Expand Up @@ -106,6 +108,9 @@ struct Cli {
/// Used for startup to minimize the window
#[arg(long)]
startup: bool,
/// Used to indicate that application was restarted after crash
#[arg(long)]
after_crash: bool,
/// Used by child process such that supervisor parent process can control it
#[arg(long)]
child_process: bool,
Expand Down Expand Up @@ -285,6 +290,7 @@ impl Cli {
app_data_dir: maybe_app_data_dir,
exit_status_code: Rc::clone(&exit_status_code),
minimize_on_start: self.startup,
crash_notification: self.after_crash,
run_backend: || {
let (backend_action_sender, backend_action_receiver) = mpsc::channel(1);
let (backend_notification_sender, backend_notification_receiver) =
Expand Down Expand Up @@ -327,11 +333,16 @@ impl Cli {
fn supervisor(mut self) -> io::Result<()> {
let maybe_app_data_dir = Self::app_data_dir();

let mut last_start;
let program = Self::child_program()?;

loop {
let mut args = vec!["--child-process".to_string()];
if self.startup {
if self.after_crash {
self.after_crash = false;

args.push("--after-crash".to_string());
} else if self.startup {
// In case of restart we no longer want to minimize the app
self.startup = false;

Expand All @@ -340,6 +351,7 @@ impl Cli {
args.push("--".to_string());
args.extend_from_slice(&self.gtk_arguments);

last_start = Instant::now();
let exit_status = if let Some(app_data_dir) = (!WINDOWS_SUBSYSTEM_WINDOWS)
.then_some(maybe_app_data_dir.as_ref())
.flatten()
Expand Down Expand Up @@ -431,7 +443,23 @@ impl Cli {
}
},
None => {
eprintln!("Application terminated by signal");
#[cfg(unix)]
{
use std::os::unix::process::ExitStatusExt;

eprintln!(
"Application terminated by signal {:?}",
exit_status.signal()
);
}
#[cfg(not(unix))]
{
eprintln!("Application terminated by signal");
}
if last_start.elapsed() >= MIN_RUNTIME_DURATION_FOR_AUTORESTART {
self.after_crash = true;
continue;
}
break;
}
}
Expand Down

0 comments on commit e7da401

Please sign in to comment.