1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-12 11:15:43 +02:00
Sonarr/NzbDrone/Providers/ProcessProvider.cs

87 lines
2.6 KiB
C#
Raw Normal View History

2011-10-07 09:36:04 +03:00
using System.Collections.Generic;
2011-10-07 06:37:41 +03:00
using System.Diagnostics;
using System.Linq;
using NLog;
using NzbDrone.Model;
2011-10-07 06:37:41 +03:00
namespace NzbDrone.Providers
{
public class ProcessProvider
{
2011-10-07 09:43:35 +03:00
private static readonly Logger Logger = LogManager.GetLogger("Host.ProcessProvider");
2011-10-07 06:37:41 +03:00
2011-10-07 09:36:04 +03:00
public virtual ProcessInfo GetCurrentProcess()
2011-10-07 06:37:41 +03:00
{
2011-10-07 09:36:04 +03:00
return ConvertToProcessInfo(Process.GetCurrentProcess());
}
2011-10-07 06:37:41 +03:00
2011-10-07 09:36:04 +03:00
public virtual ProcessInfo GetProcessById(int id)
{
return ConvertToProcessInfo(Process.GetProcesses().Where(p => p.Id == id).FirstOrDefault());
}
2011-10-07 06:37:41 +03:00
2011-10-07 09:36:04 +03:00
public virtual IEnumerable<ProcessInfo> GetProcessByName(string name)
{
return Process.GetProcessesByName(name).Select(ConvertToProcessInfo);
2011-10-07 06:37:41 +03:00
}
2011-10-07 09:36:04 +03:00
public virtual void Start(string path)
2011-10-07 06:37:41 +03:00
{
2011-10-07 09:36:04 +03:00
Process.Start(path);
2011-10-07 06:37:41 +03:00
}
2011-10-07 09:36:04 +03:00
public virtual Process Start(ProcessStartInfo startInfo)
2011-10-07 06:37:41 +03:00
{
2011-10-07 09:36:04 +03:00
Logger.Info("Starting process. [{0}]", startInfo.FileName);
var process = new Process
{
StartInfo = startInfo
};
process.Start();
return process;
2011-10-07 06:37:41 +03:00
}
2011-10-07 09:36:04 +03:00
public virtual void Kill(int processId)
2011-10-07 06:37:41 +03:00
{
2011-10-07 09:36:04 +03:00
if (processId == 0) return;
if (!Process.GetProcesses().Any(p => p.Id == processId)) return;
var process = Process.GetProcessById(processId);
if (!process.HasExited)
{
Logger.Info("[{0}]Killing process", process.Id);
process.Kill();
Logger.Info("[{0}]Waiting for exit", process.Id);
process.WaitForExit();
Logger.Info("[{0}]Process terminated successfully", process.Id);
}
}
public virtual void SetPriority(int processId, ProcessPriorityClass priority)
{
var process = Process.GetProcessById(processId);
Logger.Info("Updating [{0}] process priority from {1} to {2}",
process.ProcessName,
process.PriorityClass,
priority);
process.PriorityClass = priority;
}
private static ProcessInfo ConvertToProcessInfo(Process process)
{
2011-10-08 08:06:16 +03:00
if (process == null || process.Id <= 0) return null;
2011-10-07 09:36:04 +03:00
return new ProcessInfo
2011-10-07 09:57:43 +03:00
{
Id = process.Id,
Priority = process.PriorityClass,
StartPath = process.MainModule.FileName
};
2011-10-07 06:37:41 +03:00
}
}
2011-10-07 09:57:43 +03:00
}