fix reoccurring entry dll path in args when application restarts
This commit is contained in:
39
Torch.Server/ApplicationRestartHelper.cs
Normal file
39
Torch.Server/ApplicationRestartHelper.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace Torch.Server;
|
||||
|
||||
internal static class ApplicationRestartHelper
|
||||
{
|
||||
public static void RestartApplication()
|
||||
{
|
||||
var exe = Path.Combine(AppContext.BaseDirectory, "Torch.Server.exe");
|
||||
|
||||
var args = Environment.GetCommandLineArgs().ToList();
|
||||
|
||||
args.RemoveAt(0); // dll entry path
|
||||
|
||||
args.RemoveConfigItem("--tempAutostart");
|
||||
args.RemoveConfigItem("--waitForPid");
|
||||
|
||||
args.AddRange([
|
||||
"--waitForPid", Environment.ProcessId.ToString(),
|
||||
"--tempAutostart", "true"
|
||||
]);
|
||||
|
||||
Process.Start(new ProcessStartInfo(exe, args));
|
||||
}
|
||||
|
||||
private static void RemoveConfigItem(this List<string> list, string item)
|
||||
{
|
||||
var index = list.BinarySearch(item, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
if (index < 0) return;
|
||||
|
||||
list.RemoveAt(index);
|
||||
list.RemoveAt(index);
|
||||
}
|
||||
}
|
@@ -269,24 +269,7 @@ namespace Torch.Server
|
||||
)
|
||||
return;
|
||||
|
||||
var exe = Path.Combine(AppContext.BaseDirectory, "Torch.Server.exe");
|
||||
|
||||
var args = Environment.GetCommandLineArgs();
|
||||
|
||||
for (var i = 0; i < args.Length; i++)
|
||||
{
|
||||
if (args[i].Contains(' '))
|
||||
args[i] = $"\"{args[i]}\"";
|
||||
|
||||
if (!args[i].Contains("--tempAutostart", StringComparison.InvariantCultureIgnoreCase) &&
|
||||
!args[i].Contains("--waitForPid", StringComparison.InvariantCultureIgnoreCase))
|
||||
continue;
|
||||
|
||||
args[i] = string.Empty;
|
||||
args[++i] = string.Empty;
|
||||
}
|
||||
|
||||
Process.Start(exe, $"--waitForPid {Environment.ProcessId} --tempAutostart true {string.Join(" ", args)}");
|
||||
ApplicationRestartHelper.RestartApplication();
|
||||
})
|
||||
{
|
||||
Name = "Restart thread"
|
||||
|
@@ -33,24 +33,7 @@ internal class UnhandledExceptionHandler
|
||||
Console.WriteLine("Restarting in 5 seconds.");
|
||||
Thread.Sleep(5000);
|
||||
|
||||
var exe = Path.Combine(AppContext.BaseDirectory, "Torch.Server.exe");
|
||||
|
||||
var args = Environment.GetCommandLineArgs();
|
||||
|
||||
for (var i = 0; i < args.Length; i++)
|
||||
{
|
||||
if (args[i].Contains(' '))
|
||||
args[i] = $"\"{args[i]}\"";
|
||||
|
||||
if (!args[i].Contains("--tempAutostart", StringComparison.InvariantCultureIgnoreCase) &&
|
||||
!args[i].Contains("--waitForPid", StringComparison.InvariantCultureIgnoreCase))
|
||||
continue;
|
||||
|
||||
args[i] = string.Empty;
|
||||
args[++i] = string.Empty;
|
||||
}
|
||||
|
||||
Process.Start(exe, $"--waitForPid {Environment.ProcessId} --tempAutostart true {string.Join(" ", args)}");
|
||||
ApplicationRestartHelper.RestartApplication();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
Reference in New Issue
Block a user