mirror of
https://github.com/Sonarr/Sonarr.git
synced 2024-12-16 11:37:58 +02:00
cleaned up some code around process handling.
This commit is contained in:
parent
e24c85cc1c
commit
f9fe119af2
@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
|
||||||
using FizzWare.NBuilder;
|
using FizzWare.NBuilder;
|
||||||
using Moq;
|
using Moq;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
@ -13,10 +12,10 @@ namespace NzbDrone.App.Test
|
|||||||
public class MonitoringProviderTest : TestBase<PriorityMonitor>
|
public class MonitoringProviderTest : TestBase<PriorityMonitor>
|
||||||
{
|
{
|
||||||
[Test]
|
[Test]
|
||||||
public void Ensure_priority_doesnt_fail_on_invalid_iis_proccess_id()
|
public void Ensure_priority_doesnt_fail_on_invalid_process_id()
|
||||||
{
|
{
|
||||||
Mocker.GetMock<IProcessProvider>().Setup(c => c.GetCurrentProcess())
|
Mocker.GetMock<IProcessProvider>().Setup(c => c.GetCurrentProcess())
|
||||||
.Returns(Builder<ProcessInfo>.CreateNew().With(c => c.Priority == ProcessPriorityClass.Normal).Build());
|
.Returns(Builder<ProcessInfo>.CreateNew().Build());
|
||||||
|
|
||||||
Mocker.GetMock<IProcessProvider>().Setup(c => c.GetProcessById(It.IsAny<int>())).Returns((ProcessInfo)null);
|
Mocker.GetMock<IProcessProvider>().Setup(c => c.GetProcessById(It.IsAny<int>())).Returns((ProcessInfo)null);
|
||||||
|
|
||||||
|
@ -51,12 +51,12 @@ public void Should_be_able_to_start_process()
|
|||||||
var startInfo = new ProcessStartInfo(DummyApp.DUMMY_PROCCESS_NAME + ".exe");
|
var startInfo = new ProcessStartInfo(DummyApp.DUMMY_PROCCESS_NAME + ".exe");
|
||||||
|
|
||||||
|
|
||||||
Subject.GetProcessByName(DummyApp.DUMMY_PROCCESS_NAME).Should()
|
Subject.Exists(DummyApp.DUMMY_PROCCESS_NAME).Should()
|
||||||
.BeEmpty("Dummy process is already running");
|
.BeFalse("Dummy process is already running");
|
||||||
Subject.Start(startInfo).Should().NotBeNull();
|
Subject.Start(startInfo).Should().NotBeNull();
|
||||||
|
|
||||||
Subject.GetProcessByName(DummyApp.DUMMY_PROCCESS_NAME).Should()
|
Subject.Exists(DummyApp.DUMMY_PROCCESS_NAME).Should()
|
||||||
.HaveCount(1, "excepted one dummy process to be already running");
|
.BeTrue("excepted one dummy process to be already running");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@ -83,6 +83,5 @@ public void ToString_on_new_processInfo()
|
|||||||
Console.WriteLine(new ProcessInfo().ToString());
|
Console.WriteLine(new ProcessInfo().ToString());
|
||||||
ExceptionVerification.MarkInconclusive(typeof(Win32Exception));
|
ExceptionVerification.MarkInconclusive(typeof(Win32Exception));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,14 @@
|
|||||||
using System.Diagnostics;
|
namespace NzbDrone.Common.Model
|
||||||
|
|
||||||
namespace NzbDrone.Common.Model
|
|
||||||
{
|
{
|
||||||
public class ProcessInfo
|
public class ProcessInfo
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string StartPath { get; set; }
|
public string StartPath { get; set; }
|
||||||
public ProcessPriorityClass Priority { get; set; }
|
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return string.Format("{0}:{1} [{2}] [{3}]", Id, Name ?? "Unknown", StartPath ?? "Unknown", Priority);
|
return string.Format("{0}:{1} [{2}]", Id, Name ?? "Unknown", StartPath ?? "Unknown");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,8 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.ComponentModel;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using NLog;
|
using NLog;
|
||||||
using NzbDrone.Common.EnvironmentInfo;
|
|
||||||
using NzbDrone.Common.Model;
|
using NzbDrone.Common.Model;
|
||||||
|
|
||||||
namespace NzbDrone.Common
|
namespace NzbDrone.Common
|
||||||
@ -11,12 +10,13 @@ public interface IProcessProvider
|
|||||||
{
|
{
|
||||||
ProcessInfo GetCurrentProcess();
|
ProcessInfo GetCurrentProcess();
|
||||||
ProcessInfo GetProcessById(int id);
|
ProcessInfo GetProcessById(int id);
|
||||||
IEnumerable<ProcessInfo> GetProcessByName(string name);
|
|
||||||
Process Start(string path);
|
Process Start(string path);
|
||||||
Process Start(ProcessStartInfo startInfo);
|
Process Start(ProcessStartInfo startInfo);
|
||||||
void WaitForExit(Process process);
|
void WaitForExit(Process process);
|
||||||
void SetPriority(int processId, ProcessPriorityClass priority);
|
void SetPriority(int processId, ProcessPriorityClass priority);
|
||||||
void KillAll(string processName);
|
void KillAll(string processName);
|
||||||
|
bool Exists(string processName);
|
||||||
|
ProcessPriorityClass GetCurrentProcessPriority();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ProcessProvider : IProcessProvider
|
public class ProcessProvider : IProcessProvider
|
||||||
@ -31,6 +31,16 @@ public ProcessInfo GetCurrentProcess()
|
|||||||
return ConvertToProcessInfo(Process.GetCurrentProcess());
|
return ConvertToProcessInfo(Process.GetCurrentProcess());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool Exists(string processName)
|
||||||
|
{
|
||||||
|
return Process.GetProcessesByName(processName).Any();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProcessPriorityClass GetCurrentProcessPriority()
|
||||||
|
{
|
||||||
|
return Process.GetCurrentProcess().PriorityClass;
|
||||||
|
}
|
||||||
|
|
||||||
public ProcessInfo GetProcessById(int id)
|
public ProcessInfo GetProcessById(int id)
|
||||||
{
|
{
|
||||||
Logger.Trace("Finding process with Id:{0}", id);
|
Logger.Trace("Finding process with Id:{0}", id);
|
||||||
@ -49,7 +59,7 @@ public ProcessInfo GetProcessById(int id)
|
|||||||
return processInfo;
|
return processInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<ProcessInfo> GetProcessByName(string name)
|
/* public IEnumerable<ProcessInfo> GetProcessByName(string name)
|
||||||
{
|
{
|
||||||
if (OsInfo.IsMono)
|
if (OsInfo.IsMono)
|
||||||
{
|
{
|
||||||
@ -60,7 +70,7 @@ public IEnumerable<ProcessInfo> GetProcessByName(string name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return Process.GetProcessesByName(name).Select(ConvertToProcessInfo).Where(p => p != null);
|
return Process.GetProcessesByName(name).Select(ConvertToProcessInfo).Where(p => p != null);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
public Process Start(string path)
|
public Process Start(string path)
|
||||||
{
|
{
|
||||||
@ -102,7 +112,7 @@ public void SetPriority(int processId, ProcessPriorityClass priority)
|
|||||||
|
|
||||||
public void KillAll(string processName)
|
public void KillAll(string processName)
|
||||||
{
|
{
|
||||||
var processToKill = GetProcessByName(processName);
|
var processToKill = Process.GetProcessesByName(processName);
|
||||||
|
|
||||||
foreach (var processInfo in processToKill)
|
foreach (var processInfo in processToKill)
|
||||||
{
|
{
|
||||||
@ -113,16 +123,28 @@ public void KillAll(string processName)
|
|||||||
|
|
||||||
private static ProcessInfo ConvertToProcessInfo(Process process)
|
private static ProcessInfo ConvertToProcessInfo(Process process)
|
||||||
{
|
{
|
||||||
if (process == null || process.Id <= 0 || process.HasExited) return null;
|
if (process == null) return null;
|
||||||
|
|
||||||
|
process.Refresh();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (process.Id <= 0 || process.HasExited) return null;
|
||||||
|
|
||||||
return new ProcessInfo
|
return new ProcessInfo
|
||||||
{
|
{
|
||||||
Id = process.Id,
|
Id = process.Id,
|
||||||
Priority = process.PriorityClass,
|
|
||||||
StartPath = process.MainModule.FileName,
|
StartPath = process.MainModule.FileName,
|
||||||
Name = process.ProcessName
|
Name = process.ProcessName
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
catch (Win32Exception)
|
||||||
|
{
|
||||||
|
Logger.Warn("Coudn't get process info for " + process.ProcessName);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
using System.Linq;
|
|
||||||
using NzbDrone.Common;
|
using NzbDrone.Common;
|
||||||
|
|
||||||
namespace NzbDrone.Update.UpdateEngine
|
namespace NzbDrone.Update.UpdateEngine
|
||||||
@ -27,7 +26,7 @@ public AppType GetAppType()
|
|||||||
return AppType.Service;
|
return AppType.Service;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_processProvider.GetProcessByName(ProcessProvider.NzbDroneConsoleProcessName).Any())
|
if (_processProvider.Exists(ProcessProvider.NzbDroneConsoleProcessName))
|
||||||
{
|
{
|
||||||
return AppType.Console;
|
return AppType.Console;
|
||||||
}
|
}
|
||||||
|
@ -30,17 +30,9 @@ public virtual void EnsurePriority(object sender)
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var currentProcess = _processProvider.GetCurrentProcess();
|
if (_processProvider.GetCurrentProcessPriority() != ProcessPriorityClass.Normal)
|
||||||
if (currentProcess.Priority != ProcessPriorityClass.Normal)
|
|
||||||
{
|
{
|
||||||
_processProvider.SetPriority(currentProcess.Id, ProcessPriorityClass.Normal);
|
_processProvider.SetPriority(_processProvider.GetCurrentProcess().Id, ProcessPriorityClass.Normal);
|
||||||
}
|
|
||||||
|
|
||||||
var iisProcess = _processProvider.GetProcessById(_processProvider.GetCurrentProcess().Id);
|
|
||||||
if (iisProcess != null && iisProcess.Priority != ProcessPriorityClass.Normal &&
|
|
||||||
iisProcess.Priority != ProcessPriorityClass.AboveNormal)
|
|
||||||
{
|
|
||||||
_processProvider.SetPriority(iisProcess.Id, ProcessPriorityClass.Normal);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
Loading…
Reference in New Issue
Block a user