Add PIDFile CLI option

This commit is contained in:
kaso17
2017-12-04 12:20:22 +01:00
parent 105bd85441
commit 2934bfb3e7
6 changed files with 56 additions and 12 deletions

View File

@@ -12,6 +12,7 @@ using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Targets;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
@@ -41,6 +42,20 @@ namespace Jackett
builder.RegisterModule(module);
}
container = builder.Build();
// create PID file early
if (!string.IsNullOrWhiteSpace(settings.PIDFile))
{
try
{
var proc = Process.GetCurrentProcess();
File.WriteAllText(settings.PIDFile, proc.Id.ToString());
}
catch (Exception e)
{
Logger.Error(e, "Error while creating the PID file");
}
}
}
private static void InitAutomapper()
@@ -220,6 +235,30 @@ namespace Jackett
LogManager.ReconfigExistingLoggers();
}
public static void Exit(int exitCode)
{
try
{
if (Engine.ServerConfig != null &&
Engine.ServerConfig.RuntimeSettings != null &&
!string.IsNullOrWhiteSpace(Engine.ServerConfig.RuntimeSettings.PIDFile))
{
var PIDFile = Engine.ServerConfig.RuntimeSettings.PIDFile;
if (File.Exists(PIDFile))
{
Engine.Logger.Info("Deleting PID file " + PIDFile);
File.Delete(PIDFile);
}
}
}
catch (Exception e)
{
Logger.Error(e, "Error while deleting the PID file");
}
Environment.Exit(exitCode);
}
public static void SaveServerConfig()
{
ConfigService.SaveConfig(ServerConfig);

View File

@@ -66,7 +66,8 @@ namespace Jackett.Common.Models.Config
[Option("NoRestart", HelpText = "Don't restart after update")]
public bool NoRestart { get; set; }
[Option("PIDFile", HelpText = "Specify the location of PID file")]
public string PIDFile { get; set; }
public RuntimeSettings ToRunTimeSettings()
{
@@ -83,7 +84,7 @@ namespace Jackett.Common.Models.Config
if (options.ListenPublic && options.ListenPrivate)
{
Console.WriteLine("You can only use listen private OR listen publicly.");
Environment.Exit(1);
Engine.Exit(1);
}
// SSL Fix
@@ -105,6 +106,8 @@ namespace Jackett.Common.Models.Config
if (!string.IsNullOrWhiteSpace(options.DataFolder))
runtimeSettings.CustomDataFolder = options.DataFolder;
runtimeSettings.PIDFile = options.PIDFile;
return runtimeSettings;
}

View File

@@ -27,6 +27,8 @@ namespace Jackett.Common.Models.Config
public string CustomLogFileName { get; set; }
public string PIDFile { get; set; }
public string DataFolder
{

View File

@@ -257,7 +257,7 @@ namespace Jackett.Services
private void StartUpdate(string updaterExePath, string installLocation, bool isWindows, bool NoRestart)
{
var exe = Path.GetFileName(ExePath());
var args = string.Join(" ", Environment.GetCommandLineArgs().Skip(1));
var args = string.Join(" ", Environment.GetCommandLineArgs().Skip(1).Select(a => a.Contains(" ") ? "\"" +a + "\"" : a )).Replace("\"", "\\\"");
var startInfo = new ProcessStartInfo();
@@ -299,7 +299,7 @@ namespace Jackett.Services
{
logger.Info("Exiting Jackett..");
lockService.Signal();
Environment.Exit(0);
Engine.Exit(0);
}
}
}

View File

@@ -152,7 +152,7 @@ namespace JackettConsole
else
{
Engine.Logger.Error("Unable to switch to public listening without admin rights.");
Environment.Exit(1);
Engine.Exit(1);
}
}
@@ -176,7 +176,7 @@ namespace JackettConsole
else
{
Engine.Logger.Error("Unable to switch ports when not running as administrator");
Environment.Exit(1);
Engine.Exit(1);
}
}

View File

@@ -144,7 +144,7 @@ namespace Jackett.Services
if (monoVersionO.Major < 4)
{
logger.Error("Your mono version is to old (mono 3 is no longer supported). Please update to the latest version from http://www.mono-project.com/download/");
Environment.Exit(2);
Engine.Exit(2);
}
else if (monoVersionO.Major == 4 && monoVersionO.Minor == 2)
{
@@ -202,7 +202,7 @@ namespace Jackett.Services
{
logger.Debug(e);
logger.Error(e.Message + " Most likely the mono-locale-extras package is not installed.");
Environment.Exit(2);
Engine.Exit(2);
}
}
}
@@ -249,12 +249,12 @@ namespace Jackett.Services
if (inner is SocketException && ((SocketException)inner).SocketErrorCode == SocketError.AddressAlreadyInUse) // Linux (mono)
{
logger.Error("Address already in use: Most likely Jackett is already running.");
Environment.Exit(1);
Engine.Exit(1);
}
else if (inner is HttpListenerException && ((HttpListenerException)inner).ErrorCode == 183) // Windows
{
logger.Error(inner.Message + " Most likely Jackett is already running.");
Environment.Exit(1);
Engine.Exit(1);
}
throw e;
}