aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2013-09-21 17:00:12 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2013-09-21 17:00:12 -0400
commitd9fecd78a54453a7656f649a6fcfc654f74dbdf6 (patch)
treecfa17b47f60f63ec3ca630324bf5144b38287e5a
parent4064b8badad46ac30afd7009bdc793c1f49c9103 (diff)
improve service shutdown
-rw-r--r--MediaBrowser.ServerApplication/ApplicationHost.cs19
-rw-r--r--MediaBrowser.ServerApplication/MainStartup.cs60
2 files changed, 56 insertions, 23 deletions
diff --git a/MediaBrowser.ServerApplication/ApplicationHost.cs b/MediaBrowser.ServerApplication/ApplicationHost.cs
index 648ff9fc3..471525816 100644
--- a/MediaBrowser.ServerApplication/ApplicationHost.cs
+++ b/MediaBrowser.ServerApplication/ApplicationHost.cs
@@ -534,14 +534,7 @@ namespace MediaBrowser.ServerApplication
Logger.ErrorException("Error sending server restart web socket message", ex);
}
- // Second instance will start first, so release the mutex and dispose the http server ahead of time
- Application.Current.Dispatcher.Invoke(() => MainStartup.ReleaseMutex(Logger));
-
- Dispose();
-
- System.Windows.Forms.Application.Restart();
-
- ShutdownInternal();
+ MainStartup.Restart();
}
/// <summary>
@@ -661,15 +654,7 @@ namespace MediaBrowser.ServerApplication
Logger.ErrorException("Error sending server shutdown web socket message", ex);
}
- ShutdownInternal();
- }
-
- public void ShutdownInternal()
- {
- Logger.Info("Shutting down application");
- var app = Application.Current;
-
- app.Dispatcher.Invoke(app.Shutdown);
+ MainStartup.Shutdown();
}
/// <summary>
diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.cs
index 2c11f8337..e9c1fdc99 100644
--- a/MediaBrowser.ServerApplication/MainStartup.cs
+++ b/MediaBrowser.ServerApplication/MainStartup.cs
@@ -27,6 +27,10 @@ namespace MediaBrowser.ServerApplication
private static App _app;
+ private static BackgroundService _backgroundService;
+
+ private static ILogger _logger;
+
/// <summary>
/// Defines the entry point of the application.
/// </summary>
@@ -41,7 +45,7 @@ namespace MediaBrowser.ServerApplication
var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
logManager.ReloadLogger(LogSeverity.Info);
- var logger = logManager.GetLogger("Main");
+ var logger = _logger = logManager.GetLogger("Main");
BeginLog(logger);
@@ -187,6 +191,8 @@ namespace MediaBrowser.ServerApplication
service.Disposed += service_Disposed;
ServiceBase.Run(service);
+
+ _backgroundService = service;
}
/// <summary>
@@ -294,10 +300,10 @@ namespace MediaBrowser.ServerApplication
/// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
- // Try to shutdown gracefully
- var task = _appHost.Shutdown();
-
- Task.WaitAll(task);
+ if (e.Reason == SessionEndReasons.SystemShutdown || _backgroundService == null)
+ {
+ Shutdown();
+ }
}
/// <summary>
@@ -309,7 +315,10 @@ namespace MediaBrowser.ServerApplication
{
var exception = (Exception)e.ExceptionObject;
- _app.OnUnhandledException(exception);
+ if (_backgroundService == null)
+ {
+ _app.OnUnhandledException(exception);
+ }
if (!Debugger.IsAttached)
{
@@ -365,5 +374,44 @@ namespace MediaBrowser.ServerApplication
return false;
}
+
+ public static void Shutdown()
+ {
+ if (_backgroundService != null)
+ {
+ _backgroundService.Stop();
+ }
+ else
+ {
+ _app.Dispatcher.Invoke(_app.Shutdown);
+ }
+ }
+
+ public static void Restart()
+ {
+ // Second instance will start first, so release the mutex and dispose the http server ahead of time
+ _app.Dispatcher.Invoke(() => ReleaseMutex(_logger));
+
+ _appHost.Dispose();
+
+ RestartInternal();
+
+ _app.Dispatcher.Invoke(_app.Shutdown);
+ }
+
+ private static void RestartInternal()
+ {
+ if (_backgroundService == null)
+ {
+ System.Windows.Forms.Application.Restart();
+ }
+ else
+ {
+ //var controller = new ServiceController()
+ //{
+ // ServiceName = BackgroundService.Name
+ //};
+ }
+ }
}
}