From 1f443097b7fa6f9ff829157120c4589d3ed65754 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 13 Jul 2016 22:36:42 -0400 Subject: improve prevention of simultaneous instances --- MediaBrowser.ServerApplication/MainStartup.cs | 58 ++++++++++++++++++++-- .../MediaBrowser.ServerApplication.csproj | 1 + 2 files changed, 55 insertions(+), 4 deletions(-) (limited to 'MediaBrowser.ServerApplication') diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.cs index fb4fb86ffd..0f2fea1556 100644 --- a/MediaBrowser.ServerApplication/MainStartup.cs +++ b/MediaBrowser.ServerApplication/MainStartup.cs @@ -12,6 +12,7 @@ using System.Configuration.Install; using System.Diagnostics; using System.IO; using System.Linq; +using System.Management; using System.Runtime.InteropServices; using System.ServiceProcess; using System.Threading; @@ -102,7 +103,7 @@ namespace MediaBrowser.ServerApplication if (IsAlreadyRunning(applicationPath, currentProcess)) { - logger.Info("Shutting down because another instance of Media Browser Server is already running."); + logger.Info("Shutting down because another instance of Emby Server is already running."); return; } @@ -130,13 +131,28 @@ namespace MediaBrowser.ServerApplication /// true if [is already running] [the specified current process]; otherwise, false. private static bool IsAlreadyRunning(string applicationPath, Process currentProcess) { - var filename = Path.GetFileName(applicationPath); - var duplicate = Process.GetProcesses().FirstOrDefault(i => { try { - return string.Equals(filename, Path.GetFileName(i.MainModule.FileName)) && currentProcess.Id != i.Id; + if (currentProcess.Id == i.Id) + { + return false; + } + } + catch (Exception) + { + return false; + } + + try + { + //_logger.Info("Module: {0}", i.MainModule.FileName); + if (string.Equals(applicationPath, i.MainModule.FileName, StringComparison.OrdinalIgnoreCase)) + { + return true; + } + return false; } catch (Exception) { @@ -155,6 +171,40 @@ namespace MediaBrowser.ServerApplication } } + if (!_isRunningAsService) + { + return IsAlreadyRunningAsService(applicationPath); + } + + return false; + } + + private static bool IsAlreadyRunningAsService(string applicationPath) + { + var serviceName = BackgroundService.GetExistingServiceName(); + + WqlObjectQuery wqlObjectQuery = new WqlObjectQuery(string.Format("SELECT * FROM Win32_Service WHERE State = 'Running' AND Name = '{0}'", serviceName)); + ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(wqlObjectQuery); + ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get(); + + foreach (ManagementObject managementObject in managementObjectCollection) + { + var obj = managementObject.GetPropertyValue("PathName"); + if (obj == null) + { + continue; + } + var path = obj.ToString(); + + _logger.Info("Service path: {0}", path); + // Need to use indexOf instead of equality because the path will have the full service command line + if (path.IndexOf(applicationPath, StringComparison.OrdinalIgnoreCase) != -1) + { + _logger.Info("The windows service is already running"); + return true; + } + } + return false; } diff --git a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj index fcd6f7fafe..b01d8c43f6 100644 --- a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj +++ b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj @@ -88,6 +88,7 @@ True + -- cgit v1.2.3 From 7e52b0d3041bd0881cd9b8c6e2e1103a700b57be Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 13 Jul 2016 23:36:23 -0400 Subject: add service message --- MediaBrowser.Server.Implementations/Dto/DtoService.cs | 2 +- MediaBrowser.ServerApplication/MainStartup.cs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'MediaBrowser.ServerApplication') diff --git a/MediaBrowser.Server.Implementations/Dto/DtoService.cs b/MediaBrowser.Server.Implementations/Dto/DtoService.cs index de6c23cac6..975e292f0d 100644 --- a/MediaBrowser.Server.Implementations/Dto/DtoService.cs +++ b/MediaBrowser.Server.Implementations/Dto/DtoService.cs @@ -1322,7 +1322,7 @@ namespace MediaBrowser.Server.Implementations.Dto } } - if (fields.Contains(ItemFields.SeriesPrimaryImage)) + //if (fields.Contains(ItemFields.SeriesPrimaryImage)) { episodeSeries = episodeSeries ?? episode.Series; if (episodeSeries != null) diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.cs index 0f2fea1556..2440eab7ce 100644 --- a/MediaBrowser.ServerApplication/MainStartup.cs +++ b/MediaBrowser.ServerApplication/MainStartup.cs @@ -201,6 +201,7 @@ namespace MediaBrowser.ServerApplication if (path.IndexOf(applicationPath, StringComparison.OrdinalIgnoreCase) != -1) { _logger.Info("The windows service is already running"); + MessageBox.Show("Emby Server is already running as a Windows Service. Only one instance is allowed at a time. To run as a tray icon, shut down the Windows Service."); return true; } } -- cgit v1.2.3 From d08f7df3c3607193252889a79b635a48e993fb37 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 18 Jul 2016 23:58:13 -0400 Subject: update vcc 2013 check --- MediaBrowser.ServerApplication/MainStartup.cs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'MediaBrowser.ServerApplication') diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.cs index 2440eab7ce..bdfd7d1bb0 100644 --- a/MediaBrowser.ServerApplication/MainStartup.cs +++ b/MediaBrowser.ServerApplication/MainStartup.cs @@ -644,14 +644,32 @@ namespace MediaBrowser.ServerApplication private static async Task InstallVcredist2013IfNeeded(ApplicationHost appHost, ILogger logger) { + // Reference + // http://stackoverflow.com/questions/12206314/detect-if-visual-c-redistributable-for-visual-studio-2012-is-installed + try { - var version = ImageMagickEncoder.GetVersion(); - return; + var subkey = Environment.Is64BitProcess + ? "SOFTWARE\\WOW6432Node\\Microsoft\\VisualStudio\\12.0\\VC\\Runtimes\\x64" + : "SOFTWARE\\Microsoft\\VisualStudio\\12.0\\VC\\Runtimes\\x86"; + + using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default) + .OpenSubKey(subkey)) + { + if (ndpKey != null && ndpKey.GetValue("Version") != null) + { + var installedVersion = ((string)ndpKey.GetValue("Version")).TrimStart('v'); + if (installedVersion.StartsWith("12", StringComparison.OrdinalIgnoreCase)) + { + return; + } + } + } } catch (Exception ex) { - logger.ErrorException("Error loading ImageMagick", ex); + logger.ErrorException("Error getting .NET Framework version", ex); + return; } try -- cgit v1.2.3