Private GIT

Skip to content
Snippets Groups Projects
Commit 11881a0a authored by kaso17's avatar kaso17 Committed by vlbox
Browse files

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

parent 1166c029
Branches
No related tags found
No related merge requests found
......@@ -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,6 +168,12 @@ namespace Jackett.Updater
}
}
// kill pids after the update on UNIX
if (!isWindows)
KillPids(pids);
if (options.NoRestart == false)
{
if (trayRunning)
{
var startInfo = new ProcessStartInfo()
......@@ -166,6 +212,7 @@ namespace Jackett.Updater
Process.Start(startInfo);
}
}
}
private string GetUpdateLocation()
{
......
......@@ -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,13 +257,14 @@ 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}\"";
......@@ -281,12 +282,27 @@ namespace Jackett.Services
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}");
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.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment