mirror of
https://github.com/Readarr/Readarr
synced 2026-05-08 21:13:58 +02:00
Fixed: Close all database connections on shutdown to remove shm/wal files
[common]
This commit is contained in:
parent
0031214fb3
commit
eb431f09fd
3 changed files with 13 additions and 4 deletions
|
|
@ -16,12 +16,11 @@ public class DatabaseTarget : TargetWithLayout, IHandle<ApplicationShutdownReque
|
||||||
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 @@ protected override void Write(LogEventInfo logEvent)
|
||||||
{
|
{
|
||||||
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 @@ protected override void Write(LogEventInfo logEvent)
|
||||||
|
|
||||||
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() });
|
||||||
|
|
|
||||||
|
|
@ -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.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 @@ private string GetRestartArgs()
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[EventHandleOrder(EventHandleOrder.Last)]
|
||||||
public void Handle(ApplicationShutdownRequested message)
|
public void Handle(ApplicationShutdownRequested message)
|
||||||
{
|
{
|
||||||
if (!_runtimeInfo.IsWindowsService)
|
if (!_runtimeInfo.IsWindowsService)
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
@ -104,6 +105,11 @@ public static void Start(string[] args, Action<IHostBuilder> trayCallback = null
|
||||||
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)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue