fix(gui): a failed start can be tried again, and a close that was not whole stays on screen
The window retries a failed server start when the next tab opens or when the person asks, says a server still starting is starting, tells the person which wanted companies did not open, and when the close reports an incomplete last backup or a bad exit it stays up with a Close anyway button. ViewerOwner only forwards to AppHost now, and the format upgrade goes to the window's own server. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
acc17a7703
commit
9f3f292bf5
5 files changed
+417
-80
No files matched your search
@@ -65,6 +65,11 @@
|
||||
//! closing the books, until the server has exited or its bounded
|
||||
//! close has run out (and then says what is left); a close is never
|
||||
//! begun while a job that changes the books runs (7) (WB-app-4).
|
||||
//! When the close says something was not whole (a company's last
|
||||
//! backup, a server that exited badly), the window stays up showing
|
||||
//! it, with a "Close anyway" button; closing again is not that.
|
||||
//! A server still starting is said under the tab bar, and one whose
|
||||
//! start failed has a "Try again" button there (WB-app-2).
|
||||
//! ```
|
||||
|
||||
use crate::company::CompanyView;
|
||||
@@ -429,7 +434,10 @@ impl App {
|
||||
return;
|
||||
}
|
||||
let asked = ctx.input(|i| i.viewport().close_requested());
|
||||
if asked && !self.serving.is_closed() {
|
||||
// What the close said was not whole is read before the window goes
|
||||
// (INVARIANT 9): asking again is not reading it.
|
||||
let report = self.serving.close_report();
|
||||
if asked && (!self.serving.is_closed() || !report.is_empty()) {
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose);
|
||||
self.serving.begin_stop();
|
||||
self.closing_window = true;
|
||||
@@ -438,8 +446,30 @@ impl App {
|
||||
return;
|
||||
}
|
||||
if self.serving.is_closed() {
|
||||
self.closing_window = false;
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
||||
if report.is_empty() {
|
||||
self.closing_window = false;
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
||||
return;
|
||||
}
|
||||
let mut close_anyway = false;
|
||||
egui::Modal::new(egui::Id::new("wanderbooks-closed-with-problems")).show(ctx, |ui| {
|
||||
ui.set_max_width(520.0);
|
||||
ui.heading("Your books did not close cleanly");
|
||||
ui.add_space(4.0);
|
||||
for line in &report {
|
||||
ui.label(line);
|
||||
ui.add_space(2.0);
|
||||
}
|
||||
ui.add_space(6.0);
|
||||
if ui.button("Close anyway").clicked() {
|
||||
close_anyway = true;
|
||||
}
|
||||
});
|
||||
if close_anyway {
|
||||
self.serving.acknowledge_close();
|
||||
self.closing_window = false;
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
||||
}
|
||||
return;
|
||||
}
|
||||
egui::Modal::new(egui::Id::new("wanderbooks-closing-books")).show(ctx, |ui| {
|
||||
@@ -460,11 +490,14 @@ impl App {
|
||||
/// dismissed (INVARIANT 8).
|
||||
fn show_notices(&mut self, ui: &mut Ui) {
|
||||
self.notices.extend(self.serving.take_notices());
|
||||
if self.notices.is_empty() {
|
||||
let starting = self.serving.is_starting();
|
||||
let down = self.serving.is_down();
|
||||
if self.notices.is_empty() && !starting && !down {
|
||||
return;
|
||||
}
|
||||
let p = pal(ui.ctx());
|
||||
let mut dismiss = None;
|
||||
let mut retry = false;
|
||||
egui::Panel::top("server_notices")
|
||||
.resizable(false)
|
||||
.frame(
|
||||
@@ -473,18 +506,42 @@ impl App {
|
||||
.inner_margin(egui::Margin::symmetric(12, 6)),
|
||||
)
|
||||
.show(ui, |ui| {
|
||||
if starting {
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
ui.spinner();
|
||||
ui.label(
|
||||
RichText::new(
|
||||
"Starting the books server for your assistants: another program may still be finishing with the books.",
|
||||
)
|
||||
.color(p.text),
|
||||
);
|
||||
});
|
||||
}
|
||||
for (i, words) in self.notices.iter().enumerate() {
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
ui.label(RichText::new(words).color(p.text));
|
||||
if down && ui.button("Try again").clicked() {
|
||||
retry = true;
|
||||
}
|
||||
if ui.button("Dismiss").clicked() {
|
||||
dismiss = Some(i);
|
||||
}
|
||||
});
|
||||
}
|
||||
if down && self.notices.is_empty() && ui.button("Try again").clicked() {
|
||||
retry = true;
|
||||
}
|
||||
});
|
||||
if retry {
|
||||
self.serving.retry();
|
||||
}
|
||||
if let Some(i) = dismiss {
|
||||
self.notices.remove(i);
|
||||
}
|
||||
if starting {
|
||||
ui.ctx()
|
||||
.request_repaint_after(std::time::Duration::from_millis(500));
|
||||
}
|
||||
}
|
||||
|
||||
/// What the server work has said that has not been dismissed (tests).
|
||||
|
||||
@@ -180,9 +180,17 @@ pub trait Owner: Send + Sync {
|
||||
fn serve_close(&self, _book: &str) -> Result<Option<String>, String> {
|
||||
Ok(None)
|
||||
}
|
||||
/// Close the server (the window closed): its input is closed and the
|
||||
/// exit waited for, for a bounded time. `Ok(Some(words))` when it is
|
||||
/// still finishing by itself after that. Blocks.
|
||||
/// Of the companies `wanted` (asked to be open at the start), those
|
||||
/// the server does not have open now, each with why in words: asked
|
||||
/// once the server is ready. Blocks.
|
||||
fn serve_not_open(&self, _wanted: &[String]) -> Vec<(String, String)> {
|
||||
Vec::new()
|
||||
}
|
||||
/// Close the server (the window closed): each open company is closed
|
||||
/// first, then the server's input, and the exit is waited for, for a
|
||||
/// bounded time. `Ok(Some(words))` says what was not whole, a line
|
||||
/// each: a company whose last backup was incomplete, a server that
|
||||
/// exited badly, one still finishing by itself after the wait. Blocks.
|
||||
fn serve_stop(&self) -> Result<Option<String>, String> {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
@@ -19,17 +19,25 @@
|
||||
//! company's request waits for another company's, so closing one
|
||||
//! company's tab, which makes its last backup, delays no other.
|
||||
//! 3. No request is made before the server has started, and none after
|
||||
//! a start that failed (the person was told why, once).
|
||||
//! a start that failed (the person was told why, once). A start that
|
||||
//! failed is tried again, with the companies wanted then, when the
|
||||
//! next tab opens or the person asks (`retry`); the owner has
|
||||
//! stopped the server that failed, so no second one runs beside it.
|
||||
//! A server still starting is waited for, in view (`is_starting`).
|
||||
//! 4. A company the window opened at the start is not opened again; a
|
||||
//! company whose open failed is asked for again the next time its
|
||||
//! tab opens.
|
||||
//! company whose open failed, at the start (the server's list says
|
||||
//! it isn't open: `serve_not_open`) or later, is a notice, and is
|
||||
//! asked for again the next time its tab opens.
|
||||
//! 5. A failed request, a close whose last backup wasn't whole, and a
|
||||
//! server that has exited on its own are each a notice, in words,
|
||||
//! once; the window shows them until they are dismissed.
|
||||
//! 6. The server is closed once: by `begin_stop` (the window asked to
|
||||
//! close, with the wait in view), else when this is dropped. Neither
|
||||
//! returns before the owner's bounded close has, and neither kills
|
||||
//! anything.
|
||||
//! anything. What the close says was not whole (a company's last
|
||||
//! backup, a server that exited badly) is the close's report: the
|
||||
//! window stays up showing it until the person closes anyway
|
||||
//! (`close_report`, `acknowledge_close`).
|
||||
//! 7. Without an owner that hosts a server (the viewer on its own, the
|
||||
//! demo, tests) it does nothing at all.
|
||||
//! ```
|
||||
@@ -70,6 +78,10 @@ struct State {
|
||||
lanes: HashMap<String, Sender<Cmd>>,
|
||||
/// The server's exit has been said.
|
||||
exit_said: bool,
|
||||
/// What the server's close said was not whole, a line each.
|
||||
report: Vec<String>,
|
||||
/// The person has seen the report and chose to close anyway.
|
||||
report_seen: bool,
|
||||
}
|
||||
|
||||
struct Inner {
|
||||
@@ -117,17 +129,18 @@ impl Inner {
|
||||
}
|
||||
*closing = Closing::Running;
|
||||
}
|
||||
match self.owner.serve_stop() {
|
||||
Ok(None) => {}
|
||||
Ok(Some(words)) => {
|
||||
// The window may be gone before anyone reads a notice.
|
||||
eprintln!("wanderbooks: {words}");
|
||||
self.note(words);
|
||||
}
|
||||
Err(why) => self.note(format!(
|
||||
let said: Vec<String> = match self.owner.serve_stop() {
|
||||
Ok(None) => Vec::new(),
|
||||
Ok(Some(words)) => words.lines().map(str::to_string).collect(),
|
||||
Err(why) => vec![format!(
|
||||
"The books server could not be closed cleanly: {why}"
|
||||
)),
|
||||
)],
|
||||
};
|
||||
for line in &said {
|
||||
// The window may be gone before anyone reads the report.
|
||||
eprintln!("wanderbooks: {line}");
|
||||
}
|
||||
lock(&self.state).report = said;
|
||||
*lock(&self.closing) = Closing::Done;
|
||||
self.closing_changed.notify_all();
|
||||
}
|
||||
@@ -161,21 +174,7 @@ impl Serving {
|
||||
..State::default()
|
||||
}),
|
||||
});
|
||||
let starting = inner.clone();
|
||||
let spawned = std::thread::Builder::new()
|
||||
.name("serving-start".into())
|
||||
.spawn(move || {
|
||||
let came = match starting.owner.serve_start(&open) {
|
||||
Ok(()) => Gate::Up,
|
||||
Err(why) => {
|
||||
starting.note(format!("Assistants can't reach your companies: {why}"));
|
||||
Gate::Down
|
||||
}
|
||||
};
|
||||
*lock(&starting.gate) = came;
|
||||
starting.gate_changed.notify_all();
|
||||
});
|
||||
if spawned.is_err() {
|
||||
if !Self::spawn_start(&inner) {
|
||||
inner.note(
|
||||
"Assistants can't reach your companies: the books server could not be started."
|
||||
.into(),
|
||||
@@ -185,6 +184,86 @@ impl Serving {
|
||||
Self { inner: Some(inner) }
|
||||
}
|
||||
|
||||
/// Start the server, on a thread of its own, with the companies
|
||||
/// wanted now (INVARIANTS 1, 3, 4): false when no thread could be made.
|
||||
fn spawn_start(inner: &Arc<Inner>) -> bool {
|
||||
let open: Vec<String> = lock(&inner.state).wanted.iter().cloned().collect();
|
||||
let starting = inner.clone();
|
||||
std::thread::Builder::new()
|
||||
.name("serving-start".into())
|
||||
.spawn(move || {
|
||||
let came = match starting.owner.serve_start(&open) {
|
||||
Ok(()) => {
|
||||
// The server says ready once each company was
|
||||
// tried: the ones that did not open are said, and
|
||||
// asked for again with their tab (INVARIANT 4).
|
||||
for (id, words) in starting.owner.serve_not_open(&open) {
|
||||
lock(&starting.state).wanted.remove(&id);
|
||||
starting.note(words);
|
||||
}
|
||||
Gate::Up
|
||||
}
|
||||
Err(why) => {
|
||||
// A window asked to close meanwhile has no use for
|
||||
// the word.
|
||||
if *lock(&starting.closing) == Closing::Not {
|
||||
starting.note(format!("Assistants can't reach your companies: {why}"));
|
||||
}
|
||||
Gate::Down
|
||||
}
|
||||
};
|
||||
*lock(&starting.gate) = came;
|
||||
starting.gate_changed.notify_all();
|
||||
})
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
/// Try a start that failed again, with the companies wanted now
|
||||
/// (INVARIANT 3): true when one has begun.
|
||||
pub fn retry(&self) -> bool {
|
||||
let Some(inner) = &self.inner else {
|
||||
return false;
|
||||
};
|
||||
Self::restart(inner)
|
||||
}
|
||||
|
||||
fn restart(inner: &Arc<Inner>) -> bool {
|
||||
if *lock(&inner.closing) != Closing::Not {
|
||||
return false;
|
||||
}
|
||||
{
|
||||
let mut gate = lock(&inner.gate);
|
||||
if *gate != Gate::Down {
|
||||
return false;
|
||||
}
|
||||
*gate = Gate::Starting;
|
||||
}
|
||||
if Self::spawn_start(inner) {
|
||||
return true;
|
||||
}
|
||||
inner.note(
|
||||
"Assistants can't reach your companies: the books server could not be started.".into(),
|
||||
);
|
||||
*lock(&inner.gate) = Gate::Down;
|
||||
false
|
||||
}
|
||||
|
||||
/// Whether the server is still starting (INVARIANT 3): its tabs wait
|
||||
/// for it, and the window says so.
|
||||
pub fn is_starting(&self) -> bool {
|
||||
self.inner
|
||||
.as_ref()
|
||||
.is_some_and(|i| *lock(&i.gate) == Gate::Starting)
|
||||
}
|
||||
|
||||
/// Whether the start failed and a `retry` would try again.
|
||||
pub fn is_down(&self) -> bool {
|
||||
self.inner
|
||||
.as_ref()
|
||||
.is_some_and(|i| *lock(&i.gate) == Gate::Down)
|
||||
&& !self.is_closing()
|
||||
}
|
||||
|
||||
/// Whether a server is being hosted for this window.
|
||||
pub fn is_on(&self) -> bool {
|
||||
self.inner.is_some()
|
||||
@@ -195,7 +274,14 @@ impl Serving {
|
||||
pub fn open(&self, id: &str) {
|
||||
let Some(inner) = &self.inner else { return };
|
||||
let fresh = lock(&inner.state).wanted.insert(id.to_string());
|
||||
// After a failed start the next tab is the next try (INVARIANT 3):
|
||||
// the start opens every company wanted, this one included.
|
||||
// A company already wanted is the window's own start-up opening
|
||||
// the tabs it restored, which the start has in its list.
|
||||
if fresh {
|
||||
if Self::restart(inner) {
|
||||
return;
|
||||
}
|
||||
Self::send(inner, id, Cmd::Open);
|
||||
}
|
||||
}
|
||||
@@ -293,6 +379,31 @@ impl Serving {
|
||||
std::mem::take(&mut state.notices)
|
||||
}
|
||||
|
||||
/// What the server's close said was not whole, a line each, until the
|
||||
/// person has closed anyway (INVARIANT 6); nothing before the close
|
||||
/// is done, or when it was whole.
|
||||
pub fn close_report(&self) -> Vec<String> {
|
||||
let Some(inner) = &self.inner else {
|
||||
return Vec::new();
|
||||
};
|
||||
if *lock(&inner.closing) != Closing::Done {
|
||||
return Vec::new();
|
||||
}
|
||||
let state = lock(&inner.state);
|
||||
if state.report_seen {
|
||||
Vec::new()
|
||||
} else {
|
||||
state.report.clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// The person has read the close's report and closes anyway.
|
||||
pub fn acknowledge_close(&self) {
|
||||
if let Some(inner) = &self.inner {
|
||||
lock(&inner.state).report_seen = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// The window was asked to close: close the server on a thread of its
|
||||
/// own, so the window can say so while it does (INVARIANT 6).
|
||||
pub fn begin_stop(&self) {
|
||||
|
||||
Reference in new issue
Block a user