2013-05-07 07:38:40 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2011-10-07 05:37:41 +02:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NLog;
|
2011-10-23 07:26:43 +02:00
|
|
|
|
using NzbDrone.Common.Model;
|
2011-10-07 05:37:41 +02:00
|
|
|
|
|
2011-10-23 07:26:43 +02:00
|
|
|
|
namespace NzbDrone.Common
|
2011-10-07 05:37:41 +02:00
|
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
|
public interface IProcessProvider
|
|
|
|
|
{
|
|
|
|
|
ProcessInfo GetCurrentProcess();
|
|
|
|
|
ProcessInfo GetProcessById(int id);
|
|
|
|
|
IEnumerable<ProcessInfo> GetProcessByName(string name);
|
|
|
|
|
void Start(string path);
|
|
|
|
|
Process Start(ProcessStartInfo startInfo);
|
|
|
|
|
void WaitForExit(Process process);
|
|
|
|
|
void Kill(int processId);
|
|
|
|
|
void SetPriority(int processId, ProcessPriorityClass priority);
|
|
|
|
|
void KillAll(string processName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ProcessProvider : IProcessProvider
|
2011-10-07 05:37:41 +02:00
|
|
|
|
{
|
2012-01-23 03:24:16 +01:00
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-10-07 05:37:41 +02:00
|
|
|
|
|
2013-04-18 01:32:53 +02:00
|
|
|
|
public const string NzbDroneProcessName = "NzbDrone";
|
|
|
|
|
public const string NzbDroneConsoleProcessName = "NzbDrone.Console";
|
2011-10-07 05:37:41 +02:00
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
public ProcessInfo GetCurrentProcess()
|
2011-10-07 05:37:41 +02:00
|
|
|
|
{
|
2011-10-07 08:36:04 +02:00
|
|
|
|
return ConvertToProcessInfo(Process.GetCurrentProcess());
|
|
|
|
|
}
|
2011-10-07 05:37:41 +02:00
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
public ProcessInfo GetProcessById(int id)
|
2011-10-07 08:36:04 +02:00
|
|
|
|
{
|
2011-12-12 07:52:58 +01:00
|
|
|
|
Logger.Trace("Finding process with Id:{0}", id);
|
|
|
|
|
|
2013-05-07 07:54:21 +02:00
|
|
|
|
var processInfo = ConvertToProcessInfo(Process.GetProcesses().FirstOrDefault(p => p.Id == id));
|
2011-12-12 07:52:58 +01:00
|
|
|
|
|
|
|
|
|
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 08:36:04 +02:00
|
|
|
|
}
|
2011-10-07 05:37:41 +02:00
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
public IEnumerable<ProcessInfo> GetProcessByName(string name)
|
2011-10-07 08:36:04 +02:00
|
|
|
|
{
|
2012-01-18 21:57:08 +01:00
|
|
|
|
return Process.GetProcessesByName(name).Select(ConvertToProcessInfo).Where(p => p != null);
|
2011-10-07 05:37:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
public void Start(string path)
|
2011-10-07 05:37:41 +02:00
|
|
|
|
{
|
2011-10-07 08:36:04 +02:00
|
|
|
|
Process.Start(path);
|
2011-10-07 05:37:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
public Process Start(ProcessStartInfo startInfo)
|
2011-10-07 05:37:41 +02:00
|
|
|
|
{
|
2011-10-07 08:36:04 +02:00
|
|
|
|
Logger.Info("Starting process. [{0}]", startInfo.FileName);
|
|
|
|
|
|
|
|
|
|
var process = new Process
|
|
|
|
|
{
|
|
|
|
|
StartInfo = startInfo
|
|
|
|
|
};
|
|
|
|
|
process.Start();
|
|
|
|
|
return process;
|
2011-10-07 05:37:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
public void WaitForExit(Process process)
|
2012-02-26 22:22:35 +01:00
|
|
|
|
{
|
|
|
|
|
Logger.Trace("Waiting for process {0} to exit.", process.ProcessName);
|
|
|
|
|
process.WaitForExit();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
public void Kill(int processId)
|
2011-10-07 05:37:41 +02:00
|
|
|
|
{
|
2013-04-18 01:32:53 +02:00
|
|
|
|
if (processId == 0 || Process.GetProcesses().All(p => p.Id != processId))
|
2011-11-21 02:13:58 +01:00
|
|
|
|
{
|
|
|
|
|
Logger.Warn("Cannot find process with id: {0}", processId);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-10-07 08:36:04 +02:00
|
|
|
|
|
|
|
|
|
var process = Process.GetProcessById(processId);
|
|
|
|
|
|
2013-04-18 01:32:53 +02:00
|
|
|
|
if (process.HasExited)
|
2011-10-07 08:36:04 +02:00
|
|
|
|
{
|
2013-04-18 01:32:53 +02:00
|
|
|
|
return;
|
2011-10-07 08:36:04 +02:00
|
|
|
|
}
|
2013-04-18 01:32:53 +02:00
|
|
|
|
|
|
|
|
|
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);
|
2011-10-07 08:36:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
public void SetPriority(int processId, ProcessPriorityClass priority)
|
2011-10-07 08:36:04 +02:00
|
|
|
|
{
|
|
|
|
|
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-06 22:11:45 +01:00
|
|
|
|
if (process == null || process.Id <= 0 || process.HasExited) return null;
|
2011-10-07 08:36:04 +02:00
|
|
|
|
|
|
|
|
|
return new ProcessInfo
|
2011-10-07 08:57:43 +02:00
|
|
|
|
{
|
|
|
|
|
Id = process.Id,
|
|
|
|
|
Priority = process.PriorityClass,
|
2011-12-12 07:52:58 +01:00
|
|
|
|
StartPath = process.MainModule.FileName,
|
|
|
|
|
Name = process.ProcessName
|
2011-10-07 08:57:43 +02:00
|
|
|
|
};
|
2011-10-07 05:37:41 +02:00
|
|
|
|
}
|
2013-05-04 23:09:25 +02:00
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
public void KillAll(string processName)
|
2013-05-04 23:09:25 +02:00
|
|
|
|
{
|
2013-05-07 07:38:40 +02:00
|
|
|
|
var processToKill = GetProcessByName(processName);
|
|
|
|
|
|
|
|
|
|
foreach (var processInfo in processToKill)
|
|
|
|
|
{
|
|
|
|
|
Kill(processInfo.Id);
|
|
|
|
|
}
|
2013-05-04 23:09:25 +02:00
|
|
|
|
}
|
2011-10-07 05:37:41 +02:00
|
|
|
|
}
|
2011-10-07 08:57:43 +02:00
|
|
|
|
}
|