1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-09-17 15:02:34 +02:00

added database recovery on app start.

This commit is contained in:
kay.one 2013-01-19 11:42:06 -08:00
parent 653f45f10e
commit 130be58f8c
7 changed files with 162 additions and 110 deletions

View File

@ -5,7 +5,6 @@
using Autofac;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Core.Jobs;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.ExternalNotification;
@ -18,12 +17,12 @@ namespace NzbDrone.Core.Test
[TestFixture]
class CentralDispatchFixture : CoreTest
{
readonly IList<Type> indexers = typeof(CentralDispatch).Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(IndexerBase))).ToList();
readonly IList<Type> jobs = typeof(CentralDispatch).Assembly.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IJob))).ToList();
readonly IList<string> indexers = typeof(CentralDispatch).Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(IndexerBase))).Select(c => c.ToString()).ToList();
readonly IList<string> jobs = typeof(CentralDispatch).Assembly.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IJob))).Select(c=>c.ToString()).ToList();
readonly IList<Type> extNotifications = typeof(CentralDispatch).Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(ExternalNotificationBase))).ToList();
readonly IList<Type> metadata = typeof(CentralDispatch).Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(MetadataBase))).ToList();
private IContainer kernel;
private readonly IContainer kernel;
public CentralDispatchFixture()
{
@ -57,8 +56,6 @@ public void Resolve_all_providers()
[Test]
public void All_jobs_should_be_registered()
{
//Assert
var registeredJobs = kernel.Resolve<IEnumerable<IJob>>();
jobs.Should().NotBeEmpty();
@ -69,8 +66,6 @@ public void All_jobs_should_be_registered()
[Test]
public void All_indexers_should_be_registered()
{
//Assert
var registeredIndexers = kernel.Resolve<IEnumerable<IndexerBase>>();
indexers.Should().NotBeEmpty();
@ -81,8 +76,6 @@ public void All_indexers_should_be_registered()
[Test]
public void All_externalNotifiers_should_be_registered()
{
//Assert
var externalNotificationBases = kernel.Resolve<IEnumerable<ExternalNotificationBase>>();
extNotifications.Should().NotBeEmpty();
@ -93,8 +86,6 @@ public void All_externalNotifiers_should_be_registered()
[Test]
public void All_metadata_clients_should_be_registered()
{
//Assert
var metadataBases = kernel.Resolve<IEnumerable<MetadataBase>>();
metadata.Should().NotBeEmpty();
@ -111,7 +102,7 @@ public void jobs_are_initialized()
[Test]
public void indexers_are_initialized()
{
kernel.Resolve<IndexerProvider>().All().Should().HaveSameCount(indexers);
kernel.Resolve<IndexerProvider>().All().Select(c => c.IndexProviderType).Should().BeEquivalentTo(indexers);
}
[Test]
@ -129,7 +120,7 @@ public void metadata_clients_are_initialized()
[Test]
public void quality_profile_initialized()
{
kernel.Resolve<QualityProvider>().All().Should().HaveCount(2);
kernel.Resolve<QualityProvider>().All().Should().HaveCount(4);
}
[Test]

View File

@ -32,9 +32,9 @@ internal static IDatabase GetEmptyDatabase(string fileName = "")
File.Copy(DB_TEMPLATE_NAME, fileName);
ConnectionString = Connection.GetConnectionString(fileName);
ConnectionString = ConnectionFactory.GetConnectionString(fileName);
var database = Connection.GetPetaPocoDb(ConnectionString);
var database = ConnectionFactory.GetPetaPocoDb(ConnectionString);
Console.WriteLine("====================DataBase====================");
Console.WriteLine();
@ -46,8 +46,8 @@ internal static IDatabase GetEmptyDatabase(string fileName = "")
internal static void CreateDataBaseTemplate()
{
Console.WriteLine("Creating an empty PetaPoco database");
var connectionString = Connection.GetConnectionString(DB_TEMPLATE_NAME);
var database = Connection.GetPetaPocoDb(connectionString);
var connectionString = ConnectionFactory.GetConnectionString(DB_TEMPLATE_NAME);
var database = ConnectionFactory.GetPetaPocoDb(connectionString);
database.Dispose();
}
}

View File

@ -34,6 +34,8 @@ public static void RegisterCoreServices(this ContainerBuilder container)
private static void RegisterAssembly(this ContainerBuilder container, Assembly assembly)
{
logger.Info("Registering Services from {0}", assembly.FullName);
container.RegisterAssemblyTypes(assembly)
.AsSelf()
.SingleInstance();
@ -58,7 +60,7 @@ private static void RegisterAssembly(this ContainerBuilder container, Assembly a
.Where(t => t.IsSubclassOf(typeof(MetadataBase)))
.As<MetadataBase>().SingleInstance();
}
private static void InitDatabase(this ContainerBuilder container)
{
logger.Info("Registering Database...");
@ -66,14 +68,14 @@ private static void InitDatabase(this ContainerBuilder container)
var appDataPath = new EnvironmentProvider().GetAppDataPath();
if (!Directory.Exists(appDataPath)) Directory.CreateDirectory(appDataPath);
container.Register(c => c.Resolve<Connection>().GetMainPetaPocoDb())
container.Register(c => c.Resolve<ConnectionFactory>().GetMainPetaPocoDb())
.As<IDatabase>();
container.Register(c => c.Resolve<Connection>().GetLogPetaPocoDb(false))
container.Register(c => c.Resolve<ConnectionFactory>().GetLogPetaPocoDb(false))
.SingleInstance()
.Named<IDatabase>("DatabaseTarget");
container.Register(c => c.Resolve<Connection>().GetLogPetaPocoDb())
container.Register(c => c.Resolve<ConnectionFactory>().GetLogPetaPocoDb())
.Named<IDatabase>("LogProvider");
container.RegisterType<DatabaseTarget>().WithParameter(ResolvedParameter.ForNamed<IDatabase>("DatabaseTarget"));

View File

@ -1,80 +0,0 @@
using System;
using System.Configuration;
using System.Data.Common;
using System.Data.SqlServerCe;
using NzbDrone.Common;
using NzbDrone.Core.Instrumentation;
using PetaPoco;
namespace NzbDrone.Core.Datastore
{
public class Connection
{
private readonly EnvironmentProvider _environmentProvider;
static Connection()
{
Database.Mapper = new CustomeMapper();
var dataSet = ConfigurationManager.GetSection("system.data") as System.Data.DataSet;
dataSet.Tables[0].Rows.Add("Microsoft SQL Server Compact Data Provider 4.0"
, "System.Data.SqlServerCe.4.0"
, ".NET Framework Data Provider for Microsoft SQL Server Compact"
, "System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
}
public Connection(EnvironmentProvider environmentProvider)
{
_environmentProvider = environmentProvider;
}
public String MainConnectionString
{
get
{
return GetConnectionString(_environmentProvider.GetNzbDroneDbFile());
}
}
public String LogConnectionString
{
get
{
return GetConnectionString(_environmentProvider.GetLogDbFileDbFile());
}
}
public static string GetConnectionString(string path)
{
return String.Format("Data Source=\"{0}\"; Max Database Size = 512;", path);
}
public IDatabase GetMainPetaPocoDb(Boolean profiled = true)
{
return GetPetaPocoDb(MainConnectionString, profiled);
}
public IDatabase GetLogPetaPocoDb(Boolean profiled = true)
{
return GetPetaPocoDb(LogConnectionString, profiled);
}
public static IDatabase GetPetaPocoDb(string connectionString, Boolean profiled = true)
{
MigrationsHelper.Run(connectionString, true);
var factory = new DbProviderFactory
{
IsProfiled = profiled
};
var db = new Database(connectionString, factory, Database.DBType.SqlServerCE)
{
KeepConnectionAlive = true,
ForceDateTimesToUtc = false,
};
return db;
}
}
}

View File

@ -0,0 +1,145 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlServerCe;
using System.IO;
using NLog;
using NzbDrone.Common;
using PetaPoco;
namespace NzbDrone.Core.Datastore
{
public class ConnectionFactory
{
private readonly EnvironmentProvider _environmentProvider;
private static readonly Logger logger = LogManager.GetLogger("ConnectionFactory");
static ConnectionFactory()
{
Database.Mapper = new CustomeMapper();
var dataSet = (System.Data.DataSet)ConfigurationManager.GetSection("system.data");
dataSet.Tables[0].Rows.Add("Microsoft SQL Server Compact Data Provider 4.0"
, "System.Data.SqlServerCe.4.0"
, ".NET Framework Data Provider for Microsoft SQL Server Compact"
, "System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
}
public ConnectionFactory(EnvironmentProvider environmentProvider)
{
_environmentProvider = environmentProvider;
}
public String MainConnectionString
{
get
{
return GetConnectionString(_environmentProvider.GetNzbDroneDbFile());
}
}
public String LogConnectionString
{
get
{
return GetConnectionString(_environmentProvider.GetLogDbFileDbFile());
}
}
public static string GetConnectionString(string path)
{
return String.Format("Data Source=\"{0}\"; Max Database Size = 512;", path);
}
public IDatabase GetMainPetaPocoDb(Boolean profiled = true)
{
return GetPetaPocoDb(MainConnectionString, profiled);
}
public IDatabase GetLogPetaPocoDb(Boolean profiled = true)
{
return GetPetaPocoDb(LogConnectionString, profiled);
}
static readonly HashSet<String> initilized = new HashSet<string>();
public static IDatabase GetPetaPocoDb(string connectionString, Boolean profiled = true)
{
lock (initilized)
{
if (!initilized.Contains(connectionString))
{
VerifyDatabase(connectionString);
MigrationsHelper.Run(connectionString, true);
initilized.Add(connectionString);
}
}
var factory = new DbProviderFactory
{
IsProfiled = profiled
};
var db = new Database(connectionString, factory, Database.DBType.SqlServerCE)
{
KeepConnectionAlive = true,
ForceDateTimesToUtc = false,
};
return db;
}
private static void VerifyDatabase(string connectionString)
{
logger.Debug("Verifying database {0}", connectionString);
var sqlConnection = new SqlCeConnection(connectionString);
if (!File.Exists(sqlConnection.Database))
{
logger.Debug("database file doesn't exist. {0}", sqlConnection.Database);
return;
}
using (var sqlEngine = new SqlCeEngine(connectionString))
{
if (sqlEngine.Verify(VerifyOption.Default))
{
logger.Debug("Database integrity verified.");
}
else
{
logger.Error("Database verification failed.");
RepairDatabase(connectionString);
}
}
}
private static void RepairDatabase(string connectionString)
{
logger.Info("Attempting to repair database: {0}", connectionString);
using (var sqlEngine = new SqlCeEngine(connectionString))
{
try
{
sqlEngine.Repair(connectionString, RepairOption.RecoverAllOrFail);
logger.Info("Recovery was successful without any data loss {0}", connectionString);
}
catch (SqlCeException e)
{
logger.WarnException(
"Safe recovery failed. will attempts a more aggressive strategy. might case loss of data.",
e);
sqlEngine.Repair(connectionString, RepairOption.DeleteCorruptedRows);
logger.Warn("Database was recovered. some data might have been lost");
//TODO: do db cleanup to avoid broken relationships.
}
}
}
}
}

View File

@ -11,13 +11,9 @@ public class MigrationsHelper
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public static readonly Dictionary<String, String> _migrated = new Dictionary<string, string>();
public static void Run(string connetionString, bool trace)
{
if (_migrated.ContainsKey(connetionString)) return;
_migrated.Add(connetionString, string.Empty);
EnsureDatabase(connetionString);
Logger.Trace("Preparing to run database migration");
@ -37,9 +33,6 @@ public static void Run(string connetionString, bool trace)
migrator.MigrateToLastVersion();
//ForceSubSonicMigration(Connection.CreateSimpleRepository(connetionString));
Logger.Info("Database migration completed");
@ -47,6 +40,7 @@ public static void Run(string connetionString, bool trace)
catch (Exception e)
{
Logger.FatalException("An error has occurred while migrating database", e);
throw;
}
}

View File

@ -223,7 +223,7 @@
</Compile>
<Compile Include="Constants.cs" />
<Compile Include="ContainerExtentions.cs" />
<Compile Include="Datastore\Connection.cs" />
<Compile Include="Datastore\ConnectionFactory.cs" />
<Compile Include="Datastore\MigrationLogger.cs" />
<Compile Include="Datastore\MigrationsHelper.cs" />
<Compile Include="Datastore\CustomeMapper.cs" />