New: toggle log database with a flag in config.xml

This commit is contained in:
sillock1 2024-04-19 17:43:02 +01:00
parent f77e27bace
commit 75e173b9ee
10 changed files with 84 additions and 21 deletions

View File

@ -29,6 +29,7 @@ namespace NzbDrone.Common.Test
.AddNzbDroneLogger()
.AutoAddServices(Bootstrap.ASSEMBLIES)
.AddDummyDatabase()
.AddDummyLogDatabase()
.AddStartupContext(new StartupContext("first", "second"));
container.RegisterInstance(new Mock<IHostLifetime>().Object);

View File

@ -61,6 +61,7 @@ namespace NzbDrone.Core.Configuration
string PostgresMainDb { get; }
string PostgresLogDb { get; }
string Theme { get; }
bool LogDbEnabled { get; }
}
public class ConfigFileProvider : IConfigFileProvider
@ -202,6 +203,7 @@ namespace NzbDrone.Core.Configuration
public string LogLevel => GetValue("LogLevel", "info").ToLowerInvariant();
public string ConsoleLogLevel => GetValue("ConsoleLogLevel", string.Empty, persist: false);
public string Theme => GetValue("Theme", "auto", persist: false);
public bool LogDbEnabled => GetValueBoolean("LogDbEnabled", true, false);
public string PostgresHost => _postgresOptions?.Host ?? GetValue("PostgresHost", string.Empty, persist: false);
public string PostgresUser => _postgresOptions?.User ?? GetValue("PostgresUser", string.Empty, persist: false);
public string PostgresPassword => _postgresOptions?.Password ?? GetValue("PostgresPassword", string.Empty, persist: false);

View File

@ -8,14 +8,24 @@ namespace NzbDrone.Core.Datastore.Extensions
public static IContainer AddDatabase(this IContainer container)
{
container.RegisterDelegate<IDbFactory, IMainDatabase>(f => new MainDatabase(f.Create()), Reuse.Singleton);
container.RegisterDelegate<IDbFactory, ILogDatabase>(f => new LogDatabase(f.Create(MigrationType.Log)), Reuse.Singleton);
return container;
}
public static IContainer AddLogDatabase(this IContainer container)
{
container.RegisterDelegate<IDbFactory, ILogDatabase>(f => new LogDatabase(f.Create(MigrationType.Log)), Reuse.Singleton);
return container;
}
public static IContainer AddDummyDatabase(this IContainer container)
{
container.RegisterInstance<IMainDatabase>(new MainDatabase(null));
return container;
}
public static IContainer AddDummyLogDatabase(this IContainer container)
{
container.RegisterInstance<ILogDatabase>(new LogDatabase(null));
return container;

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using NLog;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Update.History.Events;
@ -19,13 +20,15 @@ namespace NzbDrone.Core.Update.History
private readonly IUpdateHistoryRepository _repository;
private readonly IEventAggregator _eventAggregator;
private readonly Logger _logger;
private readonly IConfigFileProvider _configFileProvider;
private Version _prevVersion;
public UpdateHistoryService(IUpdateHistoryRepository repository, IEventAggregator eventAggregator, Logger logger)
public UpdateHistoryService(IUpdateHistoryRepository repository, IEventAggregator eventAggregator, Logger logger, IConfigFileProvider configFileProvider)
{
_repository = repository;
_eventAggregator = eventAggregator;
_logger = logger;
_configFileProvider = configFileProvider;
}
public Version PreviouslyInstalled()
@ -58,7 +61,7 @@ namespace NzbDrone.Core.Update.History
public void Handle(ApplicationStartedEvent message)
{
if (BuildInfo.Version.Major == 10)
if (BuildInfo.Version.Major == 10 || !_configFileProvider.LogDbEnabled)
{
// Don't save dev versions, they change constantly
return;

View File

@ -41,6 +41,7 @@ namespace NzbDrone.App.Test
.AutoAddServices(Bootstrap.ASSEMBLIES)
.AddNzbDroneLogger()
.AddDummyDatabase()
.AddDummyLogDatabase()
.AddStartupContext(args);
// dummy lifetime and broadcaster so tests resolve

View File

@ -83,23 +83,31 @@ namespace NzbDrone.Host
// Utility mode
default:
{
new HostBuilder()
.UseServiceProviderFactory(new DryIocServiceProviderFactory(new Container(rules => rules.WithNzbDroneRules())))
.ConfigureContainer<IContainer>(c =>
{
c.AutoAddServices(Bootstrap.ASSEMBLIES)
.AddNzbDroneLogger()
.AddDatabase()
.AddStartupContext(startupContext)
.Resolve<UtilityModeRouter>()
.Route(appMode);
})
.ConfigureServices(services =>
{
services.Configure<PostgresOptions>(config.GetSection("Radarr:Postgres"));
}).Build();
new HostBuilder()
.UseServiceProviderFactory(new DryIocServiceProviderFactory(new Container(rules => rules.WithNzbDroneRules())))
.ConfigureContainer<IContainer>(c =>
{
c.AutoAddServices(Bootstrap.ASSEMBLIES)
.AddNzbDroneLogger()
.AddDatabase()
.AddStartupContext(startupContext)
.Resolve<UtilityModeRouter>()
.Route(appMode);
if (config.GetValue(nameof(ConfigFileProvider.LogDbEnabled), true))
{
c.AddLogDatabase();
}
else
{
c.AddDummyLogDatabase();
}
})
.ConfigureServices(services =>
{
services.Configure<PostgresOptions>(config.GetSection("Radarr:Postgres"));
}).Build();
break;
break;
}
}
}
@ -130,6 +138,7 @@ namespace NzbDrone.Host
var enableSsl = config.GetValue(nameof(ConfigFileProvider.EnableSsl), false);
var sslCertPath = config.GetValue<string>(nameof(ConfigFileProvider.SslCertPath));
var sslCertPassword = config.GetValue<string>(nameof(ConfigFileProvider.SslCertPassword));
var logDbEnabled = config.GetValue<bool>(nameof(ConfigFileProvider.LogDbEnabled), true);
var urls = new List<string> { BuildUrl("http", bindAddress, port) };
@ -147,6 +156,14 @@ namespace NzbDrone.Host
.AddNzbDroneLogger()
.AddDatabase()
.AddStartupContext(context);
if (logDbEnabled)
{
c.AddLogDatabase();
}
else
{
c.AddDummyLogDatabase();
}
SchemaBuilder.Initialize(c);
})

View File

@ -236,9 +236,12 @@ namespace NzbDrone.Host
// instantiate the databases to initialize/migrate them
_ = mainDatabaseFactory.Value;
_ = logDatabaseFactory.Value;
dbTarget.Register();
if (configFileProvider.LogDbEnabled)
{
_ = logDatabaseFactory.Value;
dbTarget.Register();
}
if (OsInfo.IsNotWindows)
{

View File

@ -3,10 +3,12 @@ using NzbDrone.Common.Extensions;
using NzbDrone.Core.Instrumentation;
using Radarr.Http;
using Radarr.Http.Extensions;
using Radarr.Http.REST.Filters;
namespace Radarr.Api.V3.Logs
{
[V3ApiController]
[LogDatabaseDisabledActionFilter]
public class LogController : Controller
{
private readonly ILogService _logService;

View File

@ -5,10 +5,12 @@ using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Core.Update;
using NzbDrone.Core.Update.History;
using Radarr.Http;
using Radarr.Http.REST.Filters;
namespace Radarr.Api.V3.Update
{
[V3ApiController]
[LogDatabaseDisabledActionFilter]
public class UpdateController : Controller
{
private readonly IRecentUpdateProvider _recentUpdateProvider;

View File

@ -0,0 +1,22 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using NzbDrone.Core.Configuration;
namespace Radarr.Http.REST.Filters;
public class LogDatabaseDisabledActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
var configFileProvider = context.HttpContext.RequestServices.GetService<IConfigFileProvider>();
if (!configFileProvider.LogDbEnabled)
{
context.Result = new NotFoundResult();
}
}
public override void OnActionExecuted(ActionExecutedContext context)
{
}
}