From c1845959359847246de78732e05e8df06a6a8245 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Fri, 18 Jan 2013 20:46:43 -0800 Subject: [PATCH] added Nancy pipelines for error handling, cleaned up container registrations. --- NzbDrone.Api/Bootstrapper.cs | 63 +++++++-- NzbDrone.Api/ErrorManagment/ApiException.cs | 41 ++++++ NzbDrone.Api/ErrorManagment/ErrorHandler.cs | 33 +++++ NzbDrone.Api/ErrorManagment/ErrorModel.cs | 21 +++ NzbDrone.Api/ErrorManagment/ErrorPipeline.cs | 28 ++++ NzbDrone.Api/NzbDrone.Api.csproj | 8 ++ NzbDrone.Api/QualityType/RequestExtensions.cs | 4 +- NzbDrone.Api/packages.config | 1 + NzbDrone.Core/CentralDispatch.cs | 129 ++---------------- NzbDrone.Core/ContainerExtentions.cs | 86 ++++++++++++ NzbDrone.Core/Jobs/JobProvider.cs | 13 +- NzbDrone.Core/NzbDrone.Core.csproj | 2 + NzbDrone.Core/Properties/AssemblyInfo.cs | 2 +- .../Providers/ExternalNotificationProvider.cs | 8 +- NzbDrone.Core/Providers/IndexerProvider.cs | 13 +- NzbDrone.Core/Providers/MetadataProvider.cs | 8 +- NzbDrone.Core/Providers/NewznabProvider.cs | 17 ++- NzbDrone.Core/Providers/QualityProvider.cs | 3 +- .../Providers/QualityTypeProvider.cs | 7 +- NzbDrone.Web/Global.asax.cs | 10 -- NzbDrone.Web/NzbDrone.Web.csproj | 32 ----- NzbDrone.Web/packages.config | 5 - 22 files changed, 333 insertions(+), 201 deletions(-) create mode 100644 NzbDrone.Api/ErrorManagment/ApiException.cs create mode 100644 NzbDrone.Api/ErrorManagment/ErrorHandler.cs create mode 100644 NzbDrone.Api/ErrorManagment/ErrorModel.cs create mode 100644 NzbDrone.Api/ErrorManagment/ErrorPipeline.cs create mode 100644 NzbDrone.Core/ContainerExtentions.cs diff --git a/NzbDrone.Api/Bootstrapper.cs b/NzbDrone.Api/Bootstrapper.cs index 81d509be7..818341466 100644 --- a/NzbDrone.Api/Bootstrapper.cs +++ b/NzbDrone.Api/Bootstrapper.cs @@ -1,27 +1,37 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Linq; using AutoMapper; +using Autofac; +using NLog; +using Nancy.Bootstrapper; +using Nancy.Bootstrappers.Autofac; +using NzbDrone.Api.ErrorManagment; using NzbDrone.Api.QualityProfiles; using NzbDrone.Api.QualityType; using NzbDrone.Api.Resolvers; +using NzbDrone.Core; using NzbDrone.Core.Repository.Quality; namespace NzbDrone.Api { - public static class Bootstrapper + + public class Bootstrapper : AutofacNancyBootstrapper { + private readonly Logger _logger; + + + public static void Initialize() { //QualityProfiles Mapper.CreateMap() .ForMember(dest => dest.QualityProfileId, opt => opt.MapFrom(src => src.Id)) - .ForMember(dest => dest.Allowed, opt => opt.ResolveUsing().FromMember(src => src.Qualities)); + .ForMember(dest => dest.Allowed, + opt => opt.ResolveUsing().FromMember(src => src.Qualities)); Mapper.CreateMap() .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.QualityProfileId)) - .ForMember(dest => dest.Qualities, opt => opt.ResolveUsing().FromMember(src => src.Allowed)); + .ForMember(dest => dest.Qualities, + opt => opt.ResolveUsing().FromMember(src => src.Allowed)); Mapper.CreateMap() .ForMember(dest => dest.Allowed, opt => opt.Ignore()); @@ -33,5 +43,42 @@ public static void Initialize() Mapper.CreateMap() .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.QualityTypeId)); } + + protected override ILifetimeScope GetApplicationContainer() + { + _logger.Info("Starting NzbDrone API"); + + + var builder = new ContainerBuilder(); + + builder.RegisterCoreServices(); + + builder.RegisterAssemblyTypes(typeof(Bootstrapper).Assembly) + .AsImplementedInterfaces() + .SingleInstance(); + + builder.RegisterType().AsSelf().SingleInstance(); + + + var container = builder.Build(); + + ApplicationPipelines.OnError.AddItemToEndOfPipeline(container.Resolve().HandleException); + + + return container; + } + + protected override NancyInternalConfiguration InternalConfiguration + { + get + { + var internalConfig = NancyInternalConfiguration.Default; + + internalConfig.StatusCodeHandlers.Add(typeof(ErrorHandler)); + + + return internalConfig; + } + } } -} +} \ No newline at end of file diff --git a/NzbDrone.Api/ErrorManagment/ApiException.cs b/NzbDrone.Api/ErrorManagment/ApiException.cs new file mode 100644 index 000000000..f9d1e1265 --- /dev/null +++ b/NzbDrone.Api/ErrorManagment/ApiException.cs @@ -0,0 +1,41 @@ +using System; +using System.Linq; +using Nancy; +using Nancy.Responses; +using Newtonsoft.Json; +using NzbDrone.Api.QualityType; + +namespace NzbDrone.Api.ErrorManagment +{ + public abstract class ApiException : Exception + { + public object Content { get; private set; } + + + public HttpStatusCode StatusCode { get; private set; } + + protected ApiException(HttpStatusCode statusCode, object content = null) + : base(GetMessage(statusCode, content)) + { + StatusCode = statusCode; + Content = content; + } + + public JsonResponse ToErrorResponse() + { + return new ErrorModel(this).AsResponse(StatusCode); + } + + private static string GetMessage(HttpStatusCode statusCode, object content) + { + var result = statusCode.ToString(); + + if (content != null) + { + result = result + " :" + JsonConvert.SerializeObject(content); + } + + return result; + } + } +} \ No newline at end of file diff --git a/NzbDrone.Api/ErrorManagment/ErrorHandler.cs b/NzbDrone.Api/ErrorManagment/ErrorHandler.cs new file mode 100644 index 000000000..9dd643e46 --- /dev/null +++ b/NzbDrone.Api/ErrorManagment/ErrorHandler.cs @@ -0,0 +1,33 @@ +using System.Linq; +using Nancy; +using Nancy.ErrorHandling; +using NzbDrone.Api.QualityType; + +namespace NzbDrone.Api.ErrorManagment +{ + public class ErrorHandler : IStatusCodeHandler + { + public bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context) + { + return true; + } + + public void Handle(HttpStatusCode statusCode, NancyContext context) + { + if (statusCode == HttpStatusCode.SeeOther || statusCode == HttpStatusCode.OK) + return; + + if (statusCode == HttpStatusCode.Continue) + { + context.Response = new Response { StatusCode = statusCode }; + return; + } + + if (context.Response.ContentType == "text/html" || context.Response.ContentType == "text/plain") + context.Response = new ErrorModel + { + Message = statusCode.ToString() + }.AsResponse(statusCode); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Api/ErrorManagment/ErrorModel.cs b/NzbDrone.Api/ErrorManagment/ErrorModel.cs new file mode 100644 index 000000000..81f83a0bf --- /dev/null +++ b/NzbDrone.Api/ErrorManagment/ErrorModel.cs @@ -0,0 +1,21 @@ +using System.Linq; + +namespace NzbDrone.Api.ErrorManagment +{ + public class ErrorModel + { + public string Message { get; set; } + public string Description { get; set; } + public object Content { get; set; } + + public ErrorModel(ApiException exception) + { + Message = exception.Message; + Content = exception.Content; + } + + public ErrorModel() + { + } + } +} \ No newline at end of file diff --git a/NzbDrone.Api/ErrorManagment/ErrorPipeline.cs b/NzbDrone.Api/ErrorManagment/ErrorPipeline.cs new file mode 100644 index 000000000..528ce5792 --- /dev/null +++ b/NzbDrone.Api/ErrorManagment/ErrorPipeline.cs @@ -0,0 +1,28 @@ +using System; +using System.Linq; +using NLog; +using Nancy; + +namespace NzbDrone.Api.ErrorManagment +{ + public class ErrorPipeline + { + private readonly Logger _logger; + + public ErrorPipeline(Logger logger) + { + _logger = logger; + } + + public Response HandleException(NancyContext context, Exception exception) + { + if (exception is ApiException) + { + _logger.WarnException("API Error", exception); + return ((ApiException)exception).ToErrorResponse(); + } + _logger.ErrorException("Unexpected error", exception); + return null; + } + } +} \ No newline at end of file diff --git a/NzbDrone.Api/NzbDrone.Api.csproj b/NzbDrone.Api/NzbDrone.Api.csproj index c2a22bebd..b4ea6f09a 100644 --- a/NzbDrone.Api/NzbDrone.Api.csproj +++ b/NzbDrone.Api/NzbDrone.Api.csproj @@ -72,6 +72,10 @@ False ..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll + + False + ..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll + @@ -81,7 +85,11 @@ + + + + diff --git a/NzbDrone.Api/QualityType/RequestExtensions.cs b/NzbDrone.Api/QualityType/RequestExtensions.cs index d7afc8ff0..f14c3f7d8 100644 --- a/NzbDrone.Api/QualityType/RequestExtensions.cs +++ b/NzbDrone.Api/QualityType/RequestExtensions.cs @@ -21,10 +21,10 @@ public static T FromJson(this Stream body) }); } - public static Response AsResponse(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK) + public static JsonResponse AsResponse(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK) { ISerializer serializer = new DefaultJsonSerializer(); - var jsonResponse = new JsonResponse(model, serializer) {StatusCode = statusCode}; + var jsonResponse = new JsonResponse(model, serializer) { StatusCode = statusCode }; return jsonResponse; } } diff --git a/NzbDrone.Api/packages.config b/NzbDrone.Api/packages.config index aee7f94d0..b3fd32088 100644 --- a/NzbDrone.Api/packages.config +++ b/NzbDrone.Api/packages.config @@ -6,4 +6,5 @@ + \ No newline at end of file diff --git a/NzbDrone.Core/CentralDispatch.cs b/NzbDrone.Core/CentralDispatch.cs index 87210a0b0..6627cc4e3 100644 --- a/NzbDrone.Core/CentralDispatch.cs +++ b/NzbDrone.Core/CentralDispatch.cs @@ -1,109 +1,30 @@ using System; -using System.Collections.Generic; using System.Diagnostics; -using System.IO; using System.Linq; using Autofac; -using Autofac.Core; using NLog; using NzbDrone.Common; using NzbDrone.Core.Instrumentation; -using NzbDrone.Core.Jobs; -using NzbDrone.Core.Providers; using NzbDrone.Core.Providers.Core; -using NzbDrone.Core.Providers.ExternalNotification; -using NzbDrone.Core.Providers.Indexer; -using NzbDrone.Core.Providers.Metadata; -using NzbDrone.Core.Repository; -using PetaPoco; using SignalR; -using Connection = NzbDrone.Core.Datastore.Connection; namespace NzbDrone.Core { public class CentralDispatch { - private static readonly Logger logger = LogManager.GetCurrentClassLogger(); + private readonly Logger _logger; private readonly EnvironmentProvider _environmentProvider; public ContainerBuilder ContainerBuilder { get; private set; } public CentralDispatch() { + _logger = LogManager.GetCurrentClassLogger(); _environmentProvider = new EnvironmentProvider(); - logger.Debug("Initializing ContainerBuilder:"); + _logger.Debug("Initializing ContainerBuilder:"); ContainerBuilder = new ContainerBuilder(); - ContainerBuilder.RegisterAssemblyTypes(typeof(DiskProvider).Assembly).SingleInstance(); - ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly).SingleInstance(); - ContainerBuilder.RegisterType(); - - InitDatabase(); - RegisterExternalNotifications(); - RegisterMetadataProviders(); - RegisterIndexers(); - RegisterJobs(); - } - - private void InitDatabase() - { - logger.Info("Registering Database..."); - - var appDataPath = _environmentProvider.GetAppDataPath(); - if (!Directory.Exists(appDataPath)) Directory.CreateDirectory(appDataPath); - - ContainerBuilder.Register(c => c.Resolve().GetMainPetaPocoDb()) - .As(); - - ContainerBuilder.Register(c => c.Resolve().GetLogPetaPocoDb(false)) - .SingleInstance() - .Named("DatabaseTarget"); - - ContainerBuilder.Register(c => c.Resolve().GetLogPetaPocoDb()) - .Named("LogProvider"); - - ContainerBuilder.RegisterType().WithParameter(ResolvedParameter.ForNamed("DatabaseTarget")); - ContainerBuilder.RegisterType().WithParameter(ResolvedParameter.ForNamed("LogProvider")); - } - - private void RegisterIndexers() - { - logger.Debug("Registering Indexers..."); - - ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly) - .Where(t => t.BaseType == typeof(IndexerBase)) - .As(); - } - - private void RegisterJobs() - { - logger.Debug("Registering Background Jobs..."); - - ContainerBuilder.RegisterType().SingleInstance(); - - ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly) - .Where(t => t.GetInterfaces().Contains(typeof(IJob))) - .As() - .SingleInstance(); - } - - private void RegisterExternalNotifications() - { - logger.Debug("Registering External Notifications..."); - - ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly) - .Where(t => t.BaseType == typeof(ExternalNotificationBase)) - .As(); - } - - private void RegisterMetadataProviders() - { - logger.Debug("Registering Metadata Providers..."); - - ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly) - .Where(t => t.IsSubclassOf(typeof(MetadataBase))) - .As(); } private void RegisterReporting(IContainer container) @@ -113,12 +34,6 @@ private void RegisterReporting(IContainer container) ReportingService.SetupExceptronDriver(); } - private void RegisterQuality(IContainer container) - { - logger.Debug("Initializing Quality..."); - container.Resolve().SetupDefaultProfiles(); - container.Resolve().SetupDefault(); - } public void DedicateToHost() { @@ -126,67 +41,49 @@ public void DedicateToHost() { var pid = _environmentProvider.NzbDroneProcessIdFromEnviroment; - logger.Debug("Attaching to parent process ({0}) for automatic termination.", pid); + _logger.Debug("Attaching to parent process ({0}) for automatic termination.", pid); var hostProcess = Process.GetProcessById(Convert.ToInt32(pid)); hostProcess.EnableRaisingEvents = true; hostProcess.Exited += (delegate { - logger.Info("Host has been terminated. Shutting down web server."); + _logger.Info("Host has been terminated. Shutting down web server."); ShutDown(); }); - logger.Debug("Successfully Attached to host. Process [{0}]", hostProcess.ProcessName); + _logger.Debug("Successfully Attached to host. Process [{0}]", hostProcess.ProcessName); } catch (Exception e) { - logger.FatalException("An error has occurred while dedicating to host.", e); + _logger.FatalException("An error has occurred while dedicating to host.", e); } } public IContainer BuildContainer() { - var container = ContainerBuilder.Build(); + _logger.Debug("Initializing Components"); - logger.Debug("Initializing Components"); + ContainerBuilder.RegisterCoreServices(); + + var container = ContainerBuilder.Build(); container.Resolve().Register(); LogConfiguration.Reload(); RegisterReporting(container); - RegisterQuality(container); - var indexers = container.Resolve>(); - container.Resolve().InitializeIndexers(indexers.ToList()); - - var newznabIndexers = new List - { - new NewznabDefinition { Enable = false, Name = "Nzbs.org", Url = "http://nzbs.org", BuiltIn = true }, - new NewznabDefinition { Enable = false, Name = "Nzb.su", Url = "https://nzb.su", BuiltIn = true }, - new NewznabDefinition { Enable = false, Name = "Dognzb.cr", Url = "https://dognzb.cr", BuiltIn = true } - }; - - container.Resolve().InitializeNewznabIndexers(newznabIndexers); - - container.Resolve().Initialize(); container.Resolve().StartTimer(30); - var notifiers = container.Resolve>(); - container.Resolve().InitializeNotifiers(notifiers.ToList()); - - var providers = container.Resolve>(); - container.Resolve().Initialize(providers.ToList()); - //SignalR GlobalHost.DependencyResolver = new AutofacSignalrDependencyResolver(container.BeginLifetimeScope("SignalR")); return container; } - private static void ShutDown() + private void ShutDown() { - logger.Info("Shutting down application..."); + _logger.Info("Shutting down application..."); WebTimer.Stop(); Process.GetCurrentProcess().Kill(); } diff --git a/NzbDrone.Core/ContainerExtentions.cs b/NzbDrone.Core/ContainerExtentions.cs new file mode 100644 index 000000000..a3ab7b4d7 --- /dev/null +++ b/NzbDrone.Core/ContainerExtentions.cs @@ -0,0 +1,86 @@ +using System.IO; +using System.Linq; +using System.Reflection; +using Autofac; +using Autofac.Core; +using NLog; +using NzbDrone.Common; +using NzbDrone.Core.Datastore; +using NzbDrone.Core.Instrumentation; +using NzbDrone.Core.Providers.ExternalNotification; +using NzbDrone.Core.Providers.Indexer; +using NzbDrone.Core.Providers.Metadata; +using NzbDrone.Core.Providers.Search; +using PetaPoco; + +namespace NzbDrone.Core +{ + public static class ContainerExtentions + { + + private static Logger _logger = LogManager.GetLogger("ServiceRegistration"); + + public static void RegisterCoreServices(this ContainerBuilder container) + { + var core = Assembly.Load("NzbDrone.Core"); + var common = Assembly.Load("NzbDrone.Common"); + + + container.RegisterAssembly(core); + container.RegisterAssembly(common); + + container.InitDatabase(); + } + + + private static void RegisterAssembly(this ContainerBuilder container, Assembly assembly) + { + container.RegisterAssemblyTypes(assembly) + .AsSelf() + .SingleInstance(); + + container.RegisterAssemblyTypes(assembly) + .AsImplementedInterfaces() + .SingleInstance(); + + container.RegisterAssemblyTypes(assembly) + .Where(t => t.BaseType == typeof(IndexerBase)) + .As().SingleInstance(); + + container.RegisterAssemblyTypes(assembly) + .Where(t => t.BaseType == typeof(SearchBase)) + .As().SingleInstance(); + + container.RegisterAssemblyTypes(assembly) + .Where(t => t.BaseType == typeof(ExternalNotificationBase)) + .As().SingleInstance(); + + container.RegisterAssemblyTypes(assembly) + .Where(t => t.BaseType == typeof(MetadataBase)) + .As().SingleInstance(); + } + + + + private static void InitDatabase(this ContainerBuilder container) + { + _logger.Info("Registering Database..."); + + var appDataPath = new EnvironmentProvider().GetAppDataPath(); + if (!Directory.Exists(appDataPath)) Directory.CreateDirectory(appDataPath); + + container.Register(c => c.Resolve().GetMainPetaPocoDb()) + .As(); + + container.Register(c => c.Resolve().GetLogPetaPocoDb(false)) + .SingleInstance() + .Named("DatabaseTarget"); + + container.Register(c => c.Resolve().GetLogPetaPocoDb()) + .Named("LogProvider"); + + container.RegisterType().WithParameter(ResolvedParameter.ForNamed("DatabaseTarget")); + container.RegisterType().WithParameter(ResolvedParameter.ForNamed("LogProvider")); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/Jobs/JobProvider.cs b/NzbDrone.Core/Jobs/JobProvider.cs index 0c23f9280..544a7c342 100644 --- a/NzbDrone.Core/Jobs/JobProvider.cs +++ b/NzbDrone.Core/Jobs/JobProvider.cs @@ -1,5 +1,3 @@ -//https://github.com/kayone/NzbDrone/blob/master/NzbDrone.Core/Providers/Jobs/JobProvider.cs - using System; using System.Collections.Generic; using System.ComponentModel; @@ -29,7 +27,7 @@ public class JobProvider private Thread _jobThread; public Stopwatch StopWatch { get; private set; } - private readonly object executionLock = new object(); + private readonly object _executionLock = new object(); private readonly List _queue = new List(); private ProgressNotification _notification; @@ -42,6 +40,7 @@ public JobProvider(IDatabase database, NotificationProvider notificationProvider _notificationProvider = notificationProvider; _jobs = jobs; ResetThread(); + Initialize(); } /// @@ -67,7 +66,7 @@ public virtual List All() return _database.Fetch().ToList(); } - public virtual void Initialize() + private void Initialize() { var currentJobs = All(); logger.Debug("Initializing jobs. Available: {0} Existing:{1}", _jobs.Count(), currentJobs.Count); @@ -121,7 +120,7 @@ public virtual void SaveDefinition(JobDefinition definitions) public virtual void QueueScheduled() { - lock (executionLock) + lock (_executionLock) { VerifyThreadTime(); @@ -153,7 +152,7 @@ public virtual void QueueJob(Type jobType, dynamic options = null, JobQueueItem. logger.Debug("Attempting to queue {0}", queueItem); - lock (executionLock) + lock (_executionLock) { VerifyThreadTime(); @@ -215,7 +214,7 @@ private void ProcessQueue() { if (Queue.Count != 0) { - job = Queue.OrderBy(c=>c.Source).First(); + job = Queue.OrderBy(c => c.Source).First(); logger.Trace("Popping {0} from the queue.", job); Queue.Remove(job); } diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index ea837fe2e..63e862f90 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -222,6 +222,7 @@ Properties\SharedAssemblyInfo.cs + @@ -613,6 +614,7 @@ + diff --git a/NzbDrone.Core/Properties/AssemblyInfo.cs b/NzbDrone.Core/Properties/AssemblyInfo.cs index 0445cde9a..3e61062d3 100644 --- a/NzbDrone.Core/Properties/AssemblyInfo.cs +++ b/NzbDrone.Core/Properties/AssemblyInfo.cs @@ -18,4 +18,4 @@ // by using the '*' as shown below: [assembly: AssemblyVersion("1.0.0.*")] -[assembly: AssemblyFileVersion("1.0.0.*")] \ No newline at end of file +[assembly: AssemblyFileVersion("1.0.0.*")] diff --git a/NzbDrone.Core/Providers/ExternalNotificationProvider.cs b/NzbDrone.Core/Providers/ExternalNotificationProvider.cs index e17c50b8b..4dcb261ca 100644 --- a/NzbDrone.Core/Providers/ExternalNotificationProvider.cs +++ b/NzbDrone.Core/Providers/ExternalNotificationProvider.cs @@ -14,12 +14,14 @@ public class ExternalNotificationProvider private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private readonly IDatabase _database; - private IEnumerable _notifiers; + private IList _notifiers; public ExternalNotificationProvider(IDatabase database, IEnumerable notifiers) { _database = database; - _notifiers = notifiers; + _notifiers = notifiers.ToList(); + + InitializeNotifiers(_notifiers); } public ExternalNotificationProvider() @@ -58,7 +60,7 @@ public virtual IList GetEnabledExternalNotifiers() return _notifiers.Where(i => all.Exists(c => c.ExternalNotificationProviderType == i.GetType().ToString() && c.Enable)).ToList(); } - public virtual void InitializeNotifiers(IList notifiers) + private void InitializeNotifiers(IList notifiers) { Logger.Debug("Initializing notifiers. Count {0}", notifiers.Count); diff --git a/NzbDrone.Core/Providers/IndexerProvider.cs b/NzbDrone.Core/Providers/IndexerProvider.cs index 8c3703121..acb487fe3 100644 --- a/NzbDrone.Core/Providers/IndexerProvider.cs +++ b/NzbDrone.Core/Providers/IndexerProvider.cs @@ -13,12 +13,14 @@ public class IndexerProvider private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private readonly IDatabase _database; - private IEnumerable _indexers; + private IList _indexers; public IndexerProvider(IDatabase database, IEnumerable indexers) { _database = database; - _indexers = indexers; + _indexers = indexers.ToList(); + InitializeIndexers(); + } public IndexerProvider() @@ -56,15 +58,14 @@ public virtual IndexerDefinition GetSettings(Type type) return _database.Single("WHERE IndexProviderType = @0", type.ToString()); } - public virtual void InitializeIndexers(IList indexers) + private void InitializeIndexers() { - Logger.Debug("Initializing indexers. Count {0}", indexers.Count); + Logger.Debug("Initializing indexers. Count {0}", _indexers.Count); - _indexers = indexers; var currentIndexers = All(); - foreach (var feedProvider in indexers) + foreach (var feedProvider in _indexers) { IndexerBase indexerLocal = feedProvider; if (!currentIndexers.Exists(c => c.IndexProviderType == indexerLocal.GetType().ToString())) diff --git a/NzbDrone.Core/Providers/MetadataProvider.cs b/NzbDrone.Core/Providers/MetadataProvider.cs index b5ca8146e..c0ebde1ac 100644 --- a/NzbDrone.Core/Providers/MetadataProvider.cs +++ b/NzbDrone.Core/Providers/MetadataProvider.cs @@ -16,15 +16,17 @@ public class MetadataProvider private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private readonly IDatabase _database; - private IEnumerable _metadataProviders; + private IList _metadataProviders; private readonly TvDbProvider _tvDbProvider; public MetadataProvider(IDatabase database, IEnumerable metadataProviders, TvDbProvider tvDbProvider) { _database = database; - _metadataProviders = metadataProviders; + _metadataProviders = metadataProviders.ToList(); _tvDbProvider = tvDbProvider; + + Initialize(_metadataProviders); } public MetadataProvider() @@ -63,7 +65,7 @@ public virtual IList GetEnabledMetabaseProviders() return _metadataProviders.Where(i => all.Exists(c => c.MetadataProviderType == i.GetType().ToString() && c.Enable)).ToList(); } - public virtual void Initialize(IList metabaseProviders) + private void Initialize(IList metabaseProviders) { Logger.Debug("Initializing metabases. Count {0}", metabaseProviders.Count); diff --git a/NzbDrone.Core/Providers/NewznabProvider.cs b/NzbDrone.Core/Providers/NewznabProvider.cs index 5c91de71a..cd3642aaf 100644 --- a/NzbDrone.Core/Providers/NewznabProvider.cs +++ b/NzbDrone.Core/Providers/NewznabProvider.cs @@ -17,6 +17,15 @@ public class NewznabProvider public NewznabProvider(IDatabase database) { _database = database; + + var newznabIndexers = new List + { + new NewznabDefinition { Enable = false, Name = "Nzbs.org", Url = "http://nzbs.org", BuiltIn = true }, + new NewznabDefinition { Enable = false, Name = "Nzb.su", Url = "https://nzb.su", BuiltIn = true }, + new NewznabDefinition { Enable = false, Name = "Dognzb.cr", Url = "https://dognzb.cr", BuiltIn = true } + }; + + InitializeNewznabIndexers(newznabIndexers); } public NewznabProvider() @@ -55,7 +64,7 @@ public virtual void SaveAll(IEnumerable definitions) var definitionsList = definitions.ToList(); //Cleanup the URL for each definition - foreach(var newznabDefinition in definitionsList) + foreach (var newznabDefinition in definitionsList) { CheckHostname(newznabDefinition.Url); //newznabDefinition.Url = new Uri(newznabDefinition.Url).ParentUriString(); @@ -64,7 +73,7 @@ public virtual void SaveAll(IEnumerable definitions) _database.UpdateMany(definitionsList); } - public virtual void InitializeNewznabIndexers(IList indexers) + private void InitializeNewznabIndexers(IList indexers) { Logger.Debug("Initializing Newznab indexers. Count {0}", indexers.Count); @@ -78,7 +87,7 @@ public virtual void InitializeNewznabIndexers(IList indexers) currentIndexers = All(); - foreach(var feedProvider in indexers) + foreach (var feedProvider in indexers) { try { @@ -115,7 +124,7 @@ public virtual void InitializeNewznabIndexers(IList indexers) } } } - catch(Exception ex) + catch (Exception ex) { Logger.ErrorException("An Error occurred while initializing Newznab Indexers", ex); } diff --git a/NzbDrone.Core/Providers/QualityProvider.cs b/NzbDrone.Core/Providers/QualityProvider.cs index a1240276d..82410935a 100644 --- a/NzbDrone.Core/Providers/QualityProvider.cs +++ b/NzbDrone.Core/Providers/QualityProvider.cs @@ -19,6 +19,7 @@ public QualityProvider() public QualityProvider(IDatabase database) { _database = database; + SetupDefaultProfiles(); } public virtual int Add(QualityProfile profile) @@ -54,7 +55,7 @@ public virtual QualityProfile Get(int profileId) return _database.Single(profileId); } - public virtual void SetupDefaultProfiles() + private void SetupDefaultProfiles() { if (All().Count != 0) return; diff --git a/NzbDrone.Core/Providers/QualityTypeProvider.cs b/NzbDrone.Core/Providers/QualityTypeProvider.cs index fba4d417c..3b4086feb 100644 --- a/NzbDrone.Core/Providers/QualityTypeProvider.cs +++ b/NzbDrone.Core/Providers/QualityTypeProvider.cs @@ -17,11 +17,12 @@ public class QualityTypeProvider public QualityTypeProvider(IDatabase database) { _database = database; + SetupDefault(); } public QualityTypeProvider() { - + } public virtual void Update(QualityType qualityType) @@ -52,13 +53,13 @@ public virtual List GetList(List qualityTypeIds) return _database.Fetch(query); } - public virtual void SetupDefault() + private void SetupDefault() { var inDb = All(); Logger.Debug("Setting up default quality types"); - foreach(var qualityType in QualityTypes.All()) + foreach (var qualityType in QualityTypes.All()) { //Skip UNKNOWN if (qualityType.Id == 0) continue; diff --git a/NzbDrone.Web/Global.asax.cs b/NzbDrone.Web/Global.asax.cs index 36a8bc371..4c158a2dd 100644 --- a/NzbDrone.Web/Global.asax.cs +++ b/NzbDrone.Web/Global.asax.cs @@ -1,7 +1,6 @@ using System; using System.Data.Common; using System.Linq; -using System.Reflection; using System.Threading; using System.Web; using System.Web.Mvc; @@ -9,16 +8,10 @@ using Autofac; using Autofac.Integration.Mvc; using LowercaseRoutesMVC; -using NLog.Config; using NLog; -using NzbDrone.Api; -using NzbDrone.Common; using NzbDrone.Core; -using ServiceStack.CacheAccess; -using ServiceStack.CacheAccess.Providers; using NzbDrone.Core.Repository.Quality; using NzbDrone.Web.Helpers.Binders; -using ServiceStack.ServiceInterface; using SignalR; namespace NzbDrone.Web @@ -82,9 +75,6 @@ private void InitContainer() //SignalR RouteTable.Routes.MapHubs(); - //ServiceStack - dispatch.ContainerBuilder.RegisterType().As().SingleInstance(); - dispatch.ContainerBuilder.RegisterType().As().SingleInstance(); } private static void MVCRegistration(ContainerBuilder builder) diff --git a/NzbDrone.Web/NzbDrone.Web.csproj b/NzbDrone.Web/NzbDrone.Web.csproj index 54959030f..6fdcc3518 100644 --- a/NzbDrone.Web/NzbDrone.Web.csproj +++ b/NzbDrone.Web/NzbDrone.Web.csproj @@ -107,38 +107,6 @@ ..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll - - False - ..\packages\ServiceStack.3.9.25\lib\net35\ServiceStack.dll - - - False - ..\packages\ServiceStack.Common.3.9.25\lib\net35\ServiceStack.Common.dll - - - False - ..\packages\ServiceStack.Mvc.3.9.25\lib\net40\ServiceStack.FluentValidation.Mvc3.dll - - - False - ..\packages\ServiceStack.Common.3.9.25\lib\net35\ServiceStack.Interfaces.dll - - - False - ..\packages\ServiceStack.OrmLite.SqlServer.3.9.26\lib\ServiceStack.OrmLite.dll - - - False - ..\packages\ServiceStack.OrmLite.SqlServer.3.9.26\lib\ServiceStack.OrmLite.SqlServer.dll - - - False - ..\packages\ServiceStack.Redis.3.9.25\lib\net35\ServiceStack.Redis.dll - - - False - ..\packages\ServiceStack.3.9.25\lib\net35\ServiceStack.ServiceInterface.dll - False ..\packages\ServiceStack.Text.3.9.27\lib\net35\ServiceStack.Text.dll diff --git a/NzbDrone.Web/packages.config b/NzbDrone.Web/packages.config index 8ace939a0..0d7d3f721 100644 --- a/NzbDrone.Web/packages.config +++ b/NzbDrone.Web/packages.config @@ -28,11 +28,6 @@ - - - - -