Fixed: Close all database connections on shutdown to remove shm/wal files

(cherry picked from commit eb431f09fdede191bfe6bc6c2da9c8cb12d804a1)
This commit is contained in:
ta264 2022-01-16 22:00:47 +00:00
parent 4ba118d351
commit ded8602976
3 changed files with 13 additions and 4 deletions

View File

@ -16,12 +16,11 @@ namespace NzbDrone.Core.Instrumentation
private const string INSERT_COMMAND = "INSERT INTO [Logs]([Message],[Time],[Logger],[Exception],[ExceptionType],[Level]) " + private const string INSERT_COMMAND = "INSERT INTO [Logs]([Message],[Time],[Logger],[Exception],[ExceptionType],[Level]) " +
"VALUES(@Message,@Time,@Logger,@Exception,@ExceptionType,@Level)"; "VALUES(@Message,@Time,@Logger,@Exception,@ExceptionType,@Level)";
private readonly SQLiteConnection _connection; private readonly IConnectionStringFactory _connectionStringFactory;
public DatabaseTarget(IConnectionStringFactory connectionStringFactory) public DatabaseTarget(IConnectionStringFactory connectionStringFactory)
{ {
_connection = new SQLiteConnection(connectionStringFactory.LogDbConnectionString); _connectionStringFactory = connectionStringFactory;
_connection.Open();
} }
public void Register() public void Register()
@ -56,6 +55,7 @@ namespace NzbDrone.Core.Instrumentation
{ {
try try
{ {
using var connection = new SQLiteConnection(_connectionStringFactory.LogDbConnectionString).OpenAndReturn();
var log = new Log(); var log = new Log();
log.Time = logEvent.TimeStamp; log.Time = logEvent.TimeStamp;
log.Message = CleanseLogMessage.Cleanse(logEvent.FormattedMessage); log.Message = CleanseLogMessage.Cleanse(logEvent.FormattedMessage);
@ -84,7 +84,7 @@ namespace NzbDrone.Core.Instrumentation
log.Level = logEvent.Level.Name; log.Level = logEvent.Level.Name;
var sqlCommand = new SQLiteCommand(INSERT_COMMAND, _connection); var sqlCommand = new SQLiteCommand(INSERT_COMMAND, connection);
sqlCommand.Parameters.Add(new SQLiteParameter("Message", DbType.String) { Value = log.Message }); sqlCommand.Parameters.Add(new SQLiteParameter("Message", DbType.String) { Value = log.Message });
sqlCommand.Parameters.Add(new SQLiteParameter("Time", DbType.DateTime) { Value = log.Time.ToUniversalTime() }); sqlCommand.Parameters.Add(new SQLiteParameter("Time", DbType.DateTime) { Value = log.Time.ToUniversalTime() });

View File

@ -1,3 +1,4 @@
using System.Data.SQLite;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@ -6,6 +7,7 @@ using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes; using NzbDrone.Common.Processes;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Messaging;
using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Messaging.Events;
namespace NzbDrone.Host namespace NzbDrone.Host
@ -99,6 +101,7 @@ namespace NzbDrone.Host
return args; return args;
} }
[EventHandleOrder(EventHandleOrder.Last)]
public void Handle(ApplicationShutdownRequested message) public void Handle(ApplicationShutdownRequested message)
{ {
if (!_runtimeInfo.IsWindowsService) if (!_runtimeInfo.IsWindowsService)

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.SQLite;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Security.Cryptography; using System.Security.Cryptography;
@ -102,6 +103,11 @@ namespace NzbDrone.Host
Logger.Info(ex.Message); Logger.Info(ex.Message);
LogManager.Configuration = null; LogManager.Configuration = null;
} }
// Make sure there are no lingering database connections
GC.Collect();
GC.WaitForPendingFinalizers();
SQLiteConnection.ClearAllPools();
} }
public static IHostBuilder CreateConsoleHostBuilder(string[] args, StartupContext context) public static IHostBuilder CreateConsoleHostBuilder(string[] args, StartupContext context)