Jackett/src/Jackett.Common/Services/ProcessService.cs

103 lines
3.1 KiB
C#
Raw Normal View History

2015-07-19 13:22:50 +00:00
using NLog;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Feature/netcore preparation (#2035) * Move to use package reference for restoring nuget packages. * Return a task result for this async method. * Update to a supported version of the .NET Framework. This also has the side effect of allowing us to automatically generate our binding redirects on build. * Set the solution to target VS2017 * Update test solution csproj file to support being built through MSBuild 15 * Move to use package reference for restoring nuget packages. * Return a task result for this async method. * Update to a supported version of the .NET Framework. This also has the side effect of allowing us to automatically generate our binding redirects on build. * Set the solution to target VS2017 * Update test solution csproj file to support being built through MSBuild 15 * DateTimeRoutines does not have Nuget packages that support .NET Standard (and therefore .NET Core). We will have to include them for now until we can get rid of this dependency. * Move the interfaces into their own files. This will be useful when we share them between the .NET Core and .NET Framework WebAPI * Stage services that need to point to the new interface namespace. * Update CurlSharp to fix memory leak issue and support better runtime compatibility with OSX and Linux * Start spliting some interfaces into their own files - this will help by allowing us to split them out in the future into a seperate project so the actual implementations can stay within their respective architectures when required
2017-10-29 10:19:09 +00:00
using Jackett.Services.Interfaces;
2015-07-19 13:22:50 +00:00
namespace Jackett.Services
{
public class ProcessService : IProcessService
{
private Logger logger;
public ProcessService(Logger l)
{
logger = l;
}
private void Run(string exe, string args, bool asAdmin, DataReceivedEventHandler d, DataReceivedEventHandler r)
2015-07-19 13:22:50 +00:00
{
var startInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
UseShellExecute = false,
FileName = exe,
Arguments = args,
RedirectStandardError = true,
RedirectStandardOutput = true,
RedirectStandardInput = true
};
if (asAdmin)
{
startInfo.Verb = "runas";
startInfo.UseShellExecute = true;
startInfo.RedirectStandardError = false;
startInfo.RedirectStandardOutput = false;
startInfo.RedirectStandardInput = false;
}
2015-07-24 21:38:31 +00:00
logger.Debug("Running " + startInfo.FileName + " " + startInfo.Arguments);
2015-07-19 13:22:50 +00:00
var proc = Process.Start(startInfo);
if (!asAdmin)
{
proc.OutputDataReceived += d;
proc.ErrorDataReceived += r;
proc.BeginErrorReadLine();
proc.BeginOutputReadLine();
}
2015-07-19 13:22:50 +00:00
proc.WaitForExit();
if (!asAdmin)
{
proc.OutputDataReceived -= d;
proc.ErrorDataReceived -= r;
}
2015-07-19 13:22:50 +00:00
}
public string StartProcessAndGetOutput(string exe, string args, bool keepnewlines = false, bool asAdmin = false)
2015-07-19 13:22:50 +00:00
{
var sb = new StringBuilder();
DataReceivedEventHandler rxData = (a, e) => {
if (keepnewlines || !string.IsNullOrWhiteSpace(e.Data))
{
sb.AppendLine(e.Data);
}
};
DataReceivedEventHandler rxError = (s, e) => {
if (keepnewlines || !string.IsNullOrWhiteSpace(e.Data))
{
sb.AppendLine(e.Data);
}
};
Run(exe, args, asAdmin, rxData, rxError);
return sb.ToString();
2015-07-19 13:22:50 +00:00
}
public void StartProcessAndLog(string exe, string args, bool asAdmin = false)
2015-07-19 13:22:50 +00:00
{
var sb = new StringBuilder();
DataReceivedEventHandler rxData = (a, e) =>
2015-07-19 13:22:50 +00:00
{
if (!string.IsNullOrWhiteSpace(e.Data))
{
logger.Debug(e.Data);
}
};
DataReceivedEventHandler rxError = (s, e) =>
{
if (!string.IsNullOrWhiteSpace(e.Data))
{
logger.Error(e.Data);
}
};
Run(exe, args, asAdmin, rxData, rxError);
2015-07-19 13:22:50 +00:00
}
}
}