2020-03-03 16:28:26 +00:00
|
|
|
using System;
|
2018-06-23 01:37:49 +00:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
2015-07-19 00:27:41 +00:00
|
|
|
using System.ServiceProcess;
|
2020-02-09 18:08:34 +00:00
|
|
|
using Jackett.Common.Models.Config;
|
|
|
|
using Jackett.Common.Services;
|
|
|
|
using Jackett.Common.Services.Interfaces;
|
|
|
|
using Jackett.Common.Utils;
|
|
|
|
using NLog;
|
2015-07-19 00:27:41 +00:00
|
|
|
|
|
|
|
namespace Jackett.Service
|
|
|
|
{
|
|
|
|
public partial class Service : ServiceBase
|
|
|
|
{
|
2020-02-10 22:16:19 +00:00
|
|
|
private readonly IProcessService processService;
|
2018-06-23 01:37:49 +00:00
|
|
|
private Process consoleProcess;
|
2020-02-10 22:16:19 +00:00
|
|
|
private readonly Logger logger;
|
2018-06-23 01:37:49 +00:00
|
|
|
private bool serviceStopInitiated;
|
|
|
|
|
2015-07-19 00:27:41 +00:00
|
|
|
public Service()
|
|
|
|
{
|
|
|
|
InitializeComponent();
|
2018-06-23 01:37:49 +00:00
|
|
|
|
2020-02-10 22:16:19 +00:00
|
|
|
var runtimeSettings = new RuntimeSettings()
|
2018-06-23 01:37:49 +00:00
|
|
|
{
|
|
|
|
CustomLogFileName = "ServiceLog.txt"
|
|
|
|
};
|
|
|
|
|
|
|
|
LogManager.Configuration = LoggingSetup.GetLoggingConfiguration(runtimeSettings);
|
|
|
|
logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
2020-12-06 23:14:23 +00:00
|
|
|
logger.Info("Initiating Jackett Service " + EnvironmentUtil.JackettVersion());
|
2018-06-23 01:37:49 +00:00
|
|
|
|
|
|
|
processService = new ProcessService(logger);
|
2015-07-19 00:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnStart(string[] args)
|
|
|
|
{
|
2018-06-23 01:37:49 +00:00
|
|
|
logger.Info("Service starting");
|
|
|
|
serviceStopInitiated = false;
|
|
|
|
StartConsoleApplication();
|
2015-07-19 00:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnStop()
|
|
|
|
{
|
2018-06-23 01:37:49 +00:00
|
|
|
logger.Info("Service stopping");
|
|
|
|
serviceStopInitiated = true;
|
|
|
|
StopConsoleApplication();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void StartConsoleApplication()
|
|
|
|
{
|
2020-12-12 20:38:33 +00:00
|
|
|
var exePath = Path.Combine(EnvironmentUtil.JackettInstallationPath(), "JackettConsole.exe");
|
2018-06-23 01:37:49 +00:00
|
|
|
|
|
|
|
var startInfo = new ProcessStartInfo()
|
|
|
|
{
|
|
|
|
CreateNoWindow = true,
|
|
|
|
UseShellExecute = false,
|
|
|
|
FileName = exePath,
|
|
|
|
RedirectStandardInput = true,
|
|
|
|
RedirectStandardError = true
|
|
|
|
};
|
|
|
|
|
|
|
|
consoleProcess = Process.Start(startInfo);
|
|
|
|
consoleProcess.EnableRaisingEvents = true;
|
|
|
|
consoleProcess.Exited += ProcessExited;
|
|
|
|
consoleProcess.ErrorDataReceived += ProcessErrorDataReceived;
|
|
|
|
}
|
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
private void ProcessErrorDataReceived(object sender, DataReceivedEventArgs e) => logger.Error(e.Data);
|
2018-06-23 01:37:49 +00:00
|
|
|
|
|
|
|
private void ProcessExited(object sender, EventArgs e)
|
|
|
|
{
|
2018-06-26 09:44:12 +00:00
|
|
|
logger.Info("Console process exited");
|
|
|
|
|
2018-06-23 01:37:49 +00:00
|
|
|
if (!serviceStopInitiated)
|
|
|
|
{
|
|
|
|
logger.Info("Service stop not responsible for process exit");
|
2018-06-26 09:44:12 +00:00
|
|
|
Stop();
|
2018-06-23 01:37:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void StopConsoleApplication()
|
|
|
|
{
|
|
|
|
if (consoleProcess != null && !consoleProcess.HasExited)
|
|
|
|
{
|
|
|
|
consoleProcess.StandardInput.Close();
|
2018-06-26 09:44:12 +00:00
|
|
|
consoleProcess.WaitForExit(2000);
|
2018-06-23 01:37:49 +00:00
|
|
|
if (consoleProcess != null && !consoleProcess.HasExited)
|
|
|
|
{
|
|
|
|
consoleProcess.Kill();
|
|
|
|
}
|
|
|
|
}
|
2015-07-19 00:27:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|