Add --NoRestart command line flag and pass pids to the updater (--KillPids)

This commit is contained in:
kaso17
2017-01-20 13:50:23 +01:00
parent bbff25fd54
commit 16ef8061a3
6 changed files with 124 additions and 44 deletions

View File

@@ -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\\\"")] [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; } public string DataFolder { get; set; }
[Option(HelpText = "Don't restart after update")]
public bool NoRestart { get; set; }
[ParserState] [ParserState]
public IParserState LastParserState { get; set; } public IParserState LastParserState { get; set; }
} }

View File

@@ -217,6 +217,8 @@ namespace JackettConsole
Engine.Server.SaveConfig(); Engine.Server.SaveConfig();
} }
} }
Startup.NoRestart = options.NoRestart;
} }
Engine.Server.Initalize(); Engine.Server.Initalize();

View File

@@ -9,6 +9,9 @@ using System.Reflection;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
#if __MonoCS__
using Mono.Unix.Native;
#endif
namespace Jackett.Updater namespace Jackett.Updater
{ {
@@ -23,7 +26,7 @@ namespace Jackett.Updater
{ {
Engine.SetupLogging(null, "updater.txt"); Engine.SetupLogging(null, "updater.txt");
Engine.Logger.Info("Jackett Updater v" + GetCurrentVersion()); Engine.Logger.Info("Jackett Updater v" + GetCurrentVersion());
Engine.Logger.Info("Options " + string.Join(" ", args)); Engine.Logger.Info("Options \"" + string.Join("\" \"", args) + "\"");
try { try {
var options = new UpdaterConsoleOptions(); var options = new UpdaterConsoleOptions();
if (Parser.Default.ParseArguments(args, options)) if (Parser.Default.ParseArguments(args, options))
@@ -32,7 +35,7 @@ namespace Jackett.Updater
} }
else else
{ {
Engine.Logger.Error("Failed to process update arguments!: " + string.Join(" ", args)); Engine.Logger.Error("Failed to process update arguments!");
Console.ReadKey(); Console.ReadKey();
} }
} }
@@ -49,6 +52,35 @@ namespace Jackett.Updater
return fvi.FileVersion; 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) private void ProcessUpdate(UpdaterConsoleOptions options)
{ {
var updateLocation = GetUpdateLocation(); var updateLocation = GetUpdateLocation();
@@ -57,6 +89,13 @@ namespace Jackett.Updater
updateLocation += Path.DirectorySeparatorChar; 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 isWindows = System.Environment.OSVersion.Platform != PlatformID.Unix;
var trayRunning = false; var trayRunning = false;
var trayProcesses = Process.GetProcessesByName("JackettTray"); var trayProcesses = Process.GetProcessesByName("JackettTray");
@@ -75,10 +114,11 @@ namespace Jackett.Updater
catch { } catch { }
} }
} }
}
Engine.Logger.Info("Waiting for Jackett to close.."); // on unix we don't have to wait (we can overwrite files which are in use)
Thread.Sleep(2000); // 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); Engine.Logger.Info("Finding files in: " + updateLocation);
var files = Directory.GetFiles(updateLocation, "*.*", SearchOption.AllDirectories); var files = Directory.GetFiles(updateLocation, "*.*", SearchOption.AllDirectories);
foreach(var file in files) foreach(var file in files)
@@ -128,42 +168,49 @@ namespace Jackett.Updater
} }
} }
if (trayRunning) // kill pids after the update on UNIX
{ if (!isWindows)
var startInfo = new ProcessStartInfo() KillPids(pids);
{
Arguments = options.Args,
FileName = Path.Combine(options.Path, "JackettTray.exe"),
UseShellExecute = true
};
Process.Start(startInfo); if (options.NoRestart == false)
}
if(string.Equals(options.Type, "JackettService.exe", StringComparison.InvariantCultureIgnoreCase))
{ {
var serviceHelper = new ServiceConfigService(null, null); if (trayRunning)
if (serviceHelper.ServiceExists())
{ {
serviceHelper.Start(); var startInfo = new ProcessStartInfo()
} {
} else Arguments = options.Args,
{ FileName = Path.Combine(options.Path, "JackettTray.exe"),
var startInfo = new ProcessStartInfo() UseShellExecute = true
{ };
Arguments = options.Args,
FileName = Path.Combine(options.Path, "JackettConsole.exe"),
UseShellExecute = true
};
if (!isWindows) Process.Start(startInfo);
{
startInfo.Arguments = startInfo.FileName + " " + startInfo.Arguments;
startInfo.FileName = "mono";
} }
Engine.Logger.Info("Starting Jackett: " + startInfo.FileName + " " + startInfo.Arguments); if(string.Equals(options.Type, "JackettService.exe", StringComparison.InvariantCultureIgnoreCase))
Process.Start(startInfo); {
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);
}
} }
} }

View File

@@ -17,5 +17,11 @@ namespace Jackett.Updater
[Option('a', "Args", HelpText = "Launch arguments")] [Option('a', "Args", HelpText = "Launch arguments")]
public string Args { get; set; } 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; }
} }
} }

View File

@@ -130,7 +130,7 @@ namespace Jackett.Services
var installDir = Path.GetDirectoryName(ExePath()); var installDir = Path.GetDirectoryName(ExePath());
var updaterPath = Path.Combine(tempDir, "Jackett", "JackettUpdater.exe"); var updaterPath = Path.Combine(tempDir, "Jackett", "JackettUpdater.exe");
if (updaterPath != null) if (updaterPath != null)
StartUpdate(updaterPath, installDir, isWindows); StartUpdate(updaterPath, installDir, isWindows, Startup.NoRestart);
} }
catch (Exception e) catch (Exception e)
{ {
@@ -257,16 +257,17 @@ namespace Jackett.Services
return tempDir; 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 exe = Path.GetFileName(ExePath()).ToLowerInvariant();
var args = string.Join(" ", Environment.GetCommandLineArgs().Skip(1)); var args = string.Join(" ", Environment.GetCommandLineArgs().Skip(1));
var startInfo = new ProcessStartInfo(); var startInfo = new ProcessStartInfo();
// Note: add a leading space to the --Args argument to avoid parsing as arguments
if (isWindows) if (isWindows)
{ {
startInfo.Arguments = $"--Path \"{installLocation}\" --Type \"{exe}\" --Args \"{args}\""; startInfo.Arguments = $"--Path \"{installLocation}\" --Type \"{exe}\" --Args \" {args}\"";
startInfo.FileName = Path.Combine(updaterExePath); startInfo.FileName = Path.Combine(updaterExePath);
} }
else else
@@ -275,18 +276,33 @@ namespace Jackett.Services
args = exe + " " + args; args = exe + " " + args;
exe = "mono"; 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.FileName = "mono";
startInfo.UseShellExecute = false; startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true; 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); var procInfo = Process.Start(startInfo);
logger.Info($"Updater started process id: {procInfo.Id}"); logger.Info($"Updater started process id: {procInfo.Id}");
logger.Info("Exiting Jackett.."); if (NoRestart == false)
lockService.Signal(); {
Environment.Exit(0); logger.Info("Exiting Jackett..");
lockService.Signal();
Environment.Exit(0);
}
} }
} }
} }

View File

@@ -71,6 +71,12 @@ namespace Jackett
set; set;
} }
public static bool NoRestart
{
get;
set;
}
public void Configuration(IAppBuilder appBuilder) public void Configuration(IAppBuilder appBuilder)
{ {
// Configure Web API for self-host. // Configure Web API for self-host.