mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-09-06 05:47:14 -04:00
improve prevention of simultaneous instances
This commit is contained in:
parent
e44a24d9e5
commit
1f443097b7
@ -274,7 +274,7 @@ namespace MediaBrowser.Server.Startup.Common
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return "Media Browser Server";
|
return "Emby Server";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ using System.Configuration.Install;
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Management;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.ServiceProcess;
|
using System.ServiceProcess;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@ -102,7 +103,7 @@ namespace MediaBrowser.ServerApplication
|
|||||||
|
|
||||||
if (IsAlreadyRunning(applicationPath, currentProcess))
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,13 +131,28 @@ namespace MediaBrowser.ServerApplication
|
|||||||
/// <returns><c>true</c> if [is already running] [the specified current process]; otherwise, <c>false</c>.</returns>
|
/// <returns><c>true</c> if [is already running] [the specified current process]; otherwise, <c>false</c>.</returns>
|
||||||
private static bool IsAlreadyRunning(string applicationPath, Process currentProcess)
|
private static bool IsAlreadyRunning(string applicationPath, Process currentProcess)
|
||||||
{
|
{
|
||||||
var filename = Path.GetFileName(applicationPath);
|
|
||||||
|
|
||||||
var duplicate = Process.GetProcesses().FirstOrDefault(i =>
|
var duplicate = Process.GetProcesses().FirstOrDefault(i =>
|
||||||
{
|
{
|
||||||
try
|
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)
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,6 +88,7 @@
|
|||||||
<Private>True</Private>
|
<Private>True</Private>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="System.Drawing" />
|
<Reference Include="System.Drawing" />
|
||||||
|
<Reference Include="System.Management" />
|
||||||
<Reference Include="System.ServiceProcess" />
|
<Reference Include="System.ServiceProcess" />
|
||||||
<Reference Include="System.Windows.Forms" />
|
<Reference Include="System.Windows.Forms" />
|
||||||
<Reference Include="System.Xml.Linq" />
|
<Reference Include="System.Xml.Linq" />
|
||||||
|
Loading…
Reference in New Issue
Block a user