diff --git a/src/NzbDrone.Console/ConsoleApp.cs b/src/NzbDrone.Console/ConsoleApp.cs index eb75bddc7..665a1c179 100644 --- a/src/NzbDrone.Console/ConsoleApp.cs +++ b/src/NzbDrone.Console/ConsoleApp.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Net.Sockets; using NLog; using NzbDrone.Common.EnvironmentInfo; @@ -11,6 +11,13 @@ namespace NzbDrone.Console { private static readonly Logger Logger = NzbDroneLogger.GetLogger(typeof(ConsoleApp)); + private enum ExitCodes : int + { + Normal = 0, + UnknownFailure = 1, + RecoverableFailure = 2 + } + public static void Main(string[] args) { try @@ -19,30 +26,41 @@ namespace NzbDrone.Console NzbDroneLogger.Register(startupArgs, false, true); Bootstrap.Start(startupArgs, new ConsoleAlerts()); } - catch (SocketException exception) + catch (SocketException e) { System.Console.WriteLine(""); System.Console.WriteLine(""); - Logger.Fatal(exception.Message + ". This can happen if another instance of Radarr is already running another application is using the same port (default: 7878) or the user has insufficient permissions"); - System.Console.WriteLine("Press enter to exit..."); - System.Console.ReadLine(); - Environment.Exit(1); + Logger.Fatal(e.Message + ". This can happen if another instance of Radarr is already running another application is using the same port (default: 7878) or the user has insufficient permissions"); + Exit(ExitCodes.RecoverableFailure); } catch (Exception e) { System.Console.WriteLine(""); System.Console.WriteLine(""); Logger.Fatal(e, "EPIC FAIL!"); - System.Console.WriteLine("Press enter to exit..."); - System.Console.ReadLine(); - Environment.Exit(1); + Exit(ExitCodes.UnknownFailure); } Logger.Info("Exiting main."); - //Need this to terminate on mono (thanks nlog) - LogManager.Configuration = null; - Environment.Exit(0); + Exit(ExitCodes.Normal); + } + + private static void Exit(ExitCodes exitCode) + { + LogManager.Shutdown(); + + if (exitCode != ExitCodes.Normal) + { + System.Console.WriteLine("Press enter to exit..."); + + System.Threading.Thread.Sleep(1000); + + // Please note that ReadLine silently succeeds if there is no console, KeyAvailable does not. + System.Console.ReadLine(); + } + + Environment.Exit((int)exitCode); } } } diff --git a/src/NzbDrone.Host/ApplicationServer.cs b/src/NzbDrone.Host/ApplicationServer.cs index 5491101bc..62803f868 100644 --- a/src/NzbDrone.Host/ApplicationServer.cs +++ b/src/NzbDrone.Host/ApplicationServer.cs @@ -4,6 +4,7 @@ using NLog; using NzbDrone.Common.Composition; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Core.Configuration; +using NzbDrone.Core.Datastore; using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Messaging.Events; using Radarr.Host.Owin; @@ -56,6 +57,7 @@ namespace Radarr.Host } _runtimeInfo.IsRunning = true; + DbFactory.RegisterDatabase(_container); _hostController.StartServer(); if (!_startupContext.Flags.Contains(StartupContext.NO_BROWSER) diff --git a/src/NzbDrone.Host/Bootstrap.cs b/src/NzbDrone.Host/Bootstrap.cs index 2baf8c3d5..8ffbf8b59 100644 --- a/src/NzbDrone.Host/Bootstrap.cs +++ b/src/NzbDrone.Host/Bootstrap.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Reflection; using System.Threading; using NLog; @@ -7,7 +7,6 @@ using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Instrumentation; using NzbDrone.Common.Processes; using NzbDrone.Common.Security; -using NzbDrone.Core.Datastore; using NzbDrone.Core.Instrumentation; namespace Radarr.Host @@ -43,7 +42,6 @@ namespace Radarr.Host { startCallback(_container); } - else { SpinToExit(appMode); @@ -69,8 +67,7 @@ namespace Radarr.Host EnsureSingleInstance(applicationModes == ApplicationModes.Service, startupContext); } - - DbFactory.RegisterDatabase(_container); + _container.Resolve().Route(applicationModes); }