mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Fixed slow build startup time and implemented loading screen
This commit is contained in:
1 parent
a0df683fee
commit
d474c662db
18 files changed
+790
-148
No files matched your search
+190
-77
@@ -1,22 +1,38 @@
|
||||
// Konduktor desktop shell (Tauri v2).
|
||||
//
|
||||
// The backend is the frozen Python sidecar (see backend/sidecar.py): on launch
|
||||
// we spawn it, read the `KONDUKTOR_PORT=<n>` line it prints on stdout, wait for
|
||||
// it to actually accept connections, then create the app window with an
|
||||
// initialization script that hands the frontend its API base
|
||||
// (`window.__KONDUKTOR_API__`). The sidecar is killed when the app exits.
|
||||
// The backend is the frozen Python sidecar (see backend/sidecar.py), a
|
||||
// PyInstaller ONEDIR folder bundled as the `sidecar/` resource — not an
|
||||
// `externalBin`, which must be one file. A one-file build unpacked ~390 native
|
||||
// libraries into a new temp folder on every launch, and macOS scans each
|
||||
// library it has not seen before as it loads, so the window took ~10 s to
|
||||
// appear; installed as a folder they are scanned once.
|
||||
//
|
||||
// The window opens AT ONCE and shows the startup screen (StartupScreen.tsx)
|
||||
// while the backend starts: a first launch after an install or update still
|
||||
// waits for that one-time scan, and a window that appears seconds late looks
|
||||
// like an app that failed to open. Meanwhile we spawn the sidecar, read the
|
||||
// `KONDUKTOR_PORT=<n>` line it prints on stdout and wait for the socket to
|
||||
// accept connections. The page asks `backend_status` and listens for
|
||||
// `konduktor://backend` (starting / ready + url / stopped + the output's last
|
||||
// lines); `backend_restart` is its Try again. There is no timeout here — a
|
||||
// fresh install can take a minute — so the page decides when to ask the user.
|
||||
// The sidecar is killed when the app exits.
|
||||
//
|
||||
// Quitting ASKS FIRST. Closing the window or Cmd/Ctrl+Q is prevented and
|
||||
// becomes a `konduktor://quit-requested` event; the frontend (QuitGuard.tsx)
|
||||
// acknowledges it, checks for unsaved changes and either quits (`quit_now`) or
|
||||
// asks Save / Discard / Cancel. The sidecar must outlive that question — a
|
||||
// Save or Discard runs in it — so it is killed only once the app really exits.
|
||||
// If the webview does not acknowledge within ~2 s (hung, or not loaded yet),
|
||||
// the app quits anyway: a broken page must never make the app unquittable.
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
// becomes a `konduktor://quit-requested` event; the frontend (QuitGuard.tsx,
|
||||
// or the startup screen before the app has loaded) acknowledges it, checks for
|
||||
// unsaved changes and either quits (`quit_now`) or asks Save / Discard /
|
||||
// Cancel. The sidecar must outlive that question — a Save or Discard runs in
|
||||
// it — so it is killed only once the app really exits. If the webview does not
|
||||
// acknowledge within ~2 s (hung, or not loaded yet), the app quits anyway: a
|
||||
// broken page must never make the app unquittable.
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||
use std::sync::Mutex;
|
||||
use std::time::Duration;
|
||||
|
||||
use serde::Serialize;
|
||||
use tauri::window::Color;
|
||||
use tauri::{AppHandle, Emitter, Manager, RunEvent, WebviewUrl, WebviewWindowBuilder, WindowEvent};
|
||||
use tauri_plugin_shell::process::{CommandChild, CommandEvent};
|
||||
use tauri_plugin_shell::ShellExt;
|
||||
@@ -24,6 +40,26 @@ use tauri_plugin_shell::ShellExt;
|
||||
/// Holds the running sidecar child so we can terminate it on exit.
|
||||
struct Sidecar(Mutex<Option<CommandChild>>);
|
||||
|
||||
/// How many of the sidecar's last output lines a "couldn't start" report keeps.
|
||||
const LOG_LINES: usize = 30;
|
||||
|
||||
/// What the startup screen shows. Serialised as `{"state": "starting"}`,
|
||||
/// `{"state": "ready", "url": …}` or `{"state": "stopped", "code", "signal", "log"}`.
|
||||
#[derive(Clone, Serialize)]
|
||||
#[serde(tag = "state", rename_all = "snake_case")]
|
||||
enum BackendStatus {
|
||||
Starting,
|
||||
Ready { url: String },
|
||||
Stopped { code: Option<i32>, signal: Option<i32>, log: Vec<String> },
|
||||
}
|
||||
|
||||
/// The backend's current status, plus a generation counter: each spawn bumps
|
||||
/// it, so events from a sidecar that was replaced (Try again) are ignored.
|
||||
struct Backend {
|
||||
status: Mutex<BackendStatus>,
|
||||
generation: AtomicU64,
|
||||
}
|
||||
|
||||
/// Quit handshake state: `allowed` once the frontend (or the timeout) has said
|
||||
/// quit; `acked` once the frontend has seen the current request.
|
||||
struct Quit {
|
||||
@@ -59,6 +95,125 @@ fn request_quit(app: &AppHandle) {
|
||||
});
|
||||
}
|
||||
|
||||
/// Record a new status for generation `gen` (ignored if a newer sidecar has
|
||||
/// been spawned since) and tell the page.
|
||||
fn set_status(app: &AppHandle, gen: u64, status: BackendStatus) {
|
||||
let backend = app.state::<Backend>();
|
||||
if backend.generation.load(Ordering::SeqCst) != gen {
|
||||
return;
|
||||
}
|
||||
*backend.status.lock().unwrap() = status.clone();
|
||||
let _ = app.emit("konduktor://backend", status);
|
||||
}
|
||||
|
||||
fn push_lines(log: &mut VecDeque<String>, bytes: &[u8]) {
|
||||
for line in String::from_utf8_lossy(bytes).lines() {
|
||||
if log.len() == LOG_LINES {
|
||||
log.pop_front();
|
||||
}
|
||||
log.push_back(line.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
/// Spawn the sidecar and follow it until it is serving (ready) or exits (stopped).
|
||||
fn start_backend(app: &AppHandle) {
|
||||
kill_sidecar(app);
|
||||
let gen = app.state::<Backend>().generation.fetch_add(1, Ordering::SeqCst) + 1;
|
||||
set_status(app, gen, BackendStatus::Starting);
|
||||
|
||||
// The frozen backend lives in the bundled `sidecar/` folder (staged into
|
||||
// src-tauri/binaries/ by backend/build_sidecar.*).
|
||||
let exe = if cfg!(windows) { "konduktor-sidecar.exe" } else { "konduktor-sidecar" };
|
||||
let spawned = app
|
||||
.path()
|
||||
.resource_dir()
|
||||
.map_err(|e| e.to_string())
|
||||
.and_then(|dir| {
|
||||
let program = dir.join("sidecar").join(exe);
|
||||
app.shell()
|
||||
.command(&program)
|
||||
.spawn()
|
||||
.map_err(|e| format!("{}: {e}", program.display()))
|
||||
});
|
||||
let (mut rx, child) = match spawned {
|
||||
Ok(pair) => pair,
|
||||
Err(e) => {
|
||||
let log = vec![format!("Could not start the library service: {e}")];
|
||||
set_status(app, gen, BackendStatus::Stopped { code: None, signal: None, log });
|
||||
return;
|
||||
}
|
||||
};
|
||||
app.state::<Sidecar>().0.lock().unwrap().replace(child);
|
||||
|
||||
let handle = app.clone();
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let mut log = VecDeque::with_capacity(LOG_LINES);
|
||||
let mut announced = false;
|
||||
// Keep draining to the end, so the sidecar's pipes never fill and block it.
|
||||
while let Some(event) = rx.recv().await {
|
||||
match event {
|
||||
CommandEvent::Stdout(bytes) => {
|
||||
if !announced {
|
||||
let text = String::from_utf8_lossy(&bytes);
|
||||
let port = text.lines().find_map(|line| {
|
||||
line.trim().strip_prefix("KONDUKTOR_PORT=")?.trim().parse::<u16>().ok()
|
||||
});
|
||||
if let Some(port) = port {
|
||||
announced = true;
|
||||
wait_until_serving(handle.clone(), gen, port);
|
||||
}
|
||||
}
|
||||
push_lines(&mut log, &bytes);
|
||||
}
|
||||
CommandEvent::Stderr(bytes) => push_lines(&mut log, &bytes),
|
||||
CommandEvent::Error(e) => push_lines(&mut log, e.as_bytes()),
|
||||
CommandEvent::Terminated(payload) => {
|
||||
let status = BackendStatus::Stopped {
|
||||
code: payload.code,
|
||||
signal: payload.signal,
|
||||
log: log.iter().cloned().collect(),
|
||||
};
|
||||
set_status(&handle, gen, status);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// The port line is printed before uvicorn binds: report ready once the socket
|
||||
/// actually accepts connections (or give up quietly if this sidecar is gone).
|
||||
fn wait_until_serving(app: AppHandle, gen: u64, port: u16) {
|
||||
tauri::async_runtime::spawn(async move {
|
||||
loop {
|
||||
let backend = app.state::<Backend>();
|
||||
let current = backend.generation.load(Ordering::SeqCst) == gen
|
||||
&& matches!(*backend.status.lock().unwrap(), BackendStatus::Starting);
|
||||
if !current {
|
||||
return;
|
||||
}
|
||||
if std::net::TcpStream::connect(("127.0.0.1", port)).is_ok() {
|
||||
let url = format!("http://127.0.0.1:{port}");
|
||||
set_status(&app, gen, BackendStatus::Ready { url });
|
||||
return;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(50)).await;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Where the backend is: the startup screen asks once on load, then listens.
|
||||
#[tauri::command]
|
||||
fn backend_status(app: AppHandle) -> BackendStatus {
|
||||
app.state::<Backend>().status.lock().unwrap().clone()
|
||||
}
|
||||
|
||||
/// The startup screen's Try again.
|
||||
#[tauri::command]
|
||||
fn backend_restart(app: AppHandle) {
|
||||
start_backend(&app);
|
||||
}
|
||||
|
||||
/// The frontend has seen the quit request (it may now take as long as it likes).
|
||||
#[tauri::command]
|
||||
fn quit_ack(app: AppHandle) {
|
||||
@@ -82,8 +237,15 @@ pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.manage(Sidecar(Mutex::new(None)))
|
||||
.manage(Backend { status: Mutex::new(BackendStatus::Starting), generation: AtomicU64::new(0) })
|
||||
.manage(Quit { allowed: AtomicBool::new(false), acked: AtomicBool::new(false) })
|
||||
.invoke_handler(tauri::generate_handler![quit_ack, quit_cancel, quit_now])
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
backend_status,
|
||||
backend_restart,
|
||||
quit_ack,
|
||||
quit_cancel,
|
||||
quit_now
|
||||
])
|
||||
.on_window_event(|window, event| {
|
||||
if let WindowEvent::CloseRequested { api, .. } = event {
|
||||
let app = window.app_handle();
|
||||
@@ -102,71 +264,22 @@ pub fn run() {
|
||||
)?;
|
||||
}
|
||||
|
||||
let handle = app.handle().clone();
|
||||
start_backend(app.handle());
|
||||
|
||||
// Spawn the frozen backend. In dev/bundle the shell plugin resolves
|
||||
// the sidecar from `binaries/konduktor-sidecar-<target-triple>`.
|
||||
let (mut rx, child) = app
|
||||
.shell()
|
||||
.sidecar("konduktor-sidecar")
|
||||
.expect("sidecar binary not found (build it into src-tauri/binaries/)")
|
||||
.spawn()
|
||||
.expect("failed to spawn backend sidecar");
|
||||
app.state::<Sidecar>().0.lock().unwrap().replace(child);
|
||||
|
||||
tauri::async_runtime::block_on(async move {
|
||||
// Read stdout until the backend announces its port (bounded so a
|
||||
// misbehaving sidecar can't hang the launch forever).
|
||||
let find_port = async {
|
||||
while let Some(event) = rx.recv().await {
|
||||
if let CommandEvent::Stdout(bytes) = event {
|
||||
let text = String::from_utf8_lossy(&bytes);
|
||||
for line in text.lines() {
|
||||
if let Some(p) = line.trim().strip_prefix("KONDUKTOR_PORT=") {
|
||||
if let Ok(n) = p.trim().parse::<u16>() {
|
||||
return Some(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
};
|
||||
let port = tokio::time::timeout(Duration::from_secs(20), find_port)
|
||||
.await
|
||||
.ok()
|
||||
.flatten()
|
||||
.expect("backend sidecar did not report a port");
|
||||
|
||||
// Keep draining events so the sidecar's stdout/stderr pipes never
|
||||
// fill and block it; the task ends when the child exits.
|
||||
tauri::async_runtime::spawn(async move { while rx.recv().await.is_some() {} });
|
||||
|
||||
// Wait for the socket to actually accept connections (the port
|
||||
// line is printed before uvicorn binds).
|
||||
for _ in 0..200 {
|
||||
if std::net::TcpStream::connect(("127.0.0.1", port)).is_ok() {
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
|
||||
let inject =
|
||||
format!("window.__KONDUKTOR_API__ = 'http://127.0.0.1:{port}';");
|
||||
WebviewWindowBuilder::new(&handle, "main", WebviewUrl::App("index.html".into()))
|
||||
.title("Konduktor")
|
||||
.inner_size(1400.0, 900.0)
|
||||
.min_inner_size(900.0, 600.0)
|
||||
// On Windows the webview would otherwise be served from
|
||||
// https://tauri.localhost, and fetching the sidecar over
|
||||
// http://127.0.0.1 would be blocked as mixed content. Force
|
||||
// the http custom-protocol scheme so both are http. (No-op on
|
||||
// macOS, which uses the tauri:// scheme.)
|
||||
.use_https_scheme(false)
|
||||
.initialization_script(&inject)
|
||||
.build()
|
||||
.expect("failed to create the main window");
|
||||
});
|
||||
WebviewWindowBuilder::new(app, "main", WebviewUrl::App("index.html".into()))
|
||||
.title("Konduktor")
|
||||
.inner_size(1400.0, 900.0)
|
||||
.min_inner_size(900.0, 600.0)
|
||||
// The page's own base colour (ink-950), so the first frame —
|
||||
// before any CSS has loaded — is dark rather than white.
|
||||
.background_color(Color(5, 6, 10, 255))
|
||||
// On Windows the webview would otherwise be served from
|
||||
// https://tauri.localhost, and fetching the sidecar over
|
||||
// http://127.0.0.1 would be blocked as mixed content. Force
|
||||
// the http custom-protocol scheme so both are http. (No-op on
|
||||
// macOS, which uses the tauri:// scheme.)
|
||||
.use_https_scheme(false)
|
||||
.build()?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user