1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-05 10:32:35 +01:00
Radarr/NzbDrone.Common/IISProvider.cs

128 lines
4.2 KiB
C#
Raw Normal View History

using System.Linq;
using System;
2010-10-15 09:10:44 +02:00
using System.Diagnostics;
using NLog;
2011-10-07 08:36:04 +02:00
using Ninject;
2010-10-15 09:10:44 +02:00
namespace NzbDrone.Common
2010-10-15 09:10:44 +02:00
{
2011-10-07 08:36:04 +02:00
public class IISProvider
2010-10-15 09:10:44 +02:00
{
2012-01-23 03:24:16 +01:00
private static readonly Logger IISLogger = LogManager.GetLogger("IISExpress");
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2011-11-13 08:27:16 +01:00
private readonly ConfigFileProvider _configFileProvider;
2011-10-07 08:57:43 +02:00
private readonly ProcessProvider _processProvider;
private readonly EnviromentProvider _enviromentProvider;
2011-10-07 08:36:04 +02:00
[Inject]
2011-11-13 08:27:16 +01:00
public IISProvider(ConfigFileProvider configFileProvider, ProcessProvider processProvider, EnviromentProvider enviromentProvider)
2011-10-07 08:36:04 +02:00
{
2011-11-13 08:27:16 +01:00
_configFileProvider = configFileProvider;
2011-10-07 08:36:04 +02:00
_processProvider = processProvider;
_enviromentProvider = enviromentProvider;
2011-10-07 08:36:04 +02:00
}
2010-10-15 09:10:44 +02:00
2011-10-07 08:36:04 +02:00
public IISProvider()
{
}
2011-10-07 08:36:04 +02:00
public string AppUrl
2010-10-15 09:10:44 +02:00
{
2011-11-13 08:27:16 +01:00
get { return string.Format("http://localhost:{0}/", _configFileProvider.Port); }
2010-10-15 09:10:44 +02:00
}
2011-10-07 08:36:04 +02:00
public int IISProcessId { get; private set; }
2011-10-07 05:37:41 +02:00
2011-10-07 08:36:04 +02:00
public bool ServerStarted { get; private set; }
2011-10-07 05:37:41 +02:00
2011-10-07 08:36:04 +02:00
public void StartServer()
2010-10-15 09:10:44 +02:00
{
Logger.Info("Preparing IISExpress Server...");
2011-10-07 08:36:04 +02:00
var startInfo = new ProcessStartInfo();
2010-10-15 09:10:44 +02:00
2011-11-13 08:27:16 +01:00
startInfo.FileName = _enviromentProvider.GetIISExe();
2011-11-13 22:06:04 +01:00
startInfo.Arguments = String.Format("/config:\"{0}\" /trace:i", _enviromentProvider.GetIISConfigPath());
startInfo.WorkingDirectory = _enviromentProvider.ApplicationPath;
2011-10-07 08:36:04 +02:00
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
startInfo.EnvironmentVariables[EnviromentProvider.NZBDRONE_PATH] = _enviromentProvider.ApplicationPath;
startInfo.EnvironmentVariables[EnviromentProvider.NZBDRONE_PID] = Process.GetCurrentProcess().Id.ToString();
try
{
2011-11-13 08:27:16 +01:00
_configFileProvider.UpdateIISConfig(_enviromentProvider.GetIISConfigPath());
}
catch (Exception e)
{
Logger.ErrorException("An error has occurred while trying to update the config file.", e);
}
2011-10-07 08:36:04 +02:00
var iisProcess = _processProvider.Start(startInfo);
IISProcessId = iisProcess.Id;
2010-10-15 09:10:44 +02:00
2011-10-07 08:36:04 +02:00
iisProcess.OutputDataReceived += (OnOutputDataReceived);
iisProcess.ErrorDataReceived += (OnErrorDataReceived);
2011-07-17 22:01:37 +02:00
2011-10-07 08:36:04 +02:00
iisProcess.BeginErrorReadLine();
iisProcess.BeginOutputReadLine();
2010-10-15 09:10:44 +02:00
2011-10-07 08:36:04 +02:00
ServerStarted = true;
2010-10-15 09:10:44 +02:00
}
2011-04-25 05:51:18 +02:00
private static void OnErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (e == null || String.IsNullOrWhiteSpace(e.Data))
return;
IISLogger.Error(e.Data);
}
public void RestartServer()
{
ServerStarted = false;
Logger.Warn("Attempting to restart server.");
StopServer();
StartServer();
}
public virtual void StopServer()
{
2011-10-07 08:36:04 +02:00
_processProvider.Kill(IISProcessId);
2011-04-22 08:46:26 +02:00
Logger.Info("Finding orphaned IIS Processes.");
2011-10-07 08:36:04 +02:00
foreach (var process in _processProvider.GetProcessByName("IISExpress"))
2011-04-22 08:46:26 +02:00
{
2011-10-07 08:36:04 +02:00
Logger.Info("[{0}]IIS Process found. Path:{1}", process.Id, process.StartPath);
if (DiskProvider.PathEquals(process.StartPath, _enviromentProvider.GetIISExe()))
2011-04-22 08:46:26 +02:00
{
Logger.Info("[{0}]Process is considered orphaned.", process.Id);
2011-10-07 08:36:04 +02:00
_processProvider.Kill(process.Id);
2011-04-22 08:46:26 +02:00
}
else
{
Logger.Info("[{0}]Process has a different start-up path. skipping.", process.Id);
}
}
}
2011-04-22 08:46:26 +02:00
private void OnOutputDataReceived(object s, DataReceivedEventArgs e)
{
if (e == null || String.IsNullOrWhiteSpace(e.Data) || e.Data.StartsWith("Request started:") ||
e.Data.StartsWith("Request ended:") || e.Data == ("IncrementMessages called"))
return;
Console.WriteLine(e.Data);
}
2010-10-15 09:10:44 +02:00
}
2011-04-10 04:44:01 +02:00
}