2017-11-13 08:38:38 +00:00
|
|
|
|
using Jackett.Common.Models.Config;
|
2018-06-23 01:37:49 +00:00
|
|
|
|
using Jackett.Common.Services;
|
|
|
|
|
using Jackett.Common.Services.Interfaces;
|
|
|
|
|
using Jackett.Common.Utils;
|
|
|
|
|
using NLog;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Reflection;
|
2015-07-19 00:27:41 +00:00
|
|
|
|
using System.ServiceProcess;
|
|
|
|
|
|
|
|
|
|
namespace Jackett.Service
|
|
|
|
|
{
|
|
|
|
|
public partial class Service : ServiceBase
|
|
|
|
|
{
|
2018-06-23 01:37:49 +00:00
|
|
|
|
private IProcessService processService;
|
|
|
|
|
private Process consoleProcess;
|
|
|
|
|
private Logger logger;
|
|
|
|
|
private bool serviceStopInitiated;
|
|
|
|
|
|
2015-07-19 00:27:41 +00:00
|
|
|
|
public Service()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2018-06-23 01:37:49 +00:00
|
|
|
|
|
|
|
|
|
RuntimeSettings runtimeSettings = new RuntimeSettings()
|
|
|
|
|
{
|
|
|
|
|
CustomLogFileName = "ServiceLog.txt"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LogManager.Configuration = LoggingSetup.GetLoggingConfiguration(runtimeSettings);
|
|
|
|
|
logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
|
|
logger.Info("Initiating Jackett Service v" + EnvironmentUtil.JackettVersion);
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
string applicationFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
|
|
|
|
|
|
|
|
|
|
var exePath = Path.Combine(applicationFolder, "JackettConsole.exe");
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error(e.Data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ProcessExited(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!serviceStopInitiated)
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Service stop not responsible for process exit");
|
|
|
|
|
OnStop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void StopConsoleApplication()
|
|
|
|
|
{
|
|
|
|
|
if (consoleProcess != null && !consoleProcess.HasExited)
|
|
|
|
|
{
|
|
|
|
|
consoleProcess.StandardInput.Close();
|
|
|
|
|
System.Threading.Thread.Sleep(1000);
|
|
|
|
|
if (consoleProcess != null && !consoleProcess.HasExited)
|
|
|
|
|
{
|
|
|
|
|
consoleProcess.Kill();
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-19 00:27:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|