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