diff --git a/launcher/Program.cs b/launcher/Program.cs index eb81c2e..f141d37 100644 --- a/launcher/Program.cs +++ b/launcher/Program.cs @@ -14,17 +14,29 @@ static class Program [STAThread] static void Main(string[] args) { - // one dashboard per port: a second start brings the running one to the front + // one dashboard per port. A second start of the SAME build just brings the running window to the + // front. A start of a NEWER build asks the running (older) instance to quit and then takes over, + // so testing a fresh build never shows the old one (as long as both builds speak this protocol). var name = $"ProjectDashboard-{MainForm.Port}"; using var mutex = new Mutex(true, name, out var first); if (!first) { - try { EventWaitHandle.OpenExisting(name + "-show").Set(); } catch { } - return; + var myBuild = MainForm.PayloadHash(); + var (pid, runBuild) = MainForm.ReadRunning(); + bool takeover = runBuild != null && runBuild != myBuild && MainForm.PidAlive(pid); + // signal the old instance to quit; an old build without the quit event -> fall back to "show" + if (takeover) { try { EventWaitHandle.OpenExisting(name + "-quit").Set(); } catch { takeover = false; } } + if (!takeover || !mutex.WaitOne(TimeSpan.FromSeconds(15))) + { + try { EventWaitHandle.OpenExisting(name + "-show").Set(); } catch { } + return; + } + // the old instance has closed and released the mutex; we own it now and run as the primary } using var show = new EventWaitHandle(false, EventResetMode.AutoReset, name + "-show"); + using var quit = new EventWaitHandle(false, EventResetMode.AutoReset, name + "-quit"); ApplicationConfiguration.Initialize(); - Application.Run(new MainForm(args.Contains("--background"), show)); + Application.Run(new MainForm(args.Contains("--background"), show, quit)); GC.KeepAlive(mutex); } } @@ -68,11 +80,12 @@ sealed class MainForm : Form AppPrefs prefs = AppPrefs.Load(); bool startHidden; // --background: the first Show() is skipped bool exiting; // "Beenden" in the tray menu: really close + bool takingOver; // a newer build asked us to quit: close without the backup prompt string? lastAutoAt; // last automatic backup result seen (notifications) string? lastPhase; bool polledOnce; - public MainForm(bool background = false, EventWaitHandle? showSignal = null) + public MainForm(bool background = false, EventWaitHandle? showSignal = null, EventWaitHandle? quitSignal = null) { Text = "Projekte"; Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath); @@ -84,6 +97,11 @@ sealed class MainForm : Form { new Thread(() => { while (showSignal.WaitOne()) { try { BeginInvoke(() => ShowFromTray()); } catch { } } }) { IsBackground = true }.Start(); } + // a newer build asked us to step aside: close for real (skip the backup prompt, release the port) + if (quitSignal != null) + { + new Thread(() => { while (quitSignal.WaitOne()) { try { BeginInvoke(() => { exiting = true; takingOver = true; Close(); }); } catch { } } }) { IsBackground = true }.Start(); + } FormClosing += (_, e) => { if (!exiting && prefs.Tray && e.CloseReason == CloseReason.UserClosing) @@ -100,7 +118,7 @@ sealed class MainForm : Form } return; } - if (server != null && BackupRunning() && + if (!takingOver && server != null && BackupRunning() && MessageBox.Show(this, "Das Backup läuft noch.\n\nBeim Schließen wird es abgebrochen (bereits kopierte Dateien bleiben). Trotzdem schließen?", "Projekte", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) { @@ -234,6 +252,7 @@ sealed class MainForm : Form if (DateTime.Now > until) throw new Exception($"Server antwortet nicht (Port {Port} belegt?)."); await Task.Delay(300); } + WriteRunning(); // we own the server: register our build so a newer build can take over } } @@ -526,6 +545,57 @@ sealed class MainForm : Form } try { if (server is { HasExited: false }) server.Kill(entireProcessTree: false); } catch { } serverLog?.Dispose(); + if (server != null) ClearRunning(); // only the owner of the server registered itself + } + + // ── instance registry: lets a newer build detect and replace a running older one ── + static readonly string RunningFile = Path.Combine(DataDir, "running.json"); + + // Our own build identity: the payload hash (portable), or "source" for the dev build next to server.js. + internal static string PayloadHash() + { + if (File.Exists(Path.Combine(AppContext.BaseDirectory, "server.js"))) return "source"; + try + { + using var res = Assembly.GetExecutingAssembly().GetManifestResourceStream("payload.zip"); + if (res == null) return "unknown"; + using var sha = SHA256.Create(); + return Convert.ToHexString(sha.ComputeHash(res))[..12].ToLowerInvariant(); + } + catch { return "unknown"; } + } + + // Build id of the instance currently serving (portable: its app-folder hash; dev: "source"). + void WriteRunning() + { + try + { + Directory.CreateDirectory(DataDir); + var build = portable ? Path.GetFileName(appDir) : "source"; + File.WriteAllText(RunningFile, JsonSerializer.Serialize(new { pid = Environment.ProcessId, build })); + } + catch { } + } + + static void ClearRunning() { try { File.Delete(RunningFile); } catch { } } + + internal static (int pid, string? build) ReadRunning() + { + try + { + using var doc = JsonDocument.Parse(File.ReadAllText(RunningFile)); + var r = doc.RootElement; + var pid = r.TryGetProperty("pid", out var p) && p.TryGetInt32(out var n) ? n : 0; + var build = r.TryGetProperty("build", out var b) ? b.GetString() : null; + return (pid, build); + } + catch { return (0, null); } + } + + internal static bool PidAlive(int pid) + { + if (pid <= 0) return false; + try { using var _ = Process.GetProcessById(pid); return true; } catch { return false; } } static void OpenExternal(string uri)