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