1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

Fixed: Close database connection in housekeeping tasks

This commit is contained in:
Márki-Zay Ferenc 2023-06-15 17:35:11 +02:00 committed by Bogdan
parent 7052a7a5ec
commit e4429d2919
3 changed files with 48 additions and 45 deletions

View File

@ -16,29 +16,30 @@ public CleanupDownloadClientUnavailablePendingReleases(IMainDatabase database)
public void Clean() public void Clean()
{ {
var mapper = _database.OpenConnection(); using (var mapper = _database.OpenConnection())
if (_database.DatabaseType == DatabaseType.PostgreSQL)
{ {
if (_database.DatabaseType == DatabaseType.PostgreSQL)
{
mapper.Execute(@"DELETE FROM ""PendingReleases""
WHERE ""Added"" < @TwoWeeksAgo
AND ""Reason"" = ANY (@Reasons)",
new
{
TwoWeeksAgo = DateTime.UtcNow.AddDays(-14),
Reasons = new[] { (int)PendingReleaseReason.DownloadClientUnavailable, (int)PendingReleaseReason.Fallback }
});
}
else
{
mapper.Execute(@"DELETE FROM ""PendingReleases"" mapper.Execute(@"DELETE FROM ""PendingReleases""
WHERE ""Added"" < @TwoWeeksAgo WHERE ""Added"" < @TwoWeeksAgo
AND ""Reason"" = ANY (@Reasons)", AND ""REASON"" IN @Reasons",
new new
{ {
TwoWeeksAgo = DateTime.UtcNow.AddDays(-14), TwoWeeksAgo = DateTime.UtcNow.AddDays(-14),
Reasons = new[] { (int)PendingReleaseReason.DownloadClientUnavailable, (int)PendingReleaseReason.Fallback } Reasons = new[] { (int)PendingReleaseReason.DownloadClientUnavailable, (int)PendingReleaseReason.Fallback }
}); });
} }
else
{
mapper.Execute(@"DELETE FROM ""PendingReleases""
WHERE ""Added"" < @TwoWeeksAgo
AND ""REASON"" IN @Reasons",
new
{
TwoWeeksAgo = DateTime.UtcNow.AddDays(-14),
Reasons = new[] { (int)PendingReleaseReason.DownloadClientUnavailable, (int)PendingReleaseReason.Fallback }
});
} }
} }
} }

View File

@ -14,14 +14,15 @@ public CleanupOrphanedDownloadClientStatus(IMainDatabase database)
public void Clean() public void Clean()
{ {
var mapper = _database.OpenConnection(); using (var mapper = _database.OpenConnection())
{
mapper.Execute(@"DELETE FROM ""DownloadClientStatus"" mapper.Execute(@"DELETE FROM ""DownloadClientStatus""
WHERE ""Id"" IN ( WHERE ""Id"" IN (
SELECT ""DownloadClientStatus"".""Id"" FROM ""DownloadClientStatus"" SELECT ""DownloadClientStatus"".""Id"" FROM ""DownloadClientStatus""
LEFT OUTER JOIN ""DownloadClients"" LEFT OUTER JOIN ""DownloadClients""
ON ""DownloadClientStatus"".""ProviderId"" = ""DownloadClients"".""Id"" ON ""DownloadClientStatus"".""ProviderId"" = ""DownloadClients"".""Id""
WHERE ""DownloadClients"".""Id"" IS NULL)"); WHERE ""DownloadClients"".""Id"" IS NULL)");
}
} }
} }
} }

View File

@ -18,30 +18,31 @@ public CleanupUnusedTags(IMainDatabase database)
public void Clean() public void Clean()
{ {
var mapper = _database.OpenConnection(); using (var mapper = _database.OpenConnection())
var usedTags = new[] { "Movies", "Notifications", "DelayProfiles", "Restrictions", "ImportLists", "Indexers" }
.SelectMany(v => GetUsedTags(v, mapper))
.Distinct()
.ToList();
if (usedTags.Any())
{ {
var usedTagsList = usedTags.Select(d => d.ToString()).Join(","); var usedTags = new[] { "Movies", "Notifications", "DelayProfiles", "Restrictions", "ImportLists", "Indexers" }
.SelectMany(v => GetUsedTags(v, mapper))
.Distinct()
.ToList();
if (_database.DatabaseType == DatabaseType.PostgreSQL) if (usedTags.Any())
{ {
mapper.Execute($"DELETE FROM \"Tags\" WHERE NOT \"Id\" = ANY (\'{{{usedTagsList}}}\'::int[])"); var usedTagsList = usedTags.Select(d => d.ToString()).Join(",");
if (_database.DatabaseType == DatabaseType.PostgreSQL)
{
mapper.Execute($"DELETE FROM \"Tags\" WHERE NOT \"Id\" = ANY (\'{{{usedTagsList}}}\'::int[])");
}
else
{
mapper.Execute($"DELETE FROM \"Tags\" WHERE NOT \"Id\" IN ({usedTagsList})");
}
} }
else else
{ {
mapper.Execute($"DELETE FROM \"Tags\" WHERE NOT \"Id\" IN ({usedTagsList})"); mapper.Execute("DELETE FROM \"Tags\"");
} }
} }
else
{
mapper.Execute("DELETE FROM \"Tags\"");
}
} }
private int[] GetUsedTags(string table, IDbConnection mapper) private int[] GetUsedTags(string table, IDbConnection mapper)