2010-10-14 06:29:01 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
2011-07-27 22:59:48 +00:00
|
|
|
|
using System.Net;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
using System.Threading;
|
2011-07-11 05:05:52 +00:00
|
|
|
|
using System.Timers;
|
2010-10-15 07:10:44 +00:00
|
|
|
|
using Exceptioneer.WindowsFormsClient;
|
2010-10-14 06:29:01 +00:00
|
|
|
|
using NLog;
|
2010-10-10 19:00:07 +00:00
|
|
|
|
namespace NzbDrone
|
2010-09-23 03:19:47 +00:00
|
|
|
|
{
|
2011-04-10 02:44:01 +00:00
|
|
|
|
internal static class Program
|
2010-09-23 03:19:47 +00:00
|
|
|
|
{
|
2010-10-14 06:29:01 +00:00
|
|
|
|
private static readonly Logger Logger = LogManager.GetLogger("Application");
|
|
|
|
|
|
2011-04-10 02:44:01 +00:00
|
|
|
|
private static void Main()
|
2010-10-14 06:29:01 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2011-07-04 05:03:05 +00:00
|
|
|
|
Config.ConfigureNlog();
|
|
|
|
|
Logger.Info("Starting NZBDrone. Start-up Path:'{0}'", Config.ProjectRoot);
|
2010-10-17 17:22:48 +00:00
|
|
|
|
Thread.CurrentThread.Name = "Host";
|
|
|
|
|
|
2011-07-04 05:03:05 +00:00
|
|
|
|
Process currentProcess = Process.GetCurrentProcess();
|
2011-07-11 00:03:01 +00:00
|
|
|
|
|
2011-07-11 05:05:52 +00:00
|
|
|
|
var prioCheckTimer = new System.Timers.Timer(5000);
|
|
|
|
|
prioCheckTimer.Elapsed += prioCheckTimer_Elapsed;
|
|
|
|
|
prioCheckTimer.Enabled = true;
|
2011-07-04 05:03:05 +00:00
|
|
|
|
|
|
|
|
|
currentProcess.EnableRaisingEvents = true;
|
|
|
|
|
currentProcess.Exited += ProgramExited;
|
|
|
|
|
|
2010-10-14 06:29:01 +00:00
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += ((s, e) => AppDomainException(e));
|
2010-10-15 07:10:44 +00:00
|
|
|
|
AppDomain.CurrentDomain.ProcessExit += ProgramExited;
|
|
|
|
|
AppDomain.CurrentDomain.DomainUnload += ProgramExited;
|
2010-10-14 06:29:01 +00:00
|
|
|
|
|
2011-04-22 06:46:26 +00:00
|
|
|
|
IISController.StopServer();
|
|
|
|
|
IISController.StartServer();
|
2010-10-14 06:29:01 +00:00
|
|
|
|
|
2010-10-15 07:10:44 +00:00
|
|
|
|
#if DEBUG
|
2011-04-22 17:09:06 +00:00
|
|
|
|
Attach();
|
|
|
|
|
#endif
|
2011-07-11 00:03:01 +00:00
|
|
|
|
|
2011-07-27 22:59:48 +00:00
|
|
|
|
if (!Environment.UserInteractive)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
new WebClient().DownloadString(IISController.AppUrl);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Failed to load home page.", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2011-05-16 07:32:01 +00:00
|
|
|
|
{
|
2011-06-22 03:38:46 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Starting default browser. {0}", IISController.AppUrl);
|
|
|
|
|
Process.Start(IISController.AppUrl);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Failed to open URL in default browser.", e);
|
|
|
|
|
}
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
Console.ReadLine();
|
|
|
|
|
}
|
2011-04-24 03:02:20 +00:00
|
|
|
|
}
|
2011-04-22 17:09:06 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
AppDomainException(e);
|
|
|
|
|
}
|
2011-04-25 18:41:20 +00:00
|
|
|
|
|
|
|
|
|
Console.WriteLine("Press enter to exit.");
|
|
|
|
|
Console.ReadLine();
|
2011-04-22 17:09:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-07-17 20:01:37 +00:00
|
|
|
|
private static void prioCheckTimer_Elapsed(object sender, ElapsedEventArgs e)
|
2011-07-11 00:03:01 +00:00
|
|
|
|
{
|
|
|
|
|
Process currentProcess = Process.GetCurrentProcess();
|
2011-07-17 20:01:37 +00:00
|
|
|
|
if (currentProcess.PriorityClass != ProcessPriorityClass.Normal)
|
2011-07-11 00:03:01 +00:00
|
|
|
|
{
|
2011-07-17 20:01:37 +00:00
|
|
|
|
SetPriority(currentProcess);
|
2011-07-11 00:03:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-07-11 05:05:52 +00:00
|
|
|
|
|
2011-07-17 20:01:37 +00:00
|
|
|
|
if (IISController.IISProcess != null)
|
2011-07-11 00:03:01 +00:00
|
|
|
|
{
|
2011-07-17 20:01:37 +00:00
|
|
|
|
IISController.IISProcess.Refresh();
|
|
|
|
|
|
|
|
|
|
if (IISController.IISProcess.PriorityClass != ProcessPriorityClass.Normal && IISController.IISProcess.PriorityClass != ProcessPriorityClass.AboveNormal)
|
|
|
|
|
{
|
|
|
|
|
SetPriority(IISController.IISProcess);
|
|
|
|
|
}
|
2011-07-11 00:03:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-17 20:01:37 +00:00
|
|
|
|
private static void SetPriority(Process process)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Updating [{0}] process priority from {1} to {2}",
|
|
|
|
|
process.ProcessName,
|
|
|
|
|
IISController.IISProcess.PriorityClass,
|
|
|
|
|
ProcessPriorityClass.Normal);
|
|
|
|
|
process.PriorityClass = ProcessPriorityClass.Normal;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-11 05:05:52 +00:00
|
|
|
|
|
|
|
|
|
|
2011-05-27 06:01:07 +00:00
|
|
|
|
#if DEBUG
|
2011-04-22 17:09:06 +00:00
|
|
|
|
private static void Attach()
|
|
|
|
|
{
|
|
|
|
|
if (Debugger.IsAttached)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Trying to attach to debugger");
|
|
|
|
|
|
|
|
|
|
var count = 0;
|
|
|
|
|
|
|
|
|
|
while (true)
|
2010-10-17 17:22:48 +00:00
|
|
|
|
{
|
2010-10-24 07:46:58 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ProcessAttacher.Attach();
|
2011-04-22 17:09:06 +00:00
|
|
|
|
Logger.Info("Debugger Attached");
|
|
|
|
|
return;
|
2010-10-24 07:46:58 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2011-04-22 17:09:06 +00:00
|
|
|
|
count++;
|
|
|
|
|
if (count > 20)
|
|
|
|
|
{
|
|
|
|
|
Logger.WarnException("Unable to attach to debugger", e);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
|
2010-10-24 07:46:58 +00:00
|
|
|
|
}
|
2010-10-15 07:10:44 +00:00
|
|
|
|
}
|
2010-10-14 06:29:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-27 06:01:07 +00:00
|
|
|
|
#endif
|
2010-10-14 06:29:01 +00:00
|
|
|
|
|
|
|
|
|
private static void AppDomainException(object excepion)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("EPIC FAIL: {0}", excepion);
|
|
|
|
|
Logger.Fatal("EPIC FAIL: {0}", excepion);
|
|
|
|
|
|
2011-07-05 06:46:03 +00:00
|
|
|
|
#if RELEASE
|
2010-10-15 07:10:44 +00:00
|
|
|
|
new Client
|
2011-04-25 18:41:20 +00:00
|
|
|
|
{
|
|
|
|
|
ApiKey = "43BBF60A-EB2A-4C1C-B09E-422ADF637265",
|
|
|
|
|
ApplicationName = "NZBDrone",
|
|
|
|
|
CurrentException = excepion as Exception
|
|
|
|
|
}.Submit();
|
|
|
|
|
#endif
|
2010-09-23 03:19:47 +00:00
|
|
|
|
|
2011-04-22 06:46:26 +00:00
|
|
|
|
IISController.StopServer();
|
2010-10-14 06:29:01 +00:00
|
|
|
|
}
|
2010-09-23 03:19:47 +00:00
|
|
|
|
|
2011-04-10 02:44:01 +00:00
|
|
|
|
private static void ProgramExited(object sender, EventArgs e)
|
2010-10-14 06:29:01 +00:00
|
|
|
|
{
|
2011-04-22 06:46:26 +00:00
|
|
|
|
IISController.StopServer();
|
2010-10-14 06:29:01 +00:00
|
|
|
|
}
|
2010-09-23 03:19:47 +00:00
|
|
|
|
}
|
2011-04-10 02:44:01 +00:00
|
|
|
|
}
|