cleaned up some code around process handling.

pull/4/head
Keivan Beigi 11 years ago
parent e24c85cc1c
commit f9fe119af2

@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;
@ -13,10 +12,10 @@ namespace NzbDrone.App.Test
public class MonitoringProviderTest : TestBase<PriorityMonitor>
{
[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())
.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);

@ -51,12 +51,12 @@ namespace NzbDrone.Common.Test
var startInfo = new ProcessStartInfo(DummyApp.DUMMY_PROCCESS_NAME + ".exe");
Subject.GetProcessByName(DummyApp.DUMMY_PROCCESS_NAME).Should()
.BeEmpty("Dummy process is already running");
Subject.Exists(DummyApp.DUMMY_PROCCESS_NAME).Should()
.BeFalse("Dummy process is already running");
Subject.Start(startInfo).Should().NotBeNull();
Subject.GetProcessByName(DummyApp.DUMMY_PROCCESS_NAME).Should()
.HaveCount(1, "excepted one dummy process to be already running");
Subject.Exists(DummyApp.DUMMY_PROCCESS_NAME).Should()
.BeTrue("excepted one dummy process to be already running");
}
[Test]
@ -83,6 +83,5 @@ namespace NzbDrone.Common.Test
Console.WriteLine(new ProcessInfo().ToString());
ExceptionVerification.MarkInconclusive(typeof(Win32Exception));
}
}
}

@ -1,17 +1,14 @@
using System.Diagnostics;
namespace NzbDrone.Common.Model
namespace NzbDrone.Common.Model
{
public class ProcessInfo
{
public int Id { get; set; }
public string Name { get; set; }
public string StartPath { get; set; }
public ProcessPriorityClass Priority { get; set; }
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.Linq;
using NLog;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Model;
namespace NzbDrone.Common
@ -11,12 +10,13 @@ namespace NzbDrone.Common
{
ProcessInfo GetCurrentProcess();
ProcessInfo GetProcessById(int id);
IEnumerable<ProcessInfo> GetProcessByName(string name);
Process Start(string path);
Process Start(ProcessStartInfo startInfo);
void WaitForExit(Process process);
void SetPriority(int processId, ProcessPriorityClass priority);
void KillAll(string processName);
bool Exists(string processName);
ProcessPriorityClass GetCurrentProcessPriority();
}
public class ProcessProvider : IProcessProvider
@ -31,6 +31,16 @@ namespace NzbDrone.Common
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)
{
Logger.Trace("Finding process with Id:{0}", id);
@ -49,18 +59,18 @@ namespace NzbDrone.Common
return processInfo;
}
public IEnumerable<ProcessInfo> GetProcessByName(string name)
{
if (OsInfo.IsMono)
{
var mono = Process.GetProcessesByName("mono");
/* public IEnumerable<ProcessInfo> GetProcessByName(string name)
{
if (OsInfo.IsMono)
{
var mono = Process.GetProcessesByName("mono");
return mono.Where(process => process.Modules.Cast<ProcessModule>().Any(module => module.ModuleName.ToLower() == name + ".exe"))
.Select(ConvertToProcessInfo);
}
return mono.Where(process => process.Modules.Cast<ProcessModule>().Any(module => module.ModuleName.ToLower() == name + ".exe"))
.Select(ConvertToProcessInfo);
}
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)
{
@ -102,7 +112,7 @@ namespace NzbDrone.Common
public void KillAll(string processName)
{
var processToKill = GetProcessByName(processName);
var processToKill = Process.GetProcessesByName(processName);
foreach (var processInfo in processToKill)
{
@ -113,15 +123,27 @@ namespace NzbDrone.Common
private static ProcessInfo ConvertToProcessInfo(Process process)
{
if (process == null || process.Id <= 0 || process.HasExited) return null;
return new ProcessInfo
{
Id = process.Id,
Priority = process.PriorityClass,
StartPath = process.MainModule.FileName,
Name = process.ProcessName
};
if (process == null) return null;
process.Refresh();
try
{
if (process.Id <= 0 || process.HasExited) return null;
return new ProcessInfo
{
Id = process.Id,
StartPath = process.MainModule.FileName,
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;
namespace NzbDrone.Update.UpdateEngine
@ -27,7 +26,7 @@ namespace NzbDrone.Update.UpdateEngine
return AppType.Service;
}
if (_processProvider.GetProcessByName(ProcessProvider.NzbDroneConsoleProcessName).Any())
if (_processProvider.Exists(ProcessProvider.NzbDroneConsoleProcessName))
{
return AppType.Console;
}

@ -30,17 +30,9 @@ namespace NzbDrone
{
try
{
var currentProcess = _processProvider.GetCurrentProcess();
if (currentProcess.Priority != ProcessPriorityClass.Normal)
if (_processProvider.GetCurrentProcessPriority() != ProcessPriorityClass.Normal)
{
_processProvider.SetPriority(currentProcess.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);
_processProvider.SetPriority(_processProvider.GetCurrentProcess().Id, ProcessPriorityClass.Normal);
}
}
catch (Exception e)

Loading…
Cancel
Save