Jackett.Service: Make compatible with both legacy & .NET Core

This commit is contained in:
flightlevel 2018-06-23 11:37:49 +10:00
parent b56552e0f4
commit 06758964c0
3 changed files with 80 additions and 31 deletions

View File

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
<system.net>
<settings>
<!-- needed to make the broken incapsula DDoS protection work on windows(e.g. for KickAssTorrent), see https://social.technet.microsoft.com/Forums/de-DE/b10b16d1-8eea-4b52-8aeb-f96ea87135fa/sectionresponseheader-detailcr-must-be-followed-by-lf?forum=powerquery -->
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
</configuration>

View File

@ -14,6 +14,7 @@
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<RuntimeIdentifier>win</RuntimeIdentifier>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@ -65,25 +66,14 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Content Include="jackett.ico" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CurlSharp\CurlSharp.csproj">
<Project>{74420a79-cc16-442c-8b1e-7c1b913844f0}</Project>
<Name>CurlSharp</Name>
</ProjectReference>
<ProjectReference Include="..\Jackett.Common\Jackett.Common.csproj">
<Project>{6B854A1B-9A90-49C0-BC37-9A35C75BCA73}</Project>
<Name>Jackett.Common</Name>
</ProjectReference>
<ProjectReference Include="..\Jackett\Jackett.csproj">
<Project>{e636d5f8-68b4-4903-b4ed-ccfd9c9e899f}</Project>
<Name>Jackett</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@ -1,29 +1,100 @@
using Jackett.Common.Models.Config;
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;
using System.ServiceProcess;
using Jackett.Common;
namespace Jackett.Service
{
public partial class Service : ServiceBase
{
private IProcessService processService;
private Process consoleProcess;
private Logger logger;
private bool serviceStopInitiated;
public Service()
{
InitializeComponent();
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);
}
protected override void OnStart(string[] args)
{
Engine.BuildContainer(new RuntimeSettings(), new WebApi2Module());
Engine.Logger.Info("Service starting");
Engine.Server.Initalize();
Engine.Server.Start();
Engine.Logger.Info("Service started");
logger.Info("Service starting");
serviceStopInitiated = false;
StartConsoleApplication();
}
protected override void OnStop()
{
Engine.Logger.Info("Service stopping");
Engine.Server.Stop();
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();
}
}
}
}
}