Skip to content

Commit

Permalink
New: toggle log database with a flag in config.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
sillock1 committed Apr 19, 2024
1 parent f77e27b commit 75e173b
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/NzbDrone.Common.Test/ServiceFactoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void event_handlers_should_be_unique()
.AddNzbDroneLogger()
.AutoAddServices(Bootstrap.ASSEMBLIES)
.AddDummyDatabase()
.AddDummyLogDatabase()
.AddStartupContext(new StartupContext("first", "second"));

container.RegisterInstance(new Mock<IHostLifetime>().Object);
Expand Down
2 changes: 2 additions & 0 deletions src/NzbDrone.Core/Configuration/ConfigFileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public interface IConfigFileProvider : IHandleAsync<ApplicationStartedEvent>,
string PostgresMainDb { get; }
string PostgresLogDb { get; }
string Theme { get; }
bool LogDbEnabled { get; }
}

public class ConfigFileProvider : IConfigFileProvider
Expand Down Expand Up @@ -202,6 +203,7 @@ public AuthenticationType AuthenticationMethod
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);
Expand Down
12 changes: 11 additions & 1 deletion src/NzbDrone.Core/Datastore/Extensions/CompositionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@ public static class CompositionExtensions
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;
Expand Down
7 changes: 5 additions & 2 deletions src/NzbDrone.Core/Update/History/UpdateHistoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,13 +20,15 @@ public class UpdateHistoryService : IUpdateHistoryService, IHandle<ApplicationSt
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()
Expand Down Expand Up @@ -58,7 +61,7 @@ public List<UpdateHistory> InstalledSince(DateTime dateTime)

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;
Expand Down
1 change: 1 addition & 0 deletions src/NzbDrone.Host.Test/ContainerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void SetUp()
.AutoAddServices(Bootstrap.ASSEMBLIES)
.AddNzbDroneLogger()
.AddDummyDatabase()
.AddDummyLogDatabase()
.AddStartupContext(args);

// dummy lifetime and broadcaster so tests resolve
Expand Down
51 changes: 34 additions & 17 deletions src/NzbDrone.Host/Bootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,31 @@ public static void Start(string[] args, Action<IHostBuilder> trayCallback = null
// 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();

break;
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;
}
}
}
Expand Down Expand Up @@ -130,6 +138,7 @@ public static IHostBuilder CreateConsoleHostBuilder(string[] args, StartupContex
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) };

Expand All @@ -147,6 +156,14 @@ public static IHostBuilder CreateConsoleHostBuilder(string[] args, StartupContex
.AddNzbDroneLogger()
.AddDatabase()
.AddStartupContext(context);
if (logDbEnabled)
{
c.AddLogDatabase();
}
else
{
c.AddDummyLogDatabase();
}
SchemaBuilder.Initialize(c);
})
Expand Down
7 changes: 5 additions & 2 deletions src/NzbDrone.Host/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,12 @@ public void ConfigureServices(IServiceCollection services)

// instantiate the databases to initialize/migrate them
_ = mainDatabaseFactory.Value;
_ = logDatabaseFactory.Value;

dbTarget.Register();
if (configFileProvider.LogDbEnabled)
{
_ = logDatabaseFactory.Value;
dbTarget.Register();
}

if (OsInfo.IsNotWindows)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Radarr.Api.V3/Logs/LogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
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;
Expand Down
2 changes: 2 additions & 0 deletions src/Radarr.Api.V3/Update/UpdateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
{
}
}

0 comments on commit 75e173b

Please sign in to comment.