2015-02-22 05:45:29 +00:00
|
|
|
|
using ImageMagickSharp;
|
|
|
|
|
using MediaBrowser.Common.Implementations.Logging;
|
2013-09-21 01:04:14 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-09-20 17:32:10 +00:00
|
|
|
|
using MediaBrowser.Server.Implementations;
|
2014-11-09 18:24:57 +00:00
|
|
|
|
using MediaBrowser.Server.Startup.Common;
|
|
|
|
|
using MediaBrowser.Server.Startup.Common.Browser;
|
2013-09-28 18:07:30 +00:00
|
|
|
|
using MediaBrowser.ServerApplication.Native;
|
2014-02-15 19:48:35 +00:00
|
|
|
|
using MediaBrowser.ServerApplication.Splash;
|
2014-03-28 18:17:18 +00:00
|
|
|
|
using MediaBrowser.ServerApplication.Updates;
|
2013-09-21 01:04:14 +00:00
|
|
|
|
using Microsoft.Win32;
|
2013-09-20 17:32:10 +00:00
|
|
|
|
using System;
|
2013-09-21 01:04:14 +00:00
|
|
|
|
using System.Configuration.Install;
|
2013-09-20 17:32:10 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2013-09-21 01:04:14 +00:00
|
|
|
|
using System.Linq;
|
2013-10-06 18:48:04 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2013-09-21 01:04:14 +00:00
|
|
|
|
using System.ServiceProcess;
|
2014-02-15 20:26:21 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2014-02-15 19:48:35 +00:00
|
|
|
|
using System.Windows.Forms;
|
2013-09-20 17:32:10 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.ServerApplication
|
|
|
|
|
{
|
|
|
|
|
public class MainStartup
|
|
|
|
|
{
|
2013-09-21 01:04:14 +00:00
|
|
|
|
private static ApplicationHost _appHost;
|
|
|
|
|
|
2013-09-21 21:00:12 +00:00
|
|
|
|
private static ILogger _logger;
|
|
|
|
|
|
2013-09-27 21:09:21 +00:00
|
|
|
|
private static bool _isRunningAsService = false;
|
|
|
|
|
|
2013-09-20 17:32:10 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Defines the entry point of the application.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void Main()
|
|
|
|
|
{
|
2014-09-14 15:26:33 +00:00
|
|
|
|
var options = new StartupOptions();
|
|
|
|
|
_isRunningAsService = options.ContainsOption("-service");
|
2013-09-21 01:04:14 +00:00
|
|
|
|
|
2014-11-09 03:18:14 +00:00
|
|
|
|
var currentProcess = Process.GetCurrentProcess();
|
|
|
|
|
|
|
|
|
|
var applicationPath = currentProcess.MainModule.FileName;
|
2013-12-13 04:06:38 +00:00
|
|
|
|
|
2015-02-22 05:45:29 +00:00
|
|
|
|
Wand.SetMagickCoderModulePath(Path.Combine(Path.GetDirectoryName(applicationPath), "ImageMagickCoders", "x86"));
|
2013-12-13 04:06:38 +00:00
|
|
|
|
var appPaths = CreateApplicationPaths(applicationPath, _isRunningAsService);
|
2013-09-21 01:04:14 +00:00
|
|
|
|
|
|
|
|
|
var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
|
2013-12-07 15:52:38 +00:00
|
|
|
|
logManager.ReloadLogger(LogSeverity.Debug);
|
2014-01-09 04:44:51 +00:00
|
|
|
|
logManager.AddConsoleOutput();
|
2013-09-21 01:04:14 +00:00
|
|
|
|
|
2013-09-21 21:00:12 +00:00
|
|
|
|
var logger = _logger = logManager.GetLogger("Main");
|
2013-09-21 01:04:14 +00:00
|
|
|
|
|
2014-11-23 23:10:41 +00:00
|
|
|
|
ApplicationHost.LogEnvironmentInfo(logger, appPaths, true);
|
2013-09-21 01:04:14 +00:00
|
|
|
|
|
|
|
|
|
// Install directly
|
2014-09-14 15:26:33 +00:00
|
|
|
|
if (options.ContainsOption("-installservice"))
|
2013-09-21 01:04:14 +00:00
|
|
|
|
{
|
|
|
|
|
logger.Info("Performing service installation");
|
2013-12-13 04:06:38 +00:00
|
|
|
|
InstallService(applicationPath, logger);
|
2013-09-21 01:04:14 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Restart with admin rights, then install
|
2014-09-14 15:26:33 +00:00
|
|
|
|
if (options.ContainsOption("-installserviceasadmin"))
|
2013-09-21 01:04:14 +00:00
|
|
|
|
{
|
|
|
|
|
logger.Info("Performing service installation");
|
2013-12-13 04:06:38 +00:00
|
|
|
|
RunServiceInstallation(applicationPath);
|
2013-09-21 01:04:14 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Uninstall directly
|
2014-09-14 15:26:33 +00:00
|
|
|
|
if (options.ContainsOption("-uninstallservice"))
|
2013-09-21 01:04:14 +00:00
|
|
|
|
{
|
|
|
|
|
logger.Info("Performing service uninstallation");
|
2013-12-13 04:06:38 +00:00
|
|
|
|
UninstallService(applicationPath, logger);
|
2013-09-21 01:04:14 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Restart with admin rights, then uninstall
|
2014-09-14 15:26:33 +00:00
|
|
|
|
if (options.ContainsOption("-uninstallserviceasadmin"))
|
2013-09-21 01:04:14 +00:00
|
|
|
|
{
|
|
|
|
|
logger.Info("Performing service uninstallation");
|
2013-12-13 04:06:38 +00:00
|
|
|
|
RunServiceUninstallation(applicationPath);
|
2013-09-21 01:04:14 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-20 17:32:10 +00:00
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
|
|
|
|
|
2013-12-13 04:06:38 +00:00
|
|
|
|
RunServiceInstallationIfNeeded(applicationPath);
|
2013-09-20 17:32:10 +00:00
|
|
|
|
|
2013-12-13 04:06:38 +00:00
|
|
|
|
if (IsAlreadyRunning(applicationPath, currentProcess))
|
2013-09-20 17:32:10 +00:00
|
|
|
|
{
|
2013-09-21 01:04:14 +00:00
|
|
|
|
logger.Info("Shutting down because another instance of Media Browser Server is already running.");
|
2013-09-20 17:32:10 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-21 01:04:14 +00:00
|
|
|
|
if (PerformUpdateIfNeeded(appPaths, logger))
|
2013-09-20 17:32:10 +00:00
|
|
|
|
{
|
2013-09-21 01:04:14 +00:00
|
|
|
|
logger.Info("Exiting to perform application update.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-09-20 17:32:10 +00:00
|
|
|
|
|
2013-09-21 01:04:14 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2014-09-14 15:26:33 +00:00
|
|
|
|
RunApplication(appPaths, logManager, _isRunningAsService, options);
|
2013-09-20 17:32:10 +00:00
|
|
|
|
}
|
2013-09-21 01:04:14 +00:00
|
|
|
|
finally
|
|
|
|
|
{
|
2013-09-27 21:09:21 +00:00
|
|
|
|
OnServiceShutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-20 17:32:10 +00:00
|
|
|
|
|
2013-09-27 21:09:21 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether [is already running] [the specified current process].
|
|
|
|
|
/// </summary>
|
2014-05-07 20:16:57 +00:00
|
|
|
|
/// <param name="applicationPath">The application path.</param>
|
2013-09-27 21:09:21 +00:00
|
|
|
|
/// <param name="currentProcess">The current process.</param>
|
|
|
|
|
/// <returns><c>true</c> if [is already running] [the specified current process]; otherwise, <c>false</c>.</returns>
|
2013-12-13 04:06:38 +00:00
|
|
|
|
private static bool IsAlreadyRunning(string applicationPath, Process currentProcess)
|
2013-09-27 21:09:21 +00:00
|
|
|
|
{
|
2014-06-04 16:15:44 +00:00
|
|
|
|
var filename = Path.GetFileName(applicationPath);
|
|
|
|
|
|
2013-09-27 21:09:21 +00:00
|
|
|
|
var duplicate = Process.GetProcesses().FirstOrDefault(i =>
|
2013-12-13 04:06:38 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
2013-09-27 21:09:21 +00:00
|
|
|
|
{
|
2014-06-04 16:15:44 +00:00
|
|
|
|
return string.Equals(filename, Path.GetFileName(i.MainModule.FileName)) && currentProcess.Id != i.Id;
|
2013-12-13 04:06:38 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-09-27 21:09:21 +00:00
|
|
|
|
|
|
|
|
|
if (duplicate != null)
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Found a duplicate process. Giving it time to exit.");
|
2013-10-07 00:49:33 +00:00
|
|
|
|
|
2014-05-07 20:16:57 +00:00
|
|
|
|
if (!duplicate.WaitForExit(10000))
|
2013-09-27 21:09:21 +00:00
|
|
|
|
{
|
|
|
|
|
_logger.Info("The duplicate process did not exit.");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-09-21 01:04:14 +00:00
|
|
|
|
}
|
2013-09-27 21:09:21 +00:00
|
|
|
|
|
|
|
|
|
return false;
|
2013-09-20 17:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-21 01:04:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates the application paths.
|
|
|
|
|
/// </summary>
|
2014-11-26 04:12:29 +00:00
|
|
|
|
/// <param name="applicationPath">The application path.</param>
|
2013-09-21 01:04:14 +00:00
|
|
|
|
/// <param name="runAsService">if set to <c>true</c> [run as service].</param>
|
|
|
|
|
/// <returns>ServerApplicationPaths.</returns>
|
2013-12-13 04:06:38 +00:00
|
|
|
|
private static ServerApplicationPaths CreateApplicationPaths(string applicationPath, bool runAsService)
|
2013-09-21 01:04:14 +00:00
|
|
|
|
{
|
2014-11-26 20:57:16 +00:00
|
|
|
|
var resourcesPath = Path.GetDirectoryName(applicationPath);
|
|
|
|
|
|
2013-09-21 01:04:14 +00:00
|
|
|
|
if (runAsService)
|
|
|
|
|
{
|
2013-12-13 04:06:38 +00:00
|
|
|
|
var systemPath = Path.GetDirectoryName(applicationPath);
|
2013-09-21 01:04:14 +00:00
|
|
|
|
|
|
|
|
|
var programDataPath = Path.GetDirectoryName(systemPath);
|
|
|
|
|
|
2014-11-26 20:57:16 +00:00
|
|
|
|
return new ServerApplicationPaths(programDataPath, applicationPath, resourcesPath);
|
2013-09-21 01:04:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-26 20:57:16 +00:00
|
|
|
|
return new ServerApplicationPaths(ApplicationPathHelper.GetProgramDataPath(applicationPath), applicationPath, resourcesPath);
|
2013-09-21 01:04:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-07 14:38:31 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating whether this instance can self restart.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value><c>true</c> if this instance can self restart; otherwise, <c>false</c>.</value>
|
|
|
|
|
public static bool CanSelfRestart
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2013-10-07 16:22:19 +00:00
|
|
|
|
return !_isRunningAsService;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating whether this instance can self update.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
|
|
|
|
|
public static bool CanSelfUpdate
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return !_isRunningAsService;
|
2013-10-07 14:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-15 19:48:35 +00:00
|
|
|
|
private static readonly TaskCompletionSource<bool> ApplicationTaskCompletionSource = new TaskCompletionSource<bool>();
|
|
|
|
|
|
2013-09-21 01:04:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Runs the application.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
|
/// <param name="logManager">The log manager.</param>
|
|
|
|
|
/// <param name="runService">if set to <c>true</c> [run service].</param>
|
2014-09-14 15:26:33 +00:00
|
|
|
|
/// <param name="options">The options.</param>
|
|
|
|
|
private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager, bool runService, StartupOptions options)
|
2013-09-20 17:32:10 +00:00
|
|
|
|
{
|
2014-10-06 23:58:46 +00:00
|
|
|
|
var fileSystem = new NativeFileSystem(logManager.GetLogger("FileSystem"), false);
|
|
|
|
|
|
2015-01-13 03:46:44 +00:00
|
|
|
|
var nativeApp = new WindowsApp(fileSystem)
|
2014-11-09 18:24:57 +00:00
|
|
|
|
{
|
|
|
|
|
IsRunningAsService = runService
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_appHost = new ApplicationHost(appPaths,
|
|
|
|
|
logManager,
|
|
|
|
|
options,
|
|
|
|
|
fileSystem,
|
2014-10-06 23:58:46 +00:00
|
|
|
|
"MBServer",
|
2014-11-09 18:24:57 +00:00
|
|
|
|
nativeApp);
|
2014-11-23 20:08:52 +00:00
|
|
|
|
|
2014-02-15 19:48:35 +00:00
|
|
|
|
var initProgress = new Progress<double>();
|
2013-09-21 01:04:14 +00:00
|
|
|
|
|
2014-02-25 15:40:16 +00:00
|
|
|
|
if (!runService)
|
2013-09-28 18:07:30 +00:00
|
|
|
|
{
|
2014-11-07 15:38:04 +00:00
|
|
|
|
if (!options.ContainsOption("-nosplash")) ShowSplashScreen(_appHost.ApplicationVersion, initProgress, logManager.GetLogger("Splash"));
|
2014-11-09 18:24:57 +00:00
|
|
|
|
|
2013-09-28 18:07:30 +00:00
|
|
|
|
// Not crazy about this but it's the only way to suppress ffmpeg crash dialog boxes
|
|
|
|
|
SetErrorMode(ErrorModes.SEM_FAILCRITICALERRORS | ErrorModes.SEM_NOALIGNMENTFAULTEXCEPT |
|
|
|
|
|
ErrorModes.SEM_NOGPFAULTERRORBOX | ErrorModes.SEM_NOOPENFILEERRORBOX);
|
|
|
|
|
}
|
2013-10-07 00:49:33 +00:00
|
|
|
|
|
2014-07-22 01:32:40 +00:00
|
|
|
|
|
2014-02-15 19:48:35 +00:00
|
|
|
|
var task = _appHost.Init(initProgress);
|
2014-07-22 01:31:53 +00:00
|
|
|
|
task = task.ContinueWith(new Action<Task>(a => _appHost.RunStartupTasks()));
|
2014-02-25 15:40:16 +00:00
|
|
|
|
|
|
|
|
|
if (runService)
|
|
|
|
|
{
|
|
|
|
|
StartService(logManager);
|
|
|
|
|
}
|
|
|
|
|
else
|
2014-02-15 19:48:35 +00:00
|
|
|
|
{
|
2014-07-22 01:31:53 +00:00
|
|
|
|
Task.WaitAll(task);
|
|
|
|
|
|
|
|
|
|
SystemEvents.SessionEnding += SystemEvents_SessionEnding;
|
|
|
|
|
SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
|
2014-11-09 18:24:57 +00:00
|
|
|
|
|
2014-02-15 19:48:35 +00:00
|
|
|
|
HideSplashScreen();
|
2014-02-15 20:26:21 +00:00
|
|
|
|
|
2014-02-15 21:17:36 +00:00
|
|
|
|
ShowTrayIcon();
|
2014-11-09 18:24:57 +00:00
|
|
|
|
|
2014-02-25 15:40:16 +00:00
|
|
|
|
task = ApplicationTaskCompletionSource.Task;
|
|
|
|
|
Task.WaitAll(task);
|
2014-02-15 19:48:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-15 21:17:36 +00:00
|
|
|
|
private static ServerNotifyIcon _serverNotifyIcon;
|
|
|
|
|
private static void ShowTrayIcon()
|
2014-02-15 20:26:21 +00:00
|
|
|
|
{
|
2014-02-15 21:17:36 +00:00
|
|
|
|
//Application.EnableVisualStyles();
|
|
|
|
|
//Application.SetCompatibleTextRenderingDefault(false);
|
2014-11-09 18:24:57 +00:00
|
|
|
|
_serverNotifyIcon = new ServerNotifyIcon(_appHost.LogManager, _appHost, _appHost.ServerConfigurationManager, _appHost.LocalizationManager);
|
2014-02-15 21:17:36 +00:00
|
|
|
|
Application.Run();
|
2014-02-15 20:26:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-15 19:48:35 +00:00
|
|
|
|
private static SplashForm _splash;
|
|
|
|
|
private static Thread _splashThread;
|
|
|
|
|
private static void ShowSplashScreen(Version appVersion, Progress<double> progress, ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
var thread = new Thread(() =>
|
|
|
|
|
{
|
|
|
|
|
_splash = new SplashForm(appVersion, progress);
|
|
|
|
|
|
|
|
|
|
_splash.ShowDialog();
|
|
|
|
|
});
|
2014-11-09 18:24:57 +00:00
|
|
|
|
|
2014-02-15 19:48:35 +00:00
|
|
|
|
thread.SetApartmentState(ApartmentState.STA);
|
|
|
|
|
thread.IsBackground = true;
|
|
|
|
|
thread.Start();
|
|
|
|
|
|
|
|
|
|
_splashThread = thread;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void HideSplashScreen()
|
|
|
|
|
{
|
|
|
|
|
if (_splash != null)
|
|
|
|
|
{
|
2014-02-15 20:26:21 +00:00
|
|
|
|
Action act = () =>
|
|
|
|
|
{
|
|
|
|
|
_splash.Close();
|
|
|
|
|
_splashThread = null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_splash.Invoke(act);
|
2014-02-15 19:48:35 +00:00
|
|
|
|
}
|
2013-09-21 01:04:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-28 18:07:30 +00:00
|
|
|
|
static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Reason == SessionSwitchReason.SessionLogon)
|
|
|
|
|
{
|
2014-11-03 03:38:43 +00:00
|
|
|
|
BrowserLauncher.OpenDashboard(_appHost, _logger);
|
2013-09-28 18:07:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-21 01:04:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts the service.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static void StartService(ILogManager logManager)
|
|
|
|
|
{
|
|
|
|
|
var service = new BackgroundService(logManager.GetLogger("Service"));
|
|
|
|
|
|
|
|
|
|
service.Disposed += service_Disposed;
|
|
|
|
|
|
|
|
|
|
ServiceBase.Run(service);
|
2013-09-20 17:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-21 01:04:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the Disposed event of the service control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
|
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
|
2013-09-27 21:09:21 +00:00
|
|
|
|
static void service_Disposed(object sender, EventArgs e)
|
|
|
|
|
{
|
2014-02-15 19:48:35 +00:00
|
|
|
|
ApplicationTaskCompletionSource.SetResult(true);
|
2013-09-27 21:09:21 +00:00
|
|
|
|
OnServiceShutdown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void OnServiceShutdown()
|
2013-09-20 17:32:10 +00:00
|
|
|
|
{
|
2013-09-27 21:09:21 +00:00
|
|
|
|
_logger.Info("Shutting down");
|
|
|
|
|
|
|
|
|
|
_appHost.Dispose();
|
|
|
|
|
|
2013-09-28 18:07:30 +00:00
|
|
|
|
if (!_isRunningAsService)
|
|
|
|
|
{
|
|
|
|
|
SetErrorMode(ErrorModes.SYSTEM_DEFAULT);
|
|
|
|
|
}
|
2013-09-21 01:04:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Installs the service.
|
|
|
|
|
/// </summary>
|
2013-12-13 04:06:38 +00:00
|
|
|
|
private static void InstallService(string applicationPath, ILogger logger)
|
2013-09-21 01:04:14 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
2013-09-20 17:32:10 +00:00
|
|
|
|
{
|
2013-12-13 04:06:38 +00:00
|
|
|
|
ManagedInstallerClass.InstallHelper(new[] { applicationPath });
|
2013-09-21 01:04:14 +00:00
|
|
|
|
|
|
|
|
|
logger.Info("Service installation succeeded");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger.ErrorException("Uninstall failed", ex);
|
2013-09-20 17:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-21 01:04:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Uninstalls the service.
|
|
|
|
|
/// </summary>
|
2013-12-13 04:06:38 +00:00
|
|
|
|
private static void UninstallService(string applicationPath, ILogger logger)
|
2013-09-20 17:32:10 +00:00
|
|
|
|
{
|
2013-09-21 01:04:14 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2013-12-13 04:06:38 +00:00
|
|
|
|
ManagedInstallerClass.InstallHelper(new[] { "/u", applicationPath });
|
2013-09-20 17:32:10 +00:00
|
|
|
|
|
2013-09-21 01:04:14 +00:00
|
|
|
|
logger.Info("Service uninstallation succeeded");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger.ErrorException("Uninstall failed", ex);
|
|
|
|
|
}
|
2013-09-20 17:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-13 04:06:38 +00:00
|
|
|
|
private static void RunServiceInstallationIfNeeded(string applicationPath)
|
2013-09-27 21:09:21 +00:00
|
|
|
|
{
|
|
|
|
|
var ctl = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == BackgroundService.Name);
|
|
|
|
|
|
|
|
|
|
if (ctl == null)
|
|
|
|
|
{
|
2013-12-13 04:06:38 +00:00
|
|
|
|
RunServiceInstallation(applicationPath);
|
2013-09-27 21:09:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-21 01:04:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Runs the service installation.
|
|
|
|
|
/// </summary>
|
2013-12-13 04:06:38 +00:00
|
|
|
|
private static void RunServiceInstallation(string applicationPath)
|
2013-09-20 17:32:10 +00:00
|
|
|
|
{
|
2013-09-21 01:04:14 +00:00
|
|
|
|
var startInfo = new ProcessStartInfo
|
|
|
|
|
{
|
2013-12-13 04:06:38 +00:00
|
|
|
|
FileName = applicationPath,
|
2013-09-21 01:04:14 +00:00
|
|
|
|
|
|
|
|
|
Arguments = "-installservice",
|
|
|
|
|
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
WindowStyle = ProcessWindowStyle.Hidden,
|
|
|
|
|
Verb = "runas",
|
|
|
|
|
ErrorDialog = false
|
|
|
|
|
};
|
2013-09-20 17:32:10 +00:00
|
|
|
|
|
2013-09-21 01:04:14 +00:00
|
|
|
|
using (var process = Process.Start(startInfo))
|
|
|
|
|
{
|
|
|
|
|
process.WaitForExit();
|
|
|
|
|
}
|
2013-09-20 17:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-21 01:04:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Runs the service uninstallation.
|
|
|
|
|
/// </summary>
|
2013-12-13 04:06:38 +00:00
|
|
|
|
private static void RunServiceUninstallation(string applicationPath)
|
2013-09-20 17:32:10 +00:00
|
|
|
|
{
|
2013-09-21 01:04:14 +00:00
|
|
|
|
var startInfo = new ProcessStartInfo
|
2013-09-20 17:32:10 +00:00
|
|
|
|
{
|
2013-12-13 04:06:38 +00:00
|
|
|
|
FileName = applicationPath,
|
2013-09-21 01:04:14 +00:00
|
|
|
|
|
|
|
|
|
Arguments = "-uninstallservice",
|
|
|
|
|
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
WindowStyle = ProcessWindowStyle.Hidden,
|
|
|
|
|
Verb = "runas",
|
|
|
|
|
ErrorDialog = false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using (var process = Process.Start(startInfo))
|
|
|
|
|
{
|
|
|
|
|
process.WaitForExit();
|
2013-09-20 17:32:10 +00:00
|
|
|
|
}
|
2013-09-21 01:04:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the SessionEnding event of the SystemEvents control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
|
/// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
|
|
|
|
|
static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
|
|
|
|
|
{
|
2013-09-27 21:09:21 +00:00
|
|
|
|
if (e.Reason == SessionEndReasons.SystemShutdown || !_isRunningAsService)
|
2013-09-21 21:00:12 +00:00
|
|
|
|
{
|
|
|
|
|
Shutdown();
|
|
|
|
|
}
|
2013-09-21 01:04:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the UnhandledException event of the CurrentDomain control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
|
/// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
|
|
|
|
|
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var exception = (Exception)e.ExceptionObject;
|
|
|
|
|
|
2014-11-23 22:36:40 +00:00
|
|
|
|
new UnhandledExceptionWriter(_appHost.ServerConfigurationManager.ApplicationPaths, _logger, _appHost.LogManager).Log(exception);
|
2013-10-06 18:48:04 +00:00
|
|
|
|
|
2013-09-27 21:09:21 +00:00
|
|
|
|
if (!_isRunningAsService)
|
2013-09-21 21:00:12 +00:00
|
|
|
|
{
|
2014-02-15 19:48:35 +00:00
|
|
|
|
MessageBox.Show("Unhandled exception: " + exception.Message);
|
2013-09-21 21:00:12 +00:00
|
|
|
|
}
|
2013-09-20 17:32:10 +00:00
|
|
|
|
|
|
|
|
|
if (!Debugger.IsAttached)
|
|
|
|
|
{
|
2013-10-06 18:48:04 +00:00
|
|
|
|
Environment.Exit(Marshal.GetHRForException(exception));
|
2013-09-20 17:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-21 01:04:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs the update if needed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
|
|
|
|
|
private static bool PerformUpdateIfNeeded(ServerApplicationPaths appPaths, ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
// Look for the existence of an update archive
|
2014-05-12 18:04:25 +00:00
|
|
|
|
var updateArchive = Path.Combine(appPaths.TempUpdatePath, "MBServer" + ".zip");
|
2013-09-21 01:04:14 +00:00
|
|
|
|
if (File.Exists(updateArchive))
|
|
|
|
|
{
|
|
|
|
|
logger.Info("An update is available from {0}", updateArchive);
|
|
|
|
|
|
|
|
|
|
// Update is there - execute update
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-10-06 18:48:04 +00:00
|
|
|
|
var serviceName = _isRunningAsService ? BackgroundService.Name : string.Empty;
|
2014-03-28 18:17:18 +00:00
|
|
|
|
new ApplicationUpdater().UpdateApplication(appPaths, updateArchive, logger, serviceName);
|
2013-09-21 01:04:14 +00:00
|
|
|
|
|
|
|
|
|
// And just let the app exit so it can update
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2013-10-06 18:48:04 +00:00
|
|
|
|
logger.ErrorException("Error starting updater.", e);
|
|
|
|
|
|
2013-09-21 01:04:14 +00:00
|
|
|
|
MessageBox.Show(string.Format("Error attempting to update application.\n\n{0}\n\n{1}", e.GetType().Name, e.Message));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-09-21 21:00:12 +00:00
|
|
|
|
|
|
|
|
|
public static void Shutdown()
|
|
|
|
|
{
|
2013-09-27 21:09:21 +00:00
|
|
|
|
if (_isRunningAsService)
|
2013-09-21 21:00:12 +00:00
|
|
|
|
{
|
2013-09-27 21:09:21 +00:00
|
|
|
|
ShutdownWindowsService();
|
2013-09-21 21:00:12 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-09-27 21:09:21 +00:00
|
|
|
|
ShutdownWindowsApplication();
|
2013-09-21 21:00:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Restart()
|
|
|
|
|
{
|
2013-09-27 21:09:21 +00:00
|
|
|
|
_logger.Info("Disposing app host");
|
2013-09-21 21:00:12 +00:00
|
|
|
|
_appHost.Dispose();
|
|
|
|
|
|
2013-09-27 21:09:21 +00:00
|
|
|
|
if (!_isRunningAsService)
|
2013-09-21 21:00:12 +00:00
|
|
|
|
{
|
2014-06-20 04:50:30 +00:00
|
|
|
|
_logger.Info("Hiding server notify icon");
|
2014-02-15 21:17:36 +00:00
|
|
|
|
_serverNotifyIcon.Visible = false;
|
2013-09-27 21:09:21 +00:00
|
|
|
|
|
2014-06-20 04:50:30 +00:00
|
|
|
|
_logger.Info("Executing windows forms restart");
|
|
|
|
|
//Application.Restart();
|
|
|
|
|
Process.Start(_appHost.ServerConfigurationManager.ApplicationPaths.ApplicationPath);
|
|
|
|
|
|
|
|
|
|
_logger.Info("Calling Application.Exit");
|
|
|
|
|
Environment.Exit(0);
|
2013-10-07 16:22:19 +00:00
|
|
|
|
}
|
2013-09-27 21:09:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ShutdownWindowsApplication()
|
|
|
|
|
{
|
2014-06-20 04:50:30 +00:00
|
|
|
|
_logger.Info("Hiding server notify icon");
|
2014-02-15 21:17:36 +00:00
|
|
|
|
_serverNotifyIcon.Visible = false;
|
2014-06-20 04:50:30 +00:00
|
|
|
|
|
|
|
|
|
_logger.Info("Calling Application.Exit");
|
2014-02-15 21:17:36 +00:00
|
|
|
|
Application.Exit();
|
2014-06-20 04:50:30 +00:00
|
|
|
|
|
|
|
|
|
_logger.Info("Calling ApplicationTaskCompletionSource.SetResult");
|
2014-02-15 19:48:35 +00:00
|
|
|
|
ApplicationTaskCompletionSource.SetResult(true);
|
2013-09-27 21:09:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ShutdownWindowsService()
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Stopping background service");
|
|
|
|
|
var service = new ServiceController(BackgroundService.Name);
|
|
|
|
|
|
|
|
|
|
service.Refresh();
|
|
|
|
|
|
|
|
|
|
if (service.Status == ServiceControllerStatus.Running)
|
|
|
|
|
{
|
|
|
|
|
service.Stop();
|
2013-09-21 21:00:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-28 18:07:30 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the error mode.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="uMode">The u mode.</param>
|
|
|
|
|
/// <returns>ErrorModes.</returns>
|
|
|
|
|
[DllImport("kernel32.dll")]
|
|
|
|
|
static extern ErrorModes SetErrorMode(ErrorModes uMode);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Enum ErrorModes
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Flags]
|
|
|
|
|
public enum ErrorModes : uint
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The SYSTE m_ DEFAULT
|
|
|
|
|
/// </summary>
|
|
|
|
|
SYSTEM_DEFAULT = 0x0,
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The SE m_ FAILCRITICALERRORS
|
|
|
|
|
/// </summary>
|
|
|
|
|
SEM_FAILCRITICALERRORS = 0x0001,
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The SE m_ NOALIGNMENTFAULTEXCEPT
|
|
|
|
|
/// </summary>
|
|
|
|
|
SEM_NOALIGNMENTFAULTEXCEPT = 0x0004,
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The SE m_ NOGPFAULTERRORBOX
|
|
|
|
|
/// </summary>
|
|
|
|
|
SEM_NOGPFAULTERRORBOX = 0x0002,
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The SE m_ NOOPENFILEERRORBOX
|
|
|
|
|
/// </summary>
|
|
|
|
|
SEM_NOOPENFILEERRORBOX = 0x8000
|
|
|
|
|
}
|
2013-09-20 17:32:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|