2010-10-15 07:10:44 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2011-04-22 02:23:31 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Timers;
|
2010-10-21 01:49:23 +00:00
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
using System.Xml.XPath;
|
2010-10-15 07:10:44 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone
|
|
|
|
|
{
|
2011-04-10 02:44:01 +00:00
|
|
|
|
internal class IISController
|
2010-10-15 07:10:44 +00:00
|
|
|
|
{
|
|
|
|
|
private static readonly Logger IISLogger = LogManager.GetLogger("IISExpress");
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetLogger("IISController");
|
|
|
|
|
private static readonly string IISFolder = Path.Combine(Config.ProjectRoot, @"IISExpress\");
|
|
|
|
|
private static readonly string IISExe = Path.Combine(IISFolder, @"iisexpress.exe");
|
2011-04-24 03:02:20 +00:00
|
|
|
|
private static readonly string IISConfigPath = Path.Combine(IISFolder, "AppServer", "applicationhost.config");
|
|
|
|
|
|
2011-04-22 02:23:31 +00:00
|
|
|
|
private static Timer _pingTimer;
|
|
|
|
|
private static int _pingFailCounter;
|
|
|
|
|
|
2011-04-10 02:44:01 +00:00
|
|
|
|
public static Process IISProcess { get; private set; }
|
2010-10-15 07:10:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal static string AppUrl
|
|
|
|
|
{
|
|
|
|
|
get { return string.Format("http://localhost:{0}/", Config.Port); }
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-22 06:46:26 +00:00
|
|
|
|
internal static Process StartServer()
|
2010-10-15 07:10:44 +00:00
|
|
|
|
{
|
|
|
|
|
Logger.Info("Preparing IISExpress Server...");
|
|
|
|
|
IISProcess = new Process();
|
|
|
|
|
|
|
|
|
|
IISProcess.StartInfo.FileName = IISExe;
|
2011-04-25 00:43:54 +00:00
|
|
|
|
IISProcess.StartInfo.Arguments = String.Format("/config:\"{0}\" /trace:i", IISConfigPath);//"/config:"""" /trace:i";
|
2010-10-15 07:10:44 +00:00
|
|
|
|
IISProcess.StartInfo.WorkingDirectory = Config.ProjectRoot;
|
|
|
|
|
|
|
|
|
|
IISProcess.StartInfo.UseShellExecute = false;
|
|
|
|
|
IISProcess.StartInfo.RedirectStandardOutput = true;
|
|
|
|
|
IISProcess.StartInfo.RedirectStandardError = true;
|
|
|
|
|
IISProcess.StartInfo.CreateNoWindow = true;
|
|
|
|
|
|
2010-10-17 17:22:48 +00:00
|
|
|
|
|
2011-04-25 07:42:29 +00:00
|
|
|
|
IISProcess.OutputDataReceived += (OnOutputDataReceived);
|
|
|
|
|
IISProcess.ErrorDataReceived += (OnErrorDataReceived);
|
2010-10-17 17:22:48 +00:00
|
|
|
|
|
2010-10-15 07:10:44 +00:00
|
|
|
|
//Set Variables for the config file.
|
|
|
|
|
Environment.SetEnvironmentVariable("NZBDRONE_PATH", Config.ProjectRoot);
|
2010-10-21 01:49:23 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
UpdateIISConfig();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2011-04-20 07:44:13 +00:00
|
|
|
|
Logger.ErrorException("An error has occurred while trying to update the config file.", e);
|
2010-10-21 01:49:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-15 07:10:44 +00:00
|
|
|
|
|
|
|
|
|
Logger.Info("Starting process. [{0}]", IISProcess.StartInfo.FileName);
|
2011-04-25 07:42:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-15 07:10:44 +00:00
|
|
|
|
IISProcess.Start();
|
|
|
|
|
|
|
|
|
|
IISProcess.BeginErrorReadLine();
|
|
|
|
|
IISProcess.BeginOutputReadLine();
|
2011-04-22 02:23:31 +00:00
|
|
|
|
|
2011-04-22 06:46:26 +00:00
|
|
|
|
//Start Ping
|
|
|
|
|
_pingTimer = new Timer(10000) { AutoReset = true };
|
|
|
|
|
_pingTimer.Elapsed += (Server);
|
|
|
|
|
_pingTimer.Start();
|
2011-04-22 02:23:31 +00:00
|
|
|
|
|
2010-10-15 07:10:44 +00:00
|
|
|
|
return IISProcess;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-25 07:42:29 +00:00
|
|
|
|
private static void OnErrorDataReceived(object sender, DataReceivedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e == null || String.IsNullOrWhiteSpace(e.Data))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
IISLogger.Error(e.Data);
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-22 06:46:26 +00:00
|
|
|
|
internal static void StopServer()
|
2011-04-22 02:23:31 +00:00
|
|
|
|
{
|
2011-04-22 06:46:26 +00:00
|
|
|
|
KillProcess(IISProcess);
|
|
|
|
|
|
|
|
|
|
Logger.Info("Finding orphaned IIS Processes.");
|
|
|
|
|
foreach (var process in Process.GetProcessesByName("IISExpress"))
|
|
|
|
|
{
|
|
|
|
|
string processPath = process.MainModule.FileName;
|
|
|
|
|
Logger.Info("[{0}]IIS Process found. Path:{1}", process.Id, processPath);
|
2011-04-25 07:42:29 +00:00
|
|
|
|
if (NormalizePath(processPath) == NormalizePath(IISExe))
|
2011-04-22 06:46:26 +00:00
|
|
|
|
{
|
|
|
|
|
Logger.Info("[{0}]Process is considered orphaned.", process.Id);
|
|
|
|
|
KillProcess(process);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("[{0}]Process has a different start-up path. skipping.", process.Id);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-22 02:23:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-22 06:46:26 +00:00
|
|
|
|
private static void RestartServer()
|
|
|
|
|
{
|
|
|
|
|
_pingTimer.Stop();
|
|
|
|
|
Logger.Warn("Attempting to restart server.");
|
|
|
|
|
StopServer();
|
|
|
|
|
StartServer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void Server(object sender, ElapsedEventArgs e)
|
2011-04-22 02:23:31 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2011-04-22 06:46:26 +00:00
|
|
|
|
new WebClient().DownloadString(AppUrl);
|
2011-04-24 03:02:20 +00:00
|
|
|
|
if (_pingFailCounter > 0)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Application pool has been successfully recovered.");
|
|
|
|
|
}
|
2011-04-22 02:23:31 +00:00
|
|
|
|
_pingFailCounter = 0;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_pingFailCounter++;
|
2011-04-24 03:02:20 +00:00
|
|
|
|
Logger.ErrorException("Application pool is not responding. Count " + _pingFailCounter, ex);
|
2011-04-22 06:46:26 +00:00
|
|
|
|
if (_pingFailCounter > 2)
|
2011-04-22 02:23:31 +00:00
|
|
|
|
{
|
2011-04-22 06:46:26 +00:00
|
|
|
|
RestartServer();
|
2011-04-22 02:23:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-25 07:42:29 +00:00
|
|
|
|
private static void OnOutputDataReceived(object s, DataReceivedEventArgs e)
|
2010-10-17 17:22:48 +00:00
|
|
|
|
{
|
2011-04-24 03:02:20 +00:00
|
|
|
|
if (e == null || String.IsNullOrWhiteSpace(e.Data) || e.Data.StartsWith("Request started:") ||
|
2011-04-10 02:44:01 +00:00
|
|
|
|
e.Data.StartsWith("Request ended:") || e.Data == ("IncrementMessages called"))
|
2010-10-17 17:22:48 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
IISLogger.Trace(e.Data);
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-15 07:10:44 +00:00
|
|
|
|
private static void UpdateIISConfig()
|
|
|
|
|
{
|
2010-10-21 01:49:23 +00:00
|
|
|
|
string configPath = Path.Combine(IISFolder, @"AppServer\applicationhost.config");
|
|
|
|
|
|
|
|
|
|
Logger.Info(@"Server configuration file: {0}", configPath);
|
2010-10-15 07:10:44 +00:00
|
|
|
|
Logger.Info(@"Configuring server to: [http://localhost:{0}]", Config.Port);
|
2010-10-21 01:49:23 +00:00
|
|
|
|
|
|
|
|
|
var configXml = XDocument.Load(configPath);
|
|
|
|
|
|
2011-04-10 02:44:01 +00:00
|
|
|
|
var bindings =
|
|
|
|
|
configXml.XPathSelectElement("configuration/system.applicationHost/sites").Elements("site").Where(
|
|
|
|
|
d => d.Attribute("name").Value.ToLowerInvariant() == "nzbdrone").First().Element("bindings");
|
2010-10-21 01:49:23 +00:00
|
|
|
|
bindings.Descendants().Remove();
|
|
|
|
|
bindings.Add(
|
2011-04-10 02:44:01 +00:00
|
|
|
|
new XElement("binding",
|
|
|
|
|
new XAttribute("protocol", "http"),
|
|
|
|
|
new XAttribute("bindingInformation", String.Format("*:{0}:", Config.Port))
|
|
|
|
|
));
|
2010-10-21 01:49:23 +00:00
|
|
|
|
|
|
|
|
|
configXml.Save(configPath);
|
2010-10-15 07:10:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-22 06:46:26 +00:00
|
|
|
|
private static void KillProcess(Process process)
|
|
|
|
|
{
|
|
|
|
|
if (process != null && !process.HasExited)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("[{0}]Killing process", process.Id);
|
|
|
|
|
process.Kill();
|
|
|
|
|
Logger.Info("[{0}]Waiting for exit", process.Id);
|
|
|
|
|
process.WaitForExit();
|
|
|
|
|
Logger.Info("[{0}]Process terminated successfully", process.Id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-25 07:42:29 +00:00
|
|
|
|
public static string NormalizePath(string path)
|
2010-10-15 07:10:44 +00:00
|
|
|
|
{
|
2011-04-25 07:42:29 +00:00
|
|
|
|
if (String.IsNullOrWhiteSpace(path))
|
|
|
|
|
throw new ArgumentException("Path can not be null or empty");
|
|
|
|
|
|
|
|
|
|
var info = new FileInfo(path);
|
|
|
|
|
|
|
|
|
|
if (info.FullName.StartsWith(@"\\")) //UNC
|
|
|
|
|
{
|
|
|
|
|
return info.FullName.TrimEnd('/', '\\', ' ');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return info.FullName.Trim('/', '\\', ' ').ToLower();
|
2010-10-15 07:10:44 +00:00
|
|
|
|
}
|
2011-04-22 02:23:31 +00:00
|
|
|
|
|
|
|
|
|
|
2010-10-15 07:10:44 +00:00
|
|
|
|
}
|
2011-04-10 02:44:01 +00:00
|
|
|
|
}
|