2017-11-12 11:25:46 +00:00
|
|
|
using System;
|
2021-01-24 22:05:08 +00:00
|
|
|
using System.Diagnostics;
|
2013-11-26 06:53:36 +00:00
|
|
|
using System.Reflection;
|
2020-12-26 03:08:59 +00:00
|
|
|
using System.Text;
|
2013-11-26 06:53:36 +00:00
|
|
|
using System.Threading;
|
|
|
|
using NLog;
|
2013-08-07 05:32:22 +00:00
|
|
|
using NzbDrone.Common.Composition;
|
2013-08-13 05:08:37 +00:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2018-11-23 07:03:32 +00:00
|
|
|
using NzbDrone.Common.Exceptions;
|
2013-05-24 03:23:59 +00:00
|
|
|
using NzbDrone.Common.Instrumentation;
|
2014-03-10 05:35:50 +00:00
|
|
|
using NzbDrone.Common.Processes;
|
2018-11-23 07:03:32 +00:00
|
|
|
using NzbDrone.Core.Configuration;
|
2016-03-21 23:10:27 +00:00
|
|
|
using NzbDrone.Core.Instrumentation;
|
2011-10-14 01:22:51 +00:00
|
|
|
|
2017-01-05 19:42:02 +00:00
|
|
|
namespace Radarr.Host
|
2011-10-14 01:22:51 +00:00
|
|
|
{
|
2013-11-26 06:53:36 +00:00
|
|
|
public static class Bootstrap
|
2011-10-14 01:22:51 +00:00
|
|
|
{
|
2014-12-17 07:12:26 +00:00
|
|
|
private static readonly Logger Logger = NzbDroneLogger.GetLogger(typeof(Bootstrap));
|
2019-12-22 22:08:53 +00:00
|
|
|
private static IContainer _container;
|
2013-11-26 02:46:12 +00:00
|
|
|
|
2013-11-26 06:53:36 +00:00
|
|
|
public static void Start(StartupContext startupContext, IUserAlert userAlert, Action<IContainer> startCallback = null)
|
2011-10-14 01:22:51 +00:00
|
|
|
{
|
2013-11-26 06:53:36 +00:00
|
|
|
try
|
|
|
|
{
|
2021-01-24 22:05:08 +00:00
|
|
|
Logger.Info("Starting Radarr - {0} - Version {1}",
|
|
|
|
Process.GetCurrentProcess().MainModule.FileName,
|
|
|
|
Assembly.GetExecutingAssembly().GetName().Version);
|
2013-11-26 06:53:36 +00:00
|
|
|
|
|
|
|
if (!PlatformValidation.IsValidate(userAlert))
|
|
|
|
{
|
|
|
|
throw new TerminateApplicationException("Missing system requirements");
|
|
|
|
}
|
2013-08-13 05:08:37 +00:00
|
|
|
|
2020-12-26 03:08:59 +00:00
|
|
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
|
|
|
|
2013-11-26 06:53:36 +00:00
|
|
|
_container = MainAppContainerBuilder.BuildContainer(startupContext);
|
2019-09-03 02:22:25 +00:00
|
|
|
_container.Resolve<InitializeLogger>().Initialize();
|
2014-01-06 06:20:08 +00:00
|
|
|
_container.Resolve<IAppFolderFactory>().Register();
|
2014-04-28 05:14:31 +00:00
|
|
|
_container.Resolve<IProvidePidFile>().Write();
|
2012-01-09 22:01:18 +00:00
|
|
|
|
2013-11-26 06:53:36 +00:00
|
|
|
var appMode = GetApplicationMode(startupContext);
|
2013-03-01 00:50:50 +00:00
|
|
|
|
2014-01-14 01:35:16 +00:00
|
|
|
Start(appMode, startupContext);
|
2019-04-13 03:25:58 +00:00
|
|
|
|
2018-02-19 11:48:56 +00:00
|
|
|
_container.Resolve<ICancelHandler>().Attach();
|
2012-01-09 22:01:18 +00:00
|
|
|
|
2013-11-26 06:53:36 +00:00
|
|
|
if (startCallback != null)
|
|
|
|
{
|
|
|
|
startCallback(_container);
|
|
|
|
}
|
2014-01-10 04:32:21 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
SpinToExit(appMode);
|
|
|
|
}
|
2013-11-26 06:53:36 +00:00
|
|
|
}
|
2018-11-23 07:03:32 +00:00
|
|
|
catch (InvalidConfigFileException ex)
|
|
|
|
{
|
|
|
|
throw new RadarrStartupException(ex);
|
|
|
|
}
|
2013-11-26 06:53:36 +00:00
|
|
|
catch (TerminateApplicationException e)
|
2013-08-16 02:20:54 +00:00
|
|
|
{
|
2013-11-26 07:08:12 +00:00
|
|
|
Logger.Info(e.Message);
|
2014-02-10 09:49:06 +00:00
|
|
|
LogManager.Configuration = null;
|
2013-08-16 02:20:54 +00:00
|
|
|
}
|
2013-11-26 06:53:36 +00:00
|
|
|
}
|
2013-08-07 05:32:22 +00:00
|
|
|
|
2014-01-14 01:35:16 +00:00
|
|
|
private static void Start(ApplicationModes applicationModes, StartupContext startupContext)
|
2013-11-26 06:53:36 +00:00
|
|
|
{
|
2016-03-21 23:10:27 +00:00
|
|
|
_container.Resolve<ReconfigureLogging>().Reconfigure();
|
|
|
|
|
2013-11-26 06:53:36 +00:00
|
|
|
if (!IsInUtilityMode(applicationModes))
|
|
|
|
{
|
2014-03-10 05:35:50 +00:00
|
|
|
if (startupContext.Flags.Contains(StartupContext.RESTART))
|
|
|
|
{
|
|
|
|
Thread.Sleep(2000);
|
|
|
|
}
|
|
|
|
|
2014-01-14 01:35:16 +00:00
|
|
|
EnsureSingleInstance(applicationModes == ApplicationModes.Service, startupContext);
|
2013-11-26 06:53:36 +00:00
|
|
|
}
|
2019-12-22 21:24:11 +00:00
|
|
|
|
2013-11-26 06:53:36 +00:00
|
|
|
_container.Resolve<Router>().Route(applicationModes);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void SpinToExit(ApplicationModes applicationModes)
|
|
|
|
{
|
|
|
|
if (IsInUtilityMode(applicationModes))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-10 05:35:50 +00:00
|
|
|
_container.Resolve<IWaitForExit>().Spin();
|
2011-10-14 01:22:51 +00:00
|
|
|
}
|
2013-11-26 02:46:12 +00:00
|
|
|
|
2014-10-03 17:39:06 +00:00
|
|
|
private static void EnsureSingleInstance(bool isService, IStartupContext startupContext)
|
2013-11-26 02:46:12 +00:00
|
|
|
{
|
2020-08-17 21:27:05 +00:00
|
|
|
if (startupContext.Flags.Contains(StartupContext.NO_SINGLE_INSTANCE_CHECK))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-26 07:08:12 +00:00
|
|
|
var instancePolicy = _container.Resolve<ISingleInstancePolicy>();
|
|
|
|
|
2017-07-16 13:08:16 +00:00
|
|
|
if (startupContext.Flags.Contains(StartupContext.TERMINATE))
|
2013-11-26 07:08:12 +00:00
|
|
|
{
|
|
|
|
instancePolicy.KillAllOtherInstance();
|
|
|
|
}
|
2017-07-16 13:08:16 +00:00
|
|
|
else if (startupContext.Args.ContainsKey(StartupContext.APPDATA))
|
|
|
|
{
|
|
|
|
instancePolicy.WarnIfAlreadyRunning();
|
|
|
|
}
|
|
|
|
else if (isService)
|
2014-01-14 01:35:16 +00:00
|
|
|
{
|
|
|
|
instancePolicy.KillAllOtherInstance();
|
|
|
|
}
|
2013-11-26 07:08:12 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
instancePolicy.PreventStartIfAlreadyRunning();
|
|
|
|
}
|
2013-11-26 02:46:12 +00:00
|
|
|
}
|
|
|
|
|
2014-10-03 17:39:06 +00:00
|
|
|
private static ApplicationModes GetApplicationMode(IStartupContext startupContext)
|
2013-11-26 06:53:36 +00:00
|
|
|
{
|
2019-09-03 02:22:25 +00:00
|
|
|
if (startupContext.Help)
|
2013-11-26 06:53:36 +00:00
|
|
|
{
|
|
|
|
return ApplicationModes.Help;
|
|
|
|
}
|
|
|
|
|
2019-09-03 02:22:25 +00:00
|
|
|
if (OsInfo.IsWindows && startupContext.RegisterUrl)
|
|
|
|
{
|
|
|
|
return ApplicationModes.RegisterUrl;
|
|
|
|
}
|
|
|
|
|
2014-12-07 20:54:07 +00:00
|
|
|
if (OsInfo.IsWindows && startupContext.InstallService)
|
2013-11-26 06:53:36 +00:00
|
|
|
{
|
|
|
|
return ApplicationModes.InstallService;
|
|
|
|
}
|
|
|
|
|
2014-12-07 20:54:07 +00:00
|
|
|
if (OsInfo.IsWindows && startupContext.UninstallService)
|
2013-11-26 06:53:36 +00:00
|
|
|
{
|
|
|
|
return ApplicationModes.UninstallService;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_container.Resolve<IRuntimeInfo>().IsWindowsService)
|
|
|
|
{
|
|
|
|
return ApplicationModes.Service;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ApplicationModes.Interactive;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsInUtilityMode(ApplicationModes applicationMode)
|
2013-11-26 02:46:12 +00:00
|
|
|
{
|
2013-11-26 06:53:36 +00:00
|
|
|
switch (applicationMode)
|
|
|
|
{
|
|
|
|
case ApplicationModes.InstallService:
|
|
|
|
case ApplicationModes.UninstallService:
|
2019-09-03 02:22:25 +00:00
|
|
|
case ApplicationModes.RegisterUrl:
|
2013-11-26 06:53:36 +00:00
|
|
|
case ApplicationModes.Help:
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-22 22:08:53 +00:00
|
|
|
|
2013-11-26 06:53:36 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-11-26 02:46:12 +00:00
|
|
|
}
|
2011-10-14 01:22:51 +00:00
|
|
|
}
|
2016-09-13 20:57:07 +00:00
|
|
|
}
|