1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00
Sonarr/NzbDrone/Providers/IISProvider.cs

147 lines
4.8 KiB
C#
Raw Normal View History

2010-10-15 10:10:44 +03:00
using System;
using System.Diagnostics;
using System.IO;
using NLog;
2011-10-07 09:36:04 +03:00
using Ninject;
using NzbDrone.Common;
using NzbDrone.Common.Model;
2010-10-15 10:10: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-10-07 09:57:43 +03:00
private readonly ConfigProvider _configProvider;
private readonly ProcessProvider _processProvider;
private readonly EnviromentProvider _enviromentProvider;
2011-10-07 09:36:04 +03:00
[Inject]
public IISProvider(ConfigProvider configProvider, ProcessProvider processProvider, EnviromentProvider enviromentProvider)
2011-10-07 09:36:04 +03:00
{
_configProvider = configProvider;
_processProvider = processProvider;
_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 09:36:04 +03:00
public string AppUrl
2010-10-15 10:10:44 +03:00
{
get { return string.Format("http://localhost:{0}/", _configProvider.PortNumber); }
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-10-07 09:36:04 +03:00
startInfo.FileName = _configProvider.IISExePath;
startInfo.Arguments = String.Format("/config:\"{0}\" /trace:i", _configProvider.IISConfigPath);
startInfo.WorkingDirectory = _enviromentProvider.ApplicationPath;
2011-10-07 09:36:04 +03:00
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
2010-10-15 10:10:44 +03:00
//Set Variables for the config file.
startInfo.EnvironmentVariables.Add("NZBDRONE_PATH", _enviromentProvider.ApplicationPath);
2011-10-07 09:36:04 +03:00
startInfo.EnvironmentVariables.Add("NZBDRONE_PID", Process.GetCurrentProcess().Id.ToString());
try
{
2011-10-07 09:36:04 +03:00
_configProvider.UpdateIISConfig(_configProvider.IISConfigPath);
}
catch (Exception e)
{
Logger.ErrorException("An error has occurred while trying to update the config file.", e);
}
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-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);
if (NormalizePath(process.StartPath) == NormalizePath(_configProvider.IISExePath))
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-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();
}
private void OnOutputDataReceived(object s, DataReceivedEventArgs e)
{
if (e == null || String.IsNullOrWhiteSpace(e.Data) || e.Data.StartsWith("Request started:") ||
2011-04-10 05:44:01 +03:00
e.Data.StartsWith("Request ended:") || e.Data == ("IncrementMessages called"))
return;
if (e.Data.Contains(" NzbDrone."))
{
Console.WriteLine(e.Data);
return;
}
IISLogger.Trace(e.Data);
}
2011-10-07 09:36:04 +03:00
private string NormalizePath(string path)
2010-10-15 10:10:44 +03:00
{
2011-04-25 06:51:18 +03: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 10:10:44 +03:00
}
}
2011-04-10 05:44:01 +03:00
}