2021-11-17 22:30:51 -05:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text.RegularExpressions;
|
2022-01-15 20:32:23 -05:00
|
|
|
using Newtonsoft.Json;
|
2021-11-17 22:30:51 -05:00
|
|
|
|
2023-05-22 00:58:36 -04:00
|
|
|
namespace vassago
|
2021-11-17 22:30:51 -05:00
|
|
|
{
|
|
|
|
public class ExternalProcess
|
|
|
|
{
|
|
|
|
public static bool GoPlz(string commandPath, string commandArguments)
|
|
|
|
{
|
|
|
|
var process = readableProcess(commandPath, commandArguments);
|
|
|
|
var outputData = new StringBuilder();
|
|
|
|
process.OutputDataReceived += new DataReceivedEventHandler((s, e) =>
|
|
|
|
{
|
|
|
|
outputData.Append(e.Data);
|
|
|
|
});
|
|
|
|
var errorData = new StringBuilder();
|
|
|
|
process.ErrorDataReceived += new DataReceivedEventHandler((s, e) =>
|
|
|
|
{
|
|
|
|
errorData.Append(e.Data);
|
|
|
|
});
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
process.Start();
|
|
|
|
process.BeginErrorReadLine();
|
|
|
|
process.BeginOutputReadLine();
|
|
|
|
process.WaitForExit();
|
|
|
|
}
|
|
|
|
catch(Exception e)
|
|
|
|
{
|
|
|
|
var dumpDir = $"fail{DateTime.Now.ToFileTimeUtc()}";
|
|
|
|
var outputFilename = $"{dumpDir}/output0.log";
|
|
|
|
var errorFilename = $"{dumpDir}/error0.err";
|
|
|
|
if(!Directory.Exists(dumpDir))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(dumpDir);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var i = 0;
|
|
|
|
foreach(var file in Directory.GetFiles(dumpDir))
|
|
|
|
{
|
|
|
|
var thisNummatch = Regex.Matches(Path.GetFileNameWithoutExtension(file), "[^\\d](\\d+)\\.err$").LastOrDefault().Value;
|
|
|
|
if(!string.IsNullOrWhiteSpace(thisNummatch))
|
|
|
|
{
|
|
|
|
var thisNumval = 0;
|
|
|
|
if(int.TryParse(thisNummatch, out thisNumval) && thisNumval > i)
|
|
|
|
{
|
|
|
|
i = thisNumval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
outputFilename = $"{dumpDir}/output{i}.log";
|
|
|
|
errorFilename = $"{dumpDir}/error{i}.err";
|
|
|
|
}
|
|
|
|
File.WriteAllText(outputFilename, outputData.ToString());
|
2022-01-15 20:32:23 -05:00
|
|
|
File.WriteAllText(errorFilename, JsonConvert.SerializeObject(e, Formatting.Indented));
|
2021-11-17 22:30:51 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
private static Process readableProcess(string commandPath, string commandArguments)
|
|
|
|
{
|
|
|
|
var pi = new ProcessStartInfo(commandPath, commandArguments);
|
|
|
|
pi.UseShellExecute = false;
|
|
|
|
pi.CreateNoWindow = true;
|
|
|
|
pi.RedirectStandardError = true;
|
|
|
|
pi.RedirectStandardOutput = true;
|
|
|
|
var process = new Process();
|
|
|
|
process.StartInfo = pi;
|
|
|
|
return process;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|