Skip to content

Commit 75e173b

Browse files
author
sillock1
committed
New: toggle log database with a flag in config.xml
1 parent f77e27b commit 75e173b

File tree

10 files changed

+85
-22
lines changed

10 files changed

+85
-22
lines changed

src/NzbDrone.Common.Test/ServiceFactoryFixture.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public void event_handlers_should_be_unique()
2929
.AddNzbDroneLogger()
3030
.AutoAddServices(Bootstrap.ASSEMBLIES)
3131
.AddDummyDatabase()
32+
.AddDummyLogDatabase()
3233
.AddStartupContext(new StartupContext("first", "second"));
3334

3435
container.RegisterInstance(new Mock<IHostLifetime>().Object);

src/NzbDrone.Core/Configuration/ConfigFileProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public interface IConfigFileProvider : IHandleAsync<ApplicationStartedEvent>,
6161
string PostgresMainDb { get; }
6262
string PostgresLogDb { get; }
6363
string Theme { get; }
64+
bool LogDbEnabled { get; }
6465
}
6566

6667
public class ConfigFileProvider : IConfigFileProvider
@@ -202,6 +203,7 @@ public AuthenticationType AuthenticationMethod
202203
public string LogLevel => GetValue("LogLevel", "info").ToLowerInvariant();
203204
public string ConsoleLogLevel => GetValue("ConsoleLogLevel", string.Empty, persist: false);
204205
public string Theme => GetValue("Theme", "auto", persist: false);
206+
public bool LogDbEnabled => GetValueBoolean("LogDbEnabled", true, false);
205207
public string PostgresHost => _postgresOptions?.Host ?? GetValue("PostgresHost", string.Empty, persist: false);
206208
public string PostgresUser => _postgresOptions?.User ?? GetValue("PostgresUser", string.Empty, persist: false);
207209
public string PostgresPassword => _postgresOptions?.Password ?? GetValue("PostgresPassword", string.Empty, persist: false);

src/NzbDrone.Core/Datastore/Extensions/CompositionExtensions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@ public static class CompositionExtensions
88
public static IContainer AddDatabase(this IContainer container)
99
{
1010
container.RegisterDelegate<IDbFactory, IMainDatabase>(f => new MainDatabase(f.Create()), Reuse.Singleton);
11-
container.RegisterDelegate<IDbFactory, ILogDatabase>(f => new LogDatabase(f.Create(MigrationType.Log)), Reuse.Singleton);
11+
return container;
12+
}
1213

14+
public static IContainer AddLogDatabase(this IContainer container)
15+
{
16+
container.RegisterDelegate<IDbFactory, ILogDatabase>(f => new LogDatabase(f.Create(MigrationType.Log)), Reuse.Singleton);
1317
return container;
1418
}
1519

1620
public static IContainer AddDummyDatabase(this IContainer container)
1721
{
1822
container.RegisterInstance<IMainDatabase>(new MainDatabase(null));
23+
24+
return container;
25+
}
26+
27+
public static IContainer AddDummyLogDatabase(this IContainer container)
28+
{
1929
container.RegisterInstance<ILogDatabase>(new LogDatabase(null));
2030

2131
return container;

src/NzbDrone.Core/Update/History/UpdateHistoryService.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using NLog;
44
using NzbDrone.Common.EnvironmentInfo;
5+
using NzbDrone.Core.Configuration;
56
using NzbDrone.Core.Lifecycle;
67
using NzbDrone.Core.Messaging.Events;
78
using NzbDrone.Core.Update.History.Events;
@@ -19,13 +20,15 @@ public class UpdateHistoryService : IUpdateHistoryService, IHandle<ApplicationSt
1920
private readonly IUpdateHistoryRepository _repository;
2021
private readonly IEventAggregator _eventAggregator;
2122
private readonly Logger _logger;
23+
private readonly IConfigFileProvider _configFileProvider;
2224
private Version _prevVersion;
2325

24-
public UpdateHistoryService(IUpdateHistoryRepository repository, IEventAggregator eventAggregator, Logger logger)
26+
public UpdateHistoryService(IUpdateHistoryRepository repository, IEventAggregator eventAggregator, Logger logger, IConfigFileProvider configFileProvider)
2527
{
2628
_repository = repository;
2729
_eventAggregator = eventAggregator;
2830
_logger = logger;
31+
_configFileProvider = configFileProvider;
2932
}
3033

3134
public Version PreviouslyInstalled()
@@ -58,7 +61,7 @@ public List<UpdateHistory> InstalledSince(DateTime dateTime)
5861

5962
public void Handle(ApplicationStartedEvent message)
6063
{
61-
if (BuildInfo.Version.Major == 10)
64+
if (BuildInfo.Version.Major == 10 || !_configFileProvider.LogDbEnabled)
6265
{
6366
// Don't save dev versions, they change constantly
6467
return;

src/NzbDrone.Host.Test/ContainerFixture.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void SetUp()
4141
.AutoAddServices(Bootstrap.ASSEMBLIES)
4242
.AddNzbDroneLogger()
4343
.AddDummyDatabase()
44+
.AddDummyLogDatabase()
4445
.AddStartupContext(args);
4546

4647
// dummy lifetime and broadcaster so tests resolve

src/NzbDrone.Host/Bootstrap.cs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,31 @@ public static void Start(string[] args, Action<IHostBuilder> trayCallback = null
8383
// Utility mode
8484
default:
8585
{
86-
new HostBuilder()
87-
.UseServiceProviderFactory(new DryIocServiceProviderFactory(new Container(rules => rules.WithNzbDroneRules())))
88-
.ConfigureContainer<IContainer>(c =>
89-
{
90-
c.AutoAddServices(Bootstrap.ASSEMBLIES)
91-
.AddNzbDroneLogger()
92-
.AddDatabase()
93-
.AddStartupContext(startupContext)
94-
.Resolve<UtilityModeRouter>()
95-
.Route(appMode);
96-
})
97-
.ConfigureServices(services =>
98-
{
99-
services.Configure<PostgresOptions>(config.GetSection("Radarr:Postgres"));
100-
}).Build();
101-
102-
break;
86+
new HostBuilder()
87+
.UseServiceProviderFactory(new DryIocServiceProviderFactory(new Container(rules => rules.WithNzbDroneRules())))
88+
.ConfigureContainer<IContainer>(c =>
89+
{
90+
c.AutoAddServices(Bootstrap.ASSEMBLIES)
91+
.AddNzbDroneLogger()
92+
.AddDatabase()
93+
.AddStartupContext(startupContext)
94+
.Resolve<UtilityModeRouter>()
95+
.Route(appMode);
96+
if (config.GetValue(nameof(ConfigFileProvider.LogDbEnabled), true))
97+
{
98+
c.AddLogDatabase();
99+
}
100+
else
101+
{
102+
c.AddDummyLogDatabase();
103+
}
104+
})
105+
.ConfigureServices(services =>
106+
{
107+
services.Configure<PostgresOptions>(config.GetSection("Radarr:Postgres"));
108+
}).Build();
109+
110+
break;
103111
}
104112
}
105113
}
@@ -130,6 +138,7 @@ public static IHostBuilder CreateConsoleHostBuilder(string[] args, StartupContex
130138
var enableSsl = config.GetValue(nameof(ConfigFileProvider.EnableSsl), false);
131139
var sslCertPath = config.GetValue<string>(nameof(ConfigFileProvider.SslCertPath));
132140
var sslCertPassword = config.GetValue<string>(nameof(ConfigFileProvider.SslCertPassword));
141+
var logDbEnabled = config.GetValue<bool>(nameof(ConfigFileProvider.LogDbEnabled), true);
133142

134143
var urls = new List<string> { BuildUrl("http", bindAddress, port) };
135144

@@ -147,6 +156,14 @@ public static IHostBuilder CreateConsoleHostBuilder(string[] args, StartupContex
147156
.AddNzbDroneLogger()
148157
.AddDatabase()
149158
.AddStartupContext(context);
159+
if (logDbEnabled)
160+
{
161+
c.AddLogDatabase();
162+
}
163+
else
164+
{
165+
c.AddDummyLogDatabase();
166+
}
150167

151168
SchemaBuilder.Initialize(c);
152169
})

src/NzbDrone.Host/Startup.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,12 @@ public void Configure(IApplicationBuilder app,
236236

237237
// instantiate the databases to initialize/migrate them
238238
_ = mainDatabaseFactory.Value;
239-
_ = logDatabaseFactory.Value;
240239

241-
dbTarget.Register();
240+
if (configFileProvider.LogDbEnabled)
241+
{
242+
_ = logDatabaseFactory.Value;
243+
dbTarget.Register();
244+
}
242245

243246
if (OsInfo.IsNotWindows)
244247
{

src/Radarr.Api.V3/Logs/LogController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
using NzbDrone.Core.Instrumentation;
44
using Radarr.Http;
55
using Radarr.Http.Extensions;
6+
using Radarr.Http.REST.Filters;
67

78
namespace Radarr.Api.V3.Logs
89
{
910
[V3ApiController]
11+
[LogDatabaseDisabledActionFilter]
1012
public class LogController : Controller
1113
{
1214
private readonly ILogService _logService;

src/Radarr.Api.V3/Update/UpdateController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
using NzbDrone.Core.Update;
66
using NzbDrone.Core.Update.History;
77
using Radarr.Http;
8+
using Radarr.Http.REST.Filters;
89

910
namespace Radarr.Api.V3.Update
1011
{
1112
[V3ApiController]
13+
[LogDatabaseDisabledActionFilter]
1214
public class UpdateController : Controller
1315
{
1416
private readonly IRecentUpdateProvider _recentUpdateProvider;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.Mvc.Filters;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using NzbDrone.Core.Configuration;
5+
6+
namespace Radarr.Http.REST.Filters;
7+
8+
public class LogDatabaseDisabledActionFilterAttribute : ActionFilterAttribute
9+
{
10+
public override void OnActionExecuting(ActionExecutingContext context)
11+
{
12+
var configFileProvider = context.HttpContext.RequestServices.GetService<IConfigFileProvider>();
13+
if (!configFileProvider.LogDbEnabled)
14+
{
15+
context.Result = new NotFoundResult();
16+
}
17+
}
18+
19+
public override void OnActionExecuted(ActionExecutedContext context)
20+
{
21+
}
22+
}

0 commit comments

Comments
 (0)