1
0
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:
Keivan Beigi 2013-07-30 13:19:09 -07:00
parent e24c85cc1c
commit f9fe119af2
6 changed files with 55 additions and 47 deletions

View File

@ -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);

View File

@ -51,12 +51,12 @@ public void Should_be_able_to_start_process()
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 @@ public void ToString_on_new_processInfo()
Console.WriteLine(new ProcessInfo().ToString());
ExceptionVerification.MarkInconclusive(typeof(Win32Exception));
}
}
}

View File

@ -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");
}
}
}

View File

@ -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 @@ public interface IProcessProvider
{
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 @@ public ProcessInfo 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)
{
Logger.Trace("Finding process with Id:{0}", id);
@ -49,7 +59,7 @@ public ProcessInfo GetProcessById(int id)
return processInfo;
}
public IEnumerable<ProcessInfo> GetProcessByName(string name)
/* public IEnumerable<ProcessInfo> GetProcessByName(string name)
{
if (OsInfo.IsMono)
{
@ -60,7 +70,7 @@ public IEnumerable<ProcessInfo> GetProcessByName(string name)
}
return Process.GetProcessesByName(name).Select(ConvertToProcessInfo).Where(p => p != null);
}
}*/
public Process Start(string path)
{
@ -102,7 +112,7 @@ public void SetPriority(int processId, ProcessPriorityClass priority)
public void KillAll(string processName)
{
var processToKill = GetProcessByName(processName);
var processToKill = Process.GetProcessesByName(processName);
foreach (var processInfo in processToKill)
{
@ -113,16 +123,28 @@ public void KillAll(string processName)
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
{
Id = process.Id,
Priority = process.PriorityClass,
StartPath = process.MainModule.FileName,
Name = process.ProcessName
};
}
catch (Win32Exception)
{
Logger.Warn("Coudn't get process info for " + process.ProcessName);
}
return null;
}

View File

@ -1,4 +1,3 @@
using System.Linq;
using NzbDrone.Common;
namespace NzbDrone.Update.UpdateEngine
@ -27,7 +26,7 @@ public AppType GetAppType()
return AppType.Service;
}
if (_processProvider.GetProcessByName(ProcessProvider.NzbDroneConsoleProcessName).Any())
if (_processProvider.Exists(ProcessProvider.NzbDroneConsoleProcessName))
{
return AppType.Console;
}

View File

@ -30,17 +30,9 @@ public virtual void EnsurePriority(object sender)
{
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)