mirror of
https://github.com/Jackett/Jackett.git
synced 2025-09-17 17:34:09 +02:00
Add --NoRestart command line flag and pass pids to the updater (--KillPids)
This commit is contained in:
@@ -63,6 +63,9 @@ namespace Jackett.Console
|
||||
[Option('d', "DataFolder", HelpText = "Specify the location of the data folder (Must be admin on Windows) eg. --DataFolder=\"D:\\Your Data\\Jackett\\\"")]
|
||||
public string DataFolder { get; set; }
|
||||
|
||||
[Option(HelpText = "Don't restart after update")]
|
||||
public bool NoRestart { get; set; }
|
||||
|
||||
[ParserState]
|
||||
public IParserState LastParserState { get; set; }
|
||||
}
|
||||
|
@@ -217,6 +217,8 @@ namespace JackettConsole
|
||||
Engine.Server.SaveConfig();
|
||||
}
|
||||
}
|
||||
|
||||
Startup.NoRestart = options.NoRestart;
|
||||
}
|
||||
|
||||
Engine.Server.Initalize();
|
||||
|
@@ -9,6 +9,9 @@ using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#if __MonoCS__
|
||||
using Mono.Unix.Native;
|
||||
#endif
|
||||
|
||||
namespace Jackett.Updater
|
||||
{
|
||||
@@ -23,7 +26,7 @@ namespace Jackett.Updater
|
||||
{
|
||||
Engine.SetupLogging(null, "updater.txt");
|
||||
Engine.Logger.Info("Jackett Updater v" + GetCurrentVersion());
|
||||
Engine.Logger.Info("Options " + string.Join(" ", args));
|
||||
Engine.Logger.Info("Options \"" + string.Join("\" \"", args) + "\"");
|
||||
try {
|
||||
var options = new UpdaterConsoleOptions();
|
||||
if (Parser.Default.ParseArguments(args, options))
|
||||
@@ -32,7 +35,7 @@ namespace Jackett.Updater
|
||||
}
|
||||
else
|
||||
{
|
||||
Engine.Logger.Error("Failed to process update arguments!: " + string.Join(" ", args));
|
||||
Engine.Logger.Error("Failed to process update arguments!");
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
@@ -49,6 +52,35 @@ namespace Jackett.Updater
|
||||
return fvi.FileVersion;
|
||||
}
|
||||
|
||||
private void KillPids(int[] pids)
|
||||
{
|
||||
foreach (var pid in pids)
|
||||
{
|
||||
try
|
||||
{
|
||||
var proc = Process.GetProcessById(pid);
|
||||
Engine.Logger.Info("Killing process " + proc.Id);
|
||||
proc.Kill();
|
||||
var exited = proc.WaitForExit(5000);
|
||||
if (!exited)
|
||||
Engine.Logger.Info("Process " + pid.ToString() + " didn't exit within 5 seconds");
|
||||
#if __MonoCS__
|
||||
Engine.Logger.Info("Sending SIGKILL to process " + pid.ToString());
|
||||
Syscall.kill(proc.Id, Signum.SIGKILL);
|
||||
#endif
|
||||
}
|
||||
catch (ArgumentException e)
|
||||
{
|
||||
Engine.Logger.Info("Process " + pid.ToString() + " is already dead");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Engine.Logger.Info("Error killing process " + pid.ToString());
|
||||
Engine.Logger.Info(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessUpdate(UpdaterConsoleOptions options)
|
||||
{
|
||||
var updateLocation = GetUpdateLocation();
|
||||
@@ -57,6 +89,13 @@ namespace Jackett.Updater
|
||||
updateLocation += Path.DirectorySeparatorChar;
|
||||
}
|
||||
|
||||
var pids = new int[] { };
|
||||
if (options.KillPids != null)
|
||||
{
|
||||
var pidsStr = options.KillPids.Split(',').Where(pid => !string.IsNullOrWhiteSpace(pid)).ToArray();
|
||||
pids = Array.ConvertAll(pidsStr, pid => int.Parse(pid));
|
||||
}
|
||||
|
||||
var isWindows = System.Environment.OSVersion.Platform != PlatformID.Unix;
|
||||
var trayRunning = false;
|
||||
var trayProcesses = Process.GetProcessesByName("JackettTray");
|
||||
@@ -75,10 +114,11 @@ namespace Jackett.Updater
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Engine.Logger.Info("Waiting for Jackett to close..");
|
||||
Thread.Sleep(2000);
|
||||
// on unix we don't have to wait (we can overwrite files which are in use)
|
||||
// On unix we kill the PIDs after the update so e.g. systemd can automatically restart the process
|
||||
KillPids(pids);
|
||||
}
|
||||
Engine.Logger.Info("Finding files in: " + updateLocation);
|
||||
var files = Directory.GetFiles(updateLocation, "*.*", SearchOption.AllDirectories);
|
||||
foreach(var file in files)
|
||||
@@ -128,42 +168,49 @@ namespace Jackett.Updater
|
||||
}
|
||||
}
|
||||
|
||||
if (trayRunning)
|
||||
{
|
||||
var startInfo = new ProcessStartInfo()
|
||||
{
|
||||
Arguments = options.Args,
|
||||
FileName = Path.Combine(options.Path, "JackettTray.exe"),
|
||||
UseShellExecute = true
|
||||
};
|
||||
// kill pids after the update on UNIX
|
||||
if (!isWindows)
|
||||
KillPids(pids);
|
||||
|
||||
Process.Start(startInfo);
|
||||
}
|
||||
|
||||
if(string.Equals(options.Type, "JackettService.exe", StringComparison.InvariantCultureIgnoreCase))
|
||||
if (options.NoRestart == false)
|
||||
{
|
||||
var serviceHelper = new ServiceConfigService(null, null);
|
||||
if (serviceHelper.ServiceExists())
|
||||
if (trayRunning)
|
||||
{
|
||||
serviceHelper.Start();
|
||||
}
|
||||
} else
|
||||
{
|
||||
var startInfo = new ProcessStartInfo()
|
||||
{
|
||||
Arguments = options.Args,
|
||||
FileName = Path.Combine(options.Path, "JackettConsole.exe"),
|
||||
UseShellExecute = true
|
||||
};
|
||||
var startInfo = new ProcessStartInfo()
|
||||
{
|
||||
Arguments = options.Args,
|
||||
FileName = Path.Combine(options.Path, "JackettTray.exe"),
|
||||
UseShellExecute = true
|
||||
};
|
||||
|
||||
if (!isWindows)
|
||||
{
|
||||
startInfo.Arguments = startInfo.FileName + " " + startInfo.Arguments;
|
||||
startInfo.FileName = "mono";
|
||||
Process.Start(startInfo);
|
||||
}
|
||||
|
||||
Engine.Logger.Info("Starting Jackett: " + startInfo.FileName + " " + startInfo.Arguments);
|
||||
Process.Start(startInfo);
|
||||
if(string.Equals(options.Type, "JackettService.exe", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
var serviceHelper = new ServiceConfigService(null, null);
|
||||
if (serviceHelper.ServiceExists())
|
||||
{
|
||||
serviceHelper.Start();
|
||||
}
|
||||
} else
|
||||
{
|
||||
var startInfo = new ProcessStartInfo()
|
||||
{
|
||||
Arguments = options.Args,
|
||||
FileName = Path.Combine(options.Path, "JackettConsole.exe"),
|
||||
UseShellExecute = true
|
||||
};
|
||||
|
||||
if (!isWindows)
|
||||
{
|
||||
startInfo.Arguments = startInfo.FileName + " " + startInfo.Arguments;
|
||||
startInfo.FileName = "mono";
|
||||
}
|
||||
|
||||
Engine.Logger.Info("Starting Jackett: " + startInfo.FileName + " " + startInfo.Arguments);
|
||||
Process.Start(startInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -17,5 +17,11 @@ namespace Jackett.Updater
|
||||
|
||||
[Option('a', "Args", HelpText = "Launch arguments")]
|
||||
public string Args { get; set; }
|
||||
|
||||
[Option(HelpText = "Don't restart after update")]
|
||||
public bool NoRestart { get; set; }
|
||||
|
||||
[Option(HelpText = "PIDs which will be killed before (Windows) or after (Unix) the update")]
|
||||
public string KillPids { get; set; }
|
||||
}
|
||||
}
|
||||
|
@@ -130,7 +130,7 @@ namespace Jackett.Services
|
||||
var installDir = Path.GetDirectoryName(ExePath());
|
||||
var updaterPath = Path.Combine(tempDir, "Jackett", "JackettUpdater.exe");
|
||||
if (updaterPath != null)
|
||||
StartUpdate(updaterPath, installDir, isWindows);
|
||||
StartUpdate(updaterPath, installDir, isWindows, Startup.NoRestart);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -257,16 +257,17 @@ namespace Jackett.Services
|
||||
return tempDir;
|
||||
}
|
||||
|
||||
private void StartUpdate(string updaterExePath, string installLocation, bool isWindows)
|
||||
private void StartUpdate(string updaterExePath, string installLocation, bool isWindows, bool NoRestart)
|
||||
{
|
||||
var exe = Path.GetFileName(ExePath()).ToLowerInvariant();
|
||||
var args = string.Join(" ", Environment.GetCommandLineArgs().Skip(1));
|
||||
|
||||
var startInfo = new ProcessStartInfo();
|
||||
|
||||
// Note: add a leading space to the --Args argument to avoid parsing as arguments
|
||||
if (isWindows)
|
||||
{
|
||||
startInfo.Arguments = $"--Path \"{installLocation}\" --Type \"{exe}\" --Args \"{args}\"";
|
||||
startInfo.Arguments = $"--Path \"{installLocation}\" --Type \"{exe}\" --Args \" {args}\"";
|
||||
startInfo.FileName = Path.Combine(updaterExePath);
|
||||
}
|
||||
else
|
||||
@@ -275,18 +276,33 @@ namespace Jackett.Services
|
||||
args = exe + " " + args;
|
||||
exe = "mono";
|
||||
|
||||
startInfo.Arguments = $"{Path.Combine(updaterExePath)} --Path \"{installLocation}\" --Type \"{exe}\" --Args \"{args}\"";
|
||||
startInfo.Arguments = $"{Path.Combine(updaterExePath)} --Path \"{installLocation}\" --Type \"{exe}\" --Args \" {args}\"";
|
||||
startInfo.FileName = "mono";
|
||||
startInfo.UseShellExecute = false;
|
||||
startInfo.CreateNoWindow = true;
|
||||
}
|
||||
|
||||
try {
|
||||
var pid = Process.GetCurrentProcess().Id;
|
||||
startInfo.Arguments += $" --KillPids \"{pid}\"";
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.Error("Unexpected error while retriving the PID");
|
||||
logger.Error(e);
|
||||
}
|
||||
|
||||
if (NoRestart)
|
||||
startInfo.Arguments += " --NoRestart";
|
||||
|
||||
var procInfo = Process.Start(startInfo);
|
||||
logger.Info($"Updater started process id: {procInfo.Id}");
|
||||
logger.Info("Exiting Jackett..");
|
||||
lockService.Signal();
|
||||
Environment.Exit(0);
|
||||
if (NoRestart == false)
|
||||
{
|
||||
logger.Info("Exiting Jackett..");
|
||||
lockService.Signal();
|
||||
Environment.Exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -71,6 +71,12 @@ namespace Jackett
|
||||
set;
|
||||
}
|
||||
|
||||
public static bool NoRestart
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public void Configuration(IAppBuilder appBuilder)
|
||||
{
|
||||
// Configure Web API for self-host.
|
||||
|
Reference in New Issue
Block a user