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;
|
2011-10-23 08:26:43 +03:00
|
|
|
using NzbDrone.Common.Model;
|
2011-10-07 06:37:41 +03:00
|
|
|
|
2011-10-23 08:26:43 +03:00
|
|
|
namespace NzbDrone.Common
|
2011-10-07 06:37:41 +03:00
|
|
|
{
|
|
|
|
public class ProcessProvider
|
|
|
|
{
|
2012-01-23 05:24:16 +03:00
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-10-07 06:37:41 +03:00
|
|
|
|
2011-10-23 21:31:17 +03:00
|
|
|
public static readonly string NzbDroneProccessName = "NzbDrone";
|
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)
|
|
|
|
{
|
2011-12-12 09:52:58 +03:00
|
|
|
Logger.Trace("Finding process with Id:{0}", id);
|
|
|
|
|
|
|
|
var processInfo = ConvertToProcessInfo(Process.GetProcesses().Where(p => p.Id == id).FirstOrDefault());
|
|
|
|
|
|
|
|
if (processInfo == null)
|
|
|
|
{
|
|
|
|
Logger.Warn("Unable to find process with ID {0}", id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Logger.Trace("Found process {0}", processInfo.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
return processInfo;
|
2011-10-07 09:36:04 +03:00
|
|
|
}
|
2011-10-07 06:37:41 +03:00
|
|
|
|
2011-10-07 09:36:04 +03:00
|
|
|
public virtual IEnumerable<ProcessInfo> GetProcessByName(string name)
|
|
|
|
{
|
2012-01-18 23:57:08 +03:00
|
|
|
return Process.GetProcessesByName(name).Select(ConvertToProcessInfo).Where(p => p != null);
|
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
|
|
|
}
|
|
|
|
|
2012-02-27 00:22:35 +03:00
|
|
|
public virtual void WaitForExit(Process process)
|
|
|
|
{
|
|
|
|
Logger.Trace("Waiting for process {0} to exit.", process.ProcessName);
|
|
|
|
process.WaitForExit();
|
|
|
|
}
|
|
|
|
|
2011-10-07 09:36:04 +03:00
|
|
|
public virtual void Kill(int processId)
|
2011-10-07 06:37:41 +03:00
|
|
|
{
|
2011-11-21 04:13:58 +03:00
|
|
|
if (processId == 0 || !Process.GetProcesses().Any(p => p.Id == processId))
|
|
|
|
{
|
|
|
|
Logger.Warn("Cannot find process with id: {0}", processId);
|
|
|
|
return;
|
|
|
|
}
|
2011-10-07 09:36:04 +03:00
|
|
|
|
|
|
|
var process = Process.GetProcessById(processId);
|
|
|
|
|
|
|
|
if (!process.HasExited)
|
|
|
|
{
|
2011-11-21 04:13:58 +03:00
|
|
|
Logger.Info("[{0}]: Killing process", process.Id);
|
2011-10-07 09:36:04 +03:00
|
|
|
process.Kill();
|
2011-11-21 04:13:58 +03:00
|
|
|
Logger.Info("[{0}]: Waiting for exit", process.Id);
|
2011-10-07 09:36:04 +03:00
|
|
|
process.WaitForExit();
|
2011-11-21 04:13:58 +03:00
|
|
|
Logger.Info("[{0}]: Process terminated successfully", process.Id);
|
2011-10-07 09:36:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2012-01-07 00:11:45 +03:00
|
|
|
if (process == null || process.Id <= 0 || process.HasExited) 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,
|
2011-12-12 09:52:58 +03:00
|
|
|
StartPath = process.MainModule.FileName,
|
|
|
|
Name = process.ProcessName
|
2011-10-07 09:57:43 +03:00
|
|
|
};
|
2011-10-07 06:37:41 +03:00
|
|
|
}
|
2011-11-21 04:13:58 +03:00
|
|
|
|
2011-10-07 06:37:41 +03:00
|
|
|
}
|
2011-10-07 09:57:43 +03:00
|
|
|
}
|