Radarr/NzbDrone.Common/IISProvider.cs

127 lines
4.4 KiB
C#
Raw Normal View History

using System.Linq;
using System;
2010-10-15 07:10:44 +00:00
using System.Diagnostics;
using NLog;
namespace NzbDrone.Common
2010-10-15 07:10:44 +00:00
{
2011-10-07 06:36:04 +00:00
public class IISProvider
2010-10-15 07:10:44 +00:00
{
2012-02-11 00:48:20 +00:00
private static readonly Logger IISLogger = LogManager.GetCurrentClassLogger();
2012-01-23 02:24:16 +00:00
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2011-11-13 07:27:16 +00:00
private readonly ConfigFileProvider _configFileProvider;
2011-10-07 06:57:43 +00:00
private readonly ProcessProvider _processProvider;
private readonly EnvironmentProvider _environmentProvider;
2011-10-07 06:36:04 +00:00
public int IISProcessId { get; private set; }
2011-10-07 03:37:41 +00:00
2011-10-07 06:36:04 +00:00
public bool ServerStarted { get; private set; }
2011-10-07 03:37:41 +00:00
2011-10-07 06:36:04 +00:00
public void StartServer()
2010-10-15 07:10:44 +00:00
{
Logger.Info("Preparing IISExpress Server...");
2011-10-07 06:36:04 +00:00
var startInfo = new ProcessStartInfo();
2010-10-15 07:10:44 +00:00
startInfo.FileName = _environmentProvider.GetIISExe();
startInfo.Arguments = String.Format("/config:\"{0}\" /trace:i", _environmentProvider.GetIISConfigPath());
startInfo.WorkingDirectory = _environmentProvider.ApplicationPath;
2011-10-07 06:36:04 +00:00
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
startInfo.EnvironmentVariables[EnvironmentProvider.NZBDRONE_PATH] = _environmentProvider.ApplicationPath;
startInfo.EnvironmentVariables[EnvironmentProvider.NZBDRONE_PID] = Process.GetCurrentProcess().Id.ToString();
try
{
_configFileProvider.UpdateIISConfig(_environmentProvider.GetIISConfigPath());
}
catch (Exception e)
{
Logger.ErrorException("An error has occurred while trying to update the config file.", e);
}
2011-10-07 06:36:04 +00:00
var iisProcess = _processProvider.Start(startInfo);
IISProcessId = iisProcess.Id;
2010-10-15 07:10:44 +00:00
2011-10-07 06:36:04 +00:00
iisProcess.OutputDataReceived += (OnOutputDataReceived);
iisProcess.ErrorDataReceived += (OnErrorDataReceived);
2011-07-17 20:01:37 +00:00
2011-10-07 06:36:04 +00:00
iisProcess.BeginErrorReadLine();
iisProcess.BeginOutputReadLine();
2010-10-15 07:10:44 +00:00
2011-10-07 06:36:04 +00:00
ServerStarted = true;
iisProcess.EnableRaisingEvents = true;
iisProcess.Exited += IIS_EXITED;
2010-10-15 07:10:44 +00:00
}
2013-01-03 05:11:39 +00:00
public IISProvider(ConfigFileProvider configFileProvider, ProcessProvider processProvider, EnvironmentProvider environmentProvider)
{
_configFileProvider = configFileProvider;
_processProvider = processProvider;
_environmentProvider = environmentProvider;
}
public IISProvider()
{
}
public string AppUrl
{
get { return string.Format("http://localhost:{0}/", _configFileProvider.Port); }
}
2011-04-25 03:51:18 +00: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 06:36:04 +00:00
_processProvider.Kill(IISProcessId);
2011-04-22 06:46:26 +00:00
Logger.Info("Finding orphaned IIS Processes.");
2011-10-07 06:36:04 +00:00
foreach (var process in _processProvider.GetProcessByName("IISExpress"))
2011-04-22 06:46:26 +00:00
{
2011-10-07 06:36:04 +00:00
Logger.Info("[{0}]IIS Process found. Path:{1}", process.Id, process.StartPath);
if (DiskProvider.PathEquals(process.StartPath, _environmentProvider.GetIISExe()))
2011-04-22 06:46:26 +00:00
{
Logger.Info("[{0}]Process is considered orphaned.", process.Id);
2011-10-07 06:36:04 +00:00
_processProvider.Kill(process.Id);
2011-04-22 06:46:26 +00:00
}
else
{
Logger.Info("[{0}]Process has a different start-up path. skipping.", process.Id);
}
}
}
public void IIS_EXITED(object obj, EventArgs args)
{
RestartServer();
}
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 07:10:44 +00:00
}
2011-04-10 02:44:01 +00:00
}