2020-02-09 18:08:34 +00:00
|
|
|
using Autofac;
|
2018-05-01 12:00:02 +00:00
|
|
|
using Jackett.Common.Models.Config;
|
|
|
|
using Jackett.Common.Services.Interfaces;
|
2018-06-10 02:33:16 +00:00
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2018-05-01 12:00:02 +00:00
|
|
|
using NLog;
|
2023-01-07 19:38:52 +00:00
|
|
|
#if !NET462
|
2019-10-14 17:51:33 +00:00
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
#endif
|
2018-05-01 12:00:02 +00:00
|
|
|
|
|
|
|
namespace Jackett.Server
|
|
|
|
{
|
2018-06-03 11:11:18 +00:00
|
|
|
public static class Helper
|
2018-05-01 12:00:02 +00:00
|
|
|
{
|
|
|
|
public static IContainer ApplicationContainer { get; set; }
|
|
|
|
|
2023-01-07 19:38:52 +00:00
|
|
|
#if NET462
|
2019-10-14 17:51:33 +00:00
|
|
|
public static IApplicationLifetime applicationLifetime;
|
|
|
|
#else
|
|
|
|
public static IHostApplicationLifetime applicationLifetime;
|
|
|
|
#endif
|
|
|
|
|
2018-05-01 12:00:02 +00:00
|
|
|
public static void Initialize()
|
|
|
|
{
|
|
|
|
//Load the indexers
|
|
|
|
ServerService.Initalize();
|
2018-06-14 09:21:31 +00:00
|
|
|
|
|
|
|
//Kicks off the update checker
|
|
|
|
ServerService.Start();
|
2018-07-10 12:22:02 +00:00
|
|
|
|
|
|
|
Logger.Debug("Helper initialization complete");
|
2018-05-01 12:00:02 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 02:33:16 +00:00
|
|
|
public static void RestartWebHost()
|
|
|
|
{
|
|
|
|
Logger.Info("Restart of the web application host (not process) initiated");
|
|
|
|
Program.isWebHostRestart = true;
|
|
|
|
applicationLifetime.StopApplication();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void StopWebHost()
|
|
|
|
{
|
|
|
|
Logger.Info("Jackett is being stopped");
|
|
|
|
applicationLifetime.StopApplication();
|
2018-06-03 11:11:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static IConfigurationService ConfigService => ApplicationContainer.Resolve<IConfigurationService>();
|
2018-06-03 11:11:18 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static IServerService ServerService => ApplicationContainer.Resolve<IServerService>();
|
2018-06-03 11:11:18 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static IServiceConfigService ServiceConfigService => ApplicationContainer.Resolve<IServiceConfigService>();
|
2018-06-10 02:33:16 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static ServerConfig ServerConfiguration => ApplicationContainer.Resolve<ServerConfig>();
|
2018-06-03 11:11:18 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static Logger Logger => ApplicationContainer.Resolve<Logger>();
|
2018-06-03 11:11:18 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public static void SetupLogging(ContainerBuilder builder) =>
|
|
|
|
builder?.RegisterInstance(LogManager.GetCurrentClassLogger()).SingleInstance();
|
2018-06-22 12:35:58 +00:00
|
|
|
|
|
|
|
public static void SetLogLevel(LogLevel level)
|
|
|
|
{
|
|
|
|
foreach (var rule in LogManager.Configuration.LoggingRules)
|
|
|
|
{
|
2018-11-14 16:52:56 +00:00
|
|
|
if (rule.LoggerNamePattern == "Microsoft.*")
|
|
|
|
{
|
|
|
|
if (!rule.Levels.Contains(LogLevel.Debug))
|
|
|
|
{
|
|
|
|
//don't change the first microsoftRule
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var targets = LogManager.Configuration.ConfiguredNamedTargets;
|
|
|
|
if (level == LogLevel.Debug)
|
|
|
|
{
|
|
|
|
foreach (var target in targets)
|
|
|
|
{
|
|
|
|
rule.Targets.Add(target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach (var target in targets)
|
|
|
|
{
|
|
|
|
rule.Targets.Remove(target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-06-22 12:35:58 +00:00
|
|
|
if (level == LogLevel.Debug)
|
|
|
|
{
|
|
|
|
if (!rule.Levels.Contains(LogLevel.Debug))
|
|
|
|
{
|
|
|
|
rule.EnableLoggingForLevel(LogLevel.Debug);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (rule.Levels.Contains(LogLevel.Debug))
|
|
|
|
{
|
|
|
|
rule.DisableLoggingForLevel(LogLevel.Debug);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LogManager.ReconfigExistingLoggers();
|
|
|
|
}
|
2018-05-01 12:00:02 +00:00
|
|
|
}
|
|
|
|
}
|