From e564b54686029a90595e1bb4277f87490763e183 Mon Sep 17 00:00:00 2001 From: hatharry Date: Mon, 29 Aug 2016 01:27:22 +1200 Subject: [PATCH 1/2] Support service restart and update --- MediaBrowser.ServerApplication/MainStartup.cs | 67 ++++++++++++++++++- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.cs index bdfd7d1bb0..8d26f6a146 100644 --- a/MediaBrowser.ServerApplication/MainStartup.cs +++ b/MediaBrowser.ServerApplication/MainStartup.cs @@ -33,6 +33,7 @@ namespace MediaBrowser.ServerApplication private static ILogger _logger; private static bool _isRunningAsService = false; + private static bool _canRestartService = false; private static bool _appHostDisposed; [DllImport("kernel32.dll", SetLastError = true)] @@ -45,6 +46,7 @@ namespace MediaBrowser.ServerApplication { var options = new StartupOptions(); _isRunningAsService = options.ContainsOption("-service"); + _canRestartService = CanRestartWindowsService(); var currentProcess = Process.GetCurrentProcess(); @@ -239,7 +241,14 @@ namespace MediaBrowser.ServerApplication { get { - return !_isRunningAsService; + if (_isRunningAsService) + { + return _canRestartService; + } + else + { + return true; + } } } @@ -251,7 +260,14 @@ namespace MediaBrowser.ServerApplication { get { - return !_isRunningAsService; + if (_isRunningAsService) + { + return _canRestartService; + } + else + { + return true; + } } } @@ -593,7 +609,11 @@ namespace MediaBrowser.ServerApplication { DisposeAppHost(); - if (!_isRunningAsService) + if (_isRunningAsService) + { + RestartWindowsService(); + } + else { //_logger.Info("Hiding server notify icon"); //_serverNotifyIcon.Visible = false; @@ -642,6 +662,47 @@ namespace MediaBrowser.ServerApplication } } + private static void RestartWindowsService() + { + _logger.Info("Restarting background service"); + + var startInfo = new ProcessStartInfo + { + FileName = "cmd.exe", + CreateNoWindow = true, + WindowStyle = ProcessWindowStyle.Hidden, + Verb = "runas", + ErrorDialog = false, + Arguments = String.Format("/c sc stop {0} & sc start {0}", BackgroundService.GetExistingServiceName()) + }; + Process.Start(startInfo); + } + + private static bool CanRestartWindowsService() + { + var startInfo = new ProcessStartInfo + { + FileName = "cmd.exe", + CreateNoWindow = true, + WindowStyle = ProcessWindowStyle.Hidden, + Verb = "runas", + ErrorDialog = false, + Arguments = String.Format("/c sc query {0}", BackgroundService.GetExistingServiceName()) + }; + using (var process = Process.Start(startInfo)) + { + process.WaitForExit(); + if (process.ExitCode == 0) + { + return true; + } + else + { + return false; + } + } + } + private static async Task InstallVcredist2013IfNeeded(ApplicationHost appHost, ILogger logger) { // Reference From 71386f0ceb15ce0bac2e588f90781a4bd274fe68 Mon Sep 17 00:00:00 2001 From: hatharry Date: Mon, 29 Aug 2016 01:42:25 +1200 Subject: [PATCH 2/2] Check service running before checking self restart --- MediaBrowser.ServerApplication/MainStartup.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.cs index d56558c72c..e4f5f3a694 100644 --- a/MediaBrowser.ServerApplication/MainStartup.cs +++ b/MediaBrowser.ServerApplication/MainStartup.cs @@ -45,7 +45,11 @@ namespace MediaBrowser.ServerApplication { var options = new StartupOptions(); _isRunningAsService = options.ContainsOption("-service"); - _canRestartService = CanRestartWindowsService(); + + if (_isRunningAsService) + { + _canRestartService = CanRestartWindowsService(); + } var currentProcess = Process.GetCurrentProcess();