From cc409d50f5a6994f662fdddf929481506128c63e Mon Sep 17 00:00:00 2001 From: Jared Date: Sun, 5 May 2024 01:54:42 +0000 Subject: [PATCH] New: Config file setting to disable log database (cherry picked from commit 92eab4b2e26741b7f20b6b7177418402cb15a3aa) --- src/Lidarr.Api.V1/Logs/LogController.cs | 10 +++++++++- src/Lidarr.Api.V1/Update/UpdateController.cs | 13 +++++++++++-- src/Lidarr.Http/PagingResource.cs | 2 +- .../ServiceFactoryFixture.cs | 1 + src/NzbDrone.Common/Options/LogOptions.cs | 1 + .../Configuration/ConfigFileProvider.cs | 3 ++- .../Extensions/CompositionExtensions.cs | 12 ++++++++++++ .../Update/History/UpdateHistoryService.cs | 7 +++++-- .../Update/RecentUpdateProvider.cs | 2 +- src/NzbDrone.Host/Bootstrap.cs | 19 +++++++++++++++++++ src/NzbDrone.Host/Startup.cs | 8 ++++++-- 11 files changed, 68 insertions(+), 10 deletions(-) diff --git a/src/Lidarr.Api.V1/Logs/LogController.cs b/src/Lidarr.Api.V1/Logs/LogController.cs index e08f56333..c232e3f58 100644 --- a/src/Lidarr.Api.V1/Logs/LogController.cs +++ b/src/Lidarr.Api.V1/Logs/LogController.cs @@ -2,6 +2,7 @@ using Lidarr.Http; using Lidarr.Http.Extensions; using Microsoft.AspNetCore.Mvc; using NzbDrone.Common.Extensions; +using NzbDrone.Core.Configuration; using NzbDrone.Core.Instrumentation; namespace Lidarr.Api.V1.Logs @@ -10,16 +11,23 @@ namespace Lidarr.Api.V1.Logs public class LogController : Controller { private readonly ILogService _logService; + private readonly IConfigFileProvider _configFileProvider; - public LogController(ILogService logService) + public LogController(ILogService logService, IConfigFileProvider configFileProvider) { _logService = logService; + _configFileProvider = configFileProvider; } [HttpGet] [Produces("application/json")] public PagingResource GetLogs([FromQuery] PagingRequestResource paging, string level) { + if (!_configFileProvider.LogDbEnabled) + { + return new PagingResource(); + } + var pagingResource = new PagingResource(paging); var pageSpec = pagingResource.MapToPagingSpec(); diff --git a/src/Lidarr.Api.V1/Update/UpdateController.cs b/src/Lidarr.Api.V1/Update/UpdateController.cs index 27cecce17..b67cae998 100644 --- a/src/Lidarr.Api.V1/Update/UpdateController.cs +++ b/src/Lidarr.Api.V1/Update/UpdateController.cs @@ -3,6 +3,7 @@ using System.Linq; using Lidarr.Http; using Microsoft.AspNetCore.Mvc; using NzbDrone.Common.EnvironmentInfo; +using NzbDrone.Core.Configuration; using NzbDrone.Core.Update; using NzbDrone.Core.Update.History; @@ -13,11 +14,13 @@ namespace Lidarr.Api.V1.Update { private readonly IRecentUpdateProvider _recentUpdateProvider; private readonly IUpdateHistoryService _updateHistoryService; + private readonly IConfigFileProvider _configFileProvider; - public UpdateController(IRecentUpdateProvider recentUpdateProvider, IUpdateHistoryService updateHistoryService) + public UpdateController(IRecentUpdateProvider recentUpdateProvider, IUpdateHistoryService updateHistoryService, IConfigFileProvider configFileProvider) { _recentUpdateProvider = recentUpdateProvider; _updateHistoryService = updateHistoryService; + _configFileProvider = configFileProvider; } [HttpGet] @@ -45,7 +48,13 @@ namespace Lidarr.Api.V1.Update installed.Installed = true; } - var installDates = _updateHistoryService.InstalledSince(resources.Last().ReleaseDate) + if (!_configFileProvider.LogDbEnabled) + { + return resources; + } + + var updateHistory = _updateHistoryService.InstalledSince(resources.Last().ReleaseDate); + var installDates = updateHistory .DistinctBy(v => v.Version) .ToDictionary(v => v.Version); diff --git a/src/Lidarr.Http/PagingResource.cs b/src/Lidarr.Http/PagingResource.cs index c5381f6d9..c38440df1 100644 --- a/src/Lidarr.Http/PagingResource.cs +++ b/src/Lidarr.Http/PagingResource.cs @@ -21,7 +21,7 @@ namespace Lidarr.Http public string SortKey { get; set; } public SortDirection SortDirection { get; set; } public int TotalRecords { get; set; } - public List Records { get; set; } + public List Records { get; set; } = new (); public PagingResource() { diff --git a/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs b/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs index 5ed08a3db..237febe74 100644 --- a/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs +++ b/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs @@ -30,6 +30,7 @@ namespace NzbDrone.Common.Test .AddNzbDroneLogger() .AutoAddServices(Bootstrap.ASSEMBLIES) .AddDummyDatabase() + .AddDummyLogDatabase() .AddStartupContext(new StartupContext("first", "second")); container.RegisterInstance(new Mock().Object); diff --git a/src/NzbDrone.Common/Options/LogOptions.cs b/src/NzbDrone.Common/Options/LogOptions.cs index 421059381..6460eeaa6 100644 --- a/src/NzbDrone.Common/Options/LogOptions.cs +++ b/src/NzbDrone.Common/Options/LogOptions.cs @@ -13,4 +13,5 @@ public class LogOptions public string SyslogServer { get; set; } public int? SyslogPort { get; set; } public string SyslogLevel { get; set; } + public bool? DbEnabled { get; set; } } diff --git a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs index 442a6fcd9..389728f84 100644 --- a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs +++ b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs @@ -57,6 +57,7 @@ namespace NzbDrone.Core.Configuration string SyslogServer { get; } int SyslogPort { get; } string SyslogLevel { get; } + bool LogDbEnabled { get; } string Theme { get; } string PostgresHost { get; } int PostgresPort { get; } @@ -238,7 +239,7 @@ namespace NzbDrone.Core.Configuration public string PostgresMainDb => _postgresOptions?.MainDb ?? GetValue("PostgresMainDb", "lidarr-main", persist: false); public string PostgresLogDb => _postgresOptions?.LogDb ?? GetValue("PostgresLogDb", "lidarr-log", persist: false); public int PostgresPort => (_postgresOptions?.Port ?? 0) != 0 ? _postgresOptions.Port : GetValueInt("PostgresPort", 5432, persist: false); - + public bool LogDbEnabled => _logOptions.DbEnabled ?? GetValueBoolean("LogDbEnabled", true, persist: false); public bool LogSql => _logOptions.Sql ?? GetValueBoolean("LogSql", false, persist: false); public int LogRotate => _logOptions.Rotate ?? GetValueInt("LogRotate", 50, persist: false); public int LogSizeLimit => Math.Min(Math.Max(_logOptions.SizeLimit ?? GetValueInt("LogSizeLimit", 1, persist: false), 0), 10); diff --git a/src/NzbDrone.Core/Datastore/Extensions/CompositionExtensions.cs b/src/NzbDrone.Core/Datastore/Extensions/CompositionExtensions.cs index c5e31f92c..67e251805 100644 --- a/src/NzbDrone.Core/Datastore/Extensions/CompositionExtensions.cs +++ b/src/NzbDrone.Core/Datastore/Extensions/CompositionExtensions.cs @@ -8,6 +8,12 @@ namespace NzbDrone.Core.Datastore.Extensions public static IContainer AddDatabase(this IContainer container) { container.RegisterDelegate(f => new MainDatabase(f.Create()), Reuse.Singleton); + + return container; + } + + public static IContainer AddLogDatabase(this IContainer container) + { container.RegisterDelegate(f => new LogDatabase(f.Create(MigrationType.Log)), Reuse.Singleton); return container; @@ -16,6 +22,12 @@ namespace NzbDrone.Core.Datastore.Extensions public static IContainer AddDummyDatabase(this IContainer container) { container.RegisterInstance(new MainDatabase(null)); + + return container; + } + + public static IContainer AddDummyLogDatabase(this IContainer container) + { container.RegisterInstance(new LogDatabase(null)); return container; diff --git a/src/NzbDrone.Core/Update/History/UpdateHistoryService.cs b/src/NzbDrone.Core/Update/History/UpdateHistoryService.cs index 09cf70602..7be7349e1 100644 --- a/src/NzbDrone.Core/Update/History/UpdateHistoryService.cs +++ b/src/NzbDrone.Core/Update/History/UpdateHistoryService.cs @@ -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; @@ -18,13 +19,15 @@ namespace NzbDrone.Core.Update.History { private readonly IUpdateHistoryRepository _repository; private readonly IEventAggregator _eventAggregator; + private readonly IConfigFileProvider _configFileProvider; private readonly Logger _logger; private Version _prevVersion; - public UpdateHistoryService(IUpdateHistoryRepository repository, IEventAggregator eventAggregator, Logger logger) + public UpdateHistoryService(IUpdateHistoryRepository repository, IEventAggregator eventAggregator, IConfigFileProvider configFileProvider, Logger logger) { _repository = repository; _eventAggregator = eventAggregator; + _configFileProvider = configFileProvider; _logger = logger; } @@ -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; diff --git a/src/NzbDrone.Core/Update/RecentUpdateProvider.cs b/src/NzbDrone.Core/Update/RecentUpdateProvider.cs index a3264300d..08cc39865 100644 --- a/src/NzbDrone.Core/Update/RecentUpdateProvider.cs +++ b/src/NzbDrone.Core/Update/RecentUpdateProvider.cs @@ -29,7 +29,7 @@ namespace NzbDrone.Core.Update { var branch = _configFileProvider.Branch; var version = BuildInfo.Version; - var prevVersion = _updateHistoryService.PreviouslyInstalled(); + var prevVersion = _configFileProvider.LogDbEnabled ? _updateHistoryService.PreviouslyInstalled() : null; return _updatePackageProvider.GetRecentUpdates(branch, version, prevVersion); } } diff --git a/src/NzbDrone.Host/Bootstrap.cs b/src/NzbDrone.Host/Bootstrap.cs index b8843c5a4..1e1c25ec6 100644 --- a/src/NzbDrone.Host/Bootstrap.cs +++ b/src/NzbDrone.Host/Bootstrap.cs @@ -94,6 +94,15 @@ namespace NzbDrone.Host .AddStartupContext(startupContext) .Resolve() .Route(appMode); + + if (config.GetValue(nameof(ConfigFileProvider.LogDbEnabled), true)) + { + c.AddLogDatabase(); + } + else + { + c.AddDummyLogDatabase(); + } }) .ConfigureServices(services => { @@ -139,6 +148,7 @@ namespace NzbDrone.Host var enableSsl = config.GetValue($"Lidarr:Server:{nameof(ServerOptions.EnableSsl)}") ?? config.GetValue(nameof(ConfigFileProvider.EnableSsl), false); var sslCertPath = config.GetValue($"Lidarr:Server:{nameof(ServerOptions.SslCertPath)}") ?? config.GetValue(nameof(ConfigFileProvider.SslCertPath)); var sslCertPassword = config.GetValue($"Lidarr:Server:{nameof(ServerOptions.SslCertPassword)}") ?? config.GetValue(nameof(ConfigFileProvider.SslCertPassword)); + var logDbEnabled = config.GetValue($"Lidarr:Log:{nameof(LogOptions.DbEnabled)}") ?? config.GetValue(nameof(ConfigFileProvider.LogDbEnabled), true); var urls = new List { BuildUrl("http", bindAddress, port) }; @@ -156,6 +166,15 @@ namespace NzbDrone.Host .AddNzbDroneLogger() .AddDatabase() .AddStartupContext(context); + + if (logDbEnabled) + { + c.AddLogDatabase(); + } + else + { + c.AddDummyLogDatabase(); + } }) .ConfigureServices(services => { diff --git a/src/NzbDrone.Host/Startup.cs b/src/NzbDrone.Host/Startup.cs index 75f393726..75f19260e 100644 --- a/src/NzbDrone.Host/Startup.cs +++ b/src/NzbDrone.Host/Startup.cs @@ -239,9 +239,13 @@ namespace NzbDrone.Host // instantiate the databases to initialize/migrate them _ = mainDatabaseFactory.Value; - _ = logDatabaseFactory.Value; - dbTarget.Register(); + if (configFileProvider.LogDbEnabled) + { + _ = logDatabaseFactory.Value; + dbTarget.Register(); + } + SchemaBuilder.Initialize(container); if (OsInfo.IsNotWindows)