1
0
Fork 0
mirror of https://github.com/Sonarr/Sonarr synced 2024-12-30 19:56:54 +00:00

Fixed: Slower daemon startup loop if Sonarr runs into non-recoverable errors such as unwritable pid/appfolder/config file.

This commit is contained in:
Taloth Saldono 2017-07-28 23:40:12 +02:00
parent f7f155be1f
commit 84d2d6a1d5
6 changed files with 112 additions and 14 deletions

View file

@ -3,6 +3,7 @@ using System.Security.AccessControl;
using System.Security.Principal; using System.Security.Principal;
using NLog; using NLog;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Common.Exceptions;
using NzbDrone.Common.Instrumentation; using NzbDrone.Common.Instrumentation;
namespace NzbDrone.Common.EnvironmentInfo namespace NzbDrone.Common.EnvironmentInfo
@ -33,6 +34,11 @@ namespace NzbDrone.Common.EnvironmentInfo
{ {
SetPermissions(); SetPermissions();
} }
if (!_diskProvider.FolderWritable(_appFolderInfo.AppDataFolder))
{
throw new SonarrStartupException("AppFolder {0} is not writable", _appFolderInfo.AppDataFolder);
}
} }
private void SetPermissions() private void SetPermissions()

View file

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Common.Exceptions
{
public class SonarrStartupException : NzbDroneException
{
public SonarrStartupException(string message, params object[] args)
: base("Sonarr failed to start: " + string.Format(message, args))
{
}
public SonarrStartupException(string message)
: base("Sonarr failed to start: " + message)
{
}
public SonarrStartupException()
: base("Sonarr failed to start")
{
}
public SonarrStartupException(Exception innerException, string message, params object[] args)
: base("Sonarr failed to start: " + string.Format(message, args), innerException)
{
}
public SonarrStartupException(Exception innerException, string message)
: base("Sonarr failed to start: " + message, innerException)
{
}
public SonarrStartupException(Exception innerException)
: base("Sonarr failed to start: " + innerException.Message)
{
}
}
}

View file

@ -92,6 +92,7 @@
<Compile Include="EnvironmentInfo\IOsVersionAdapter.cs" /> <Compile Include="EnvironmentInfo\IOsVersionAdapter.cs" />
<Compile Include="EnvironmentInfo\IPlatformInfo.cs" /> <Compile Include="EnvironmentInfo\IPlatformInfo.cs" />
<Compile Include="EnvironmentInfo\OsVersionModel.cs" /> <Compile Include="EnvironmentInfo\OsVersionModel.cs" />
<Compile Include="Exceptions\SonarrStartupException.cs" />
<Compile Include="Extensions\DictionaryExtensions.cs" /> <Compile Include="Extensions\DictionaryExtensions.cs" />
<Compile Include="Disk\OsPath.cs" /> <Compile Include="Disk\OsPath.cs" />
<Compile Include="Disk\DiskProviderBase.cs" /> <Compile Include="Disk\DiskProviderBase.cs" />

View file

@ -2,6 +2,7 @@
using System.IO; using System.IO;
using NLog; using NLog;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Exceptions;
namespace NzbDrone.Common.Processes namespace NzbDrone.Common.Processes
{ {
@ -38,7 +39,7 @@ namespace NzbDrone.Common.Processes
catch (Exception ex) catch (Exception ex)
{ {
_logger.Error(ex, "Unable to write PID file {0}", filename); _logger.Error(ex, "Unable to write PID file {0}", filename);
throw; throw new SonarrStartupException(ex, "Unable to write PID file {0}", filename);
} }
} }
} }

View file

@ -2,6 +2,7 @@
using System.Net.Sockets; using System.Net.Sockets;
using NLog; using NLog;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Exceptions;
using NzbDrone.Common.Instrumentation; using NzbDrone.Common.Instrumentation;
using NzbDrone.Host; using NzbDrone.Host;
@ -11,6 +12,14 @@ namespace NzbDrone.Console
{ {
private static readonly Logger Logger = NzbDroneLogger.GetLogger(typeof(ConsoleApp)); private static readonly Logger Logger = NzbDroneLogger.GetLogger(typeof(ConsoleApp));
private enum ExitCodes : int
{
Normal = 0,
UnknownFailure = 1,
RecoverableFailure = 2,
NonRecoverableFailure = 3
}
public static void Main(string[] args) public static void Main(string[] args)
{ {
try try
@ -19,30 +28,61 @@ namespace NzbDrone.Console
NzbDroneLogger.Register(startupArgs, false, true); NzbDroneLogger.Register(startupArgs, false, true);
Bootstrap.Start(startupArgs, new ConsoleAlerts()); Bootstrap.Start(startupArgs, new ConsoleAlerts());
} }
catch (SocketException exception) catch (SonarrStartupException ex)
{ {
System.Console.WriteLine(""); System.Console.WriteLine("");
System.Console.WriteLine(""); System.Console.WriteLine("");
Logger.Fatal(exception.Message + ". This can happen if another instance of Sonarr is already running another application is using the same port (default: 8989) or the user has insufficient permissions"); Logger.Fatal(ex, "EPIC FAIL!");
System.Console.WriteLine("Press enter to exit..."); Exit(ExitCodes.NonRecoverableFailure);
System.Console.ReadLine();
Environment.Exit(1);
} }
catch (Exception e) catch (SocketException ex)
{ {
System.Console.WriteLine(""); System.Console.WriteLine("");
System.Console.WriteLine(""); System.Console.WriteLine("");
Logger.Fatal(e, "EPIC FAIL!"); Logger.Fatal(ex.Message + ". This can happen if another instance of Sonarr is already running another application is using the same port (default: 8989) or the user has insufficient permissions");
System.Console.WriteLine("Press enter to exit..."); Exit(ExitCodes.RecoverableFailure);
System.Console.ReadLine(); }
Environment.Exit(1); catch (Exception ex)
{
System.Console.WriteLine("");
System.Console.WriteLine("");
Logger.Fatal(ex, "EPIC FAIL!");
Exit(ExitCodes.UnknownFailure);
} }
Logger.Info("Exiting main."); Logger.Info("Exiting main.");
Exit(ExitCodes.Normal);
}
private static void Exit(ExitCodes exitCode)
{
LogManager.Flush();
if (exitCode != ExitCodes.Normal)
{
System.Console.WriteLine("Press enter to exit...");
System.Threading.Thread.Sleep(1000);
if (exitCode == ExitCodes.NonRecoverableFailure)
{
System.Console.WriteLine("Non-recoverable failure, waiting for user intervention...");
for (int i = 0; i < 3600; i++)
{
System.Threading.Thread.Sleep(1000);
if (System.Console.KeyAvailable) break;
}
}
// Please note that ReadLine silently succeeds if there is no console, KeyAvailable does not.
System.Console.ReadLine();
}
//Need this to terminate on mono (thanks nlog) //Need this to terminate on mono (thanks nlog)
LogManager.Configuration = null; LogManager.Configuration = null;
Environment.Exit(0); Environment.Exit((int)exitCode);
} }
} }
} }

View file

@ -4,9 +4,11 @@ using System.Threading;
using NLog; using NLog;
using NzbDrone.Common.Composition; using NzbDrone.Common.Composition;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Exceptions;
using NzbDrone.Common.Instrumentation; using NzbDrone.Common.Instrumentation;
using NzbDrone.Common.Processes; using NzbDrone.Common.Processes;
using NzbDrone.Common.Security; using NzbDrone.Common.Security;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
using NzbDrone.Core.Instrumentation; using NzbDrone.Core.Instrumentation;
@ -49,9 +51,13 @@ namespace NzbDrone.Host
SpinToExit(appMode); SpinToExit(appMode);
} }
} }
catch (TerminateApplicationException e) catch (InvalidConfigFileException ex)
{ {
Logger.Info(e.Message); throw new SonarrStartupException(ex);
}
catch (TerminateApplicationException ex)
{
Logger.Info(ex.Message);
LogManager.Configuration = null; LogManager.Configuration = null;
} }
} }