launcher: neuer build loest laufende aeltere instanz automatisch ab
Bisher brachte ein zweiter Start nur das vorhandene Fenster nach vorn und nutzte den laufenden Server weiter - ein frisch gebauter Stand wurde darum nie sichtbar, solange die alte Tray-Instanz lief. Jetzt registriert die serverfuehrende Instanz ihren Build (running.json: pid + payload-hash). Ein Start mit anderem Build setzt ein neues -quit-Event der alten Instanz, wartet auf Freigabe des Mutex und uebernimmt Fenster/Port. Gleicher Build -> wie bisher nur nach vorn holen. Alte Builds ohne das Protokoll -> Fallback auf -show (einmalig manuell umstellen). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -14,17 +14,29 @@ static class Program
|
|||||||
[STAThread]
|
[STAThread]
|
||||||
static void Main(string[] args)
|
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}";
|
var name = $"ProjectDashboard-{MainForm.Port}";
|
||||||
using var mutex = new Mutex(true, name, out var first);
|
using var mutex = new Mutex(true, name, out var first);
|
||||||
if (!first)
|
if (!first)
|
||||||
|
{
|
||||||
|
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 { }
|
try { EventWaitHandle.OpenExisting(name + "-show").Set(); } catch { }
|
||||||
return;
|
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 show = new EventWaitHandle(false, EventResetMode.AutoReset, name + "-show");
|
||||||
|
using var quit = new EventWaitHandle(false, EventResetMode.AutoReset, name + "-quit");
|
||||||
ApplicationConfiguration.Initialize();
|
ApplicationConfiguration.Initialize();
|
||||||
Application.Run(new MainForm(args.Contains("--background"), show));
|
Application.Run(new MainForm(args.Contains("--background"), show, quit));
|
||||||
GC.KeepAlive(mutex);
|
GC.KeepAlive(mutex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -68,11 +80,12 @@ sealed class MainForm : Form
|
|||||||
AppPrefs prefs = AppPrefs.Load();
|
AppPrefs prefs = AppPrefs.Load();
|
||||||
bool startHidden; // --background: the first Show() is skipped
|
bool startHidden; // --background: the first Show() is skipped
|
||||||
bool exiting; // "Beenden" in the tray menu: really close
|
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? lastAutoAt; // last automatic backup result seen (notifications)
|
||||||
string? lastPhase;
|
string? lastPhase;
|
||||||
bool polledOnce;
|
bool polledOnce;
|
||||||
|
|
||||||
public MainForm(bool background = false, EventWaitHandle? showSignal = null)
|
public MainForm(bool background = false, EventWaitHandle? showSignal = null, EventWaitHandle? quitSignal = null)
|
||||||
{
|
{
|
||||||
Text = "Projekte";
|
Text = "Projekte";
|
||||||
Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
|
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();
|
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) =>
|
FormClosing += (_, e) =>
|
||||||
{
|
{
|
||||||
if (!exiting && prefs.Tray && e.CloseReason == CloseReason.UserClosing)
|
if (!exiting && prefs.Tray && e.CloseReason == CloseReason.UserClosing)
|
||||||
@@ -100,7 +118,7 @@ sealed class MainForm : Form
|
|||||||
}
|
}
|
||||||
return;
|
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?",
|
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)
|
"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?).");
|
if (DateTime.Now > until) throw new Exception($"Server antwortet nicht (Port {Port} belegt?).");
|
||||||
await Task.Delay(300);
|
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 { }
|
try { if (server is { HasExited: false }) server.Kill(entireProcessTree: false); } catch { }
|
||||||
serverLog?.Dispose();
|
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)
|
static void OpenExternal(string uri)
|
||||||
|
|||||||
Reference in New Issue
Block a user