Remove unnecessary CommonProcess abstraction

This commit is contained in:
Mark Monteiro
2020-03-27 00:10:16 +01:00
parent 10050a55a5
commit ee2f911a2b
10 changed files with 120 additions and 241 deletions

View File

@@ -1727,15 +1727,15 @@ namespace Emby.Server.Implementations
throw new NotSupportedException();
}
var process = ProcessFactory.Create(new ProcessOptions
var process = ProcessFactory.Create(new ProcessStartInfo
{
FileName = url,
EnableRaisingEvents = true,
UseShellExecute = true,
ErrorDialog = false
});
process.Exited += ProcessExited;
process.EnableRaisingEvents = true;
process.Exited += (sender, args) => ((Process)sender).Dispose(); ;
try
{
@@ -1748,11 +1748,6 @@ namespace Emby.Server.Implementations
}
}
private static void ProcessExited(object sender, EventArgs e)
{
((IProcess)sender).Dispose();
}
public virtual void EnableLoopback(string appName)
{
}

View File

@@ -1,152 +0,0 @@
#pragma warning disable CS1591
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Diagnostics;
namespace Emby.Server.Implementations.Diagnostics
{
public class CommonProcess : IProcess
{
private readonly Process _process;
private bool _disposed = false;
private bool _hasExited;
public CommonProcess(ProcessOptions options)
{
StartInfo = options;
var startInfo = new ProcessStartInfo
{
Arguments = options.Arguments,
FileName = options.FileName,
WorkingDirectory = options.WorkingDirectory,
UseShellExecute = options.UseShellExecute,
CreateNoWindow = options.CreateNoWindow,
RedirectStandardError = options.RedirectStandardError,
RedirectStandardInput = options.RedirectStandardInput,
RedirectStandardOutput = options.RedirectStandardOutput,
ErrorDialog = options.ErrorDialog
};
if (options.IsHidden)
{
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
}
_process = new Process
{
StartInfo = startInfo
};
if (options.EnableRaisingEvents)
{
_process.EnableRaisingEvents = true;
_process.Exited += OnProcessExited;
}
}
public event EventHandler Exited;
public ProcessOptions StartInfo { get; }
public StreamWriter StandardInput => _process.StandardInput;
public StreamReader StandardError => _process.StandardError;
public StreamReader StandardOutput => _process.StandardOutput;
public int ExitCode => _process.ExitCode;
private bool HasExited
{
get
{
if (_hasExited)
{
return true;
}
try
{
_hasExited = _process.HasExited;
}
catch (InvalidOperationException)
{
_hasExited = true;
}
return _hasExited;
}
}
public void Start()
{
_process.Start();
}
public void Kill()
{
_process.Kill();
}
public bool WaitForExit(int timeMs)
{
return _process.WaitForExit(timeMs);
}
public Task<bool> WaitForExitAsync(int timeMs)
{
// Note: For this function to work correctly, the option EnableRisingEvents needs to be set to true.
if (HasExited)
{
return Task.FromResult(true);
}
timeMs = Math.Max(0, timeMs);
var tcs = new TaskCompletionSource<bool>();
var cancellationToken = new CancellationTokenSource(timeMs).Token;
_process.Exited += (sender, args) => tcs.TrySetResult(true);
cancellationToken.Register(() => tcs.TrySetResult(HasExited));
return tcs.Task;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
if (disposing)
{
_process?.Dispose();
}
_disposed = true;
}
private void OnProcessExited(object sender, EventArgs e)
{
_hasExited = true;
Exited?.Invoke(this, e);
}
}
}

View File

@@ -1,14 +1,15 @@
#pragma warning disable CS1591
using System.Diagnostics;
using MediaBrowser.Model.Diagnostics;
namespace Emby.Server.Implementations.Diagnostics
{
public class ProcessFactory : IProcessFactory
{
public IProcess Create(ProcessOptions options)
public Process Create(ProcessStartInfo startInfo)
{
return new CommonProcess(options);
return new Process { StartInfo = startInfo };
}
}
}

View File

@@ -3,6 +3,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
@@ -1683,19 +1684,19 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
try
{
var process = _processFactory.Create(new ProcessOptions
var process = _processFactory.Create(new ProcessStartInfo
{
Arguments = GetPostProcessArguments(path, options.RecordingPostProcessorArguments),
CreateNoWindow = true,
EnableRaisingEvents = true,
ErrorDialog = false,
FileName = options.RecordingPostProcessor,
IsHidden = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false
});
_logger.LogInformation("Running recording post processor {0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
process.EnableRaisingEvents = true;
process.Exited += Process_Exited;
process.Start();
}
@@ -1712,11 +1713,14 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
private void Process_Exited(object sender, EventArgs e)
{
using (var process = (IProcess)sender)
try
{
_logger.LogInformation("Recording post-processing script completed with exit code {ExitCode}", process.ExitCode);
process.Dispose();
var exitCode = ((Process)sender).ExitCode;
_logger.LogInformation("Recording post-processing script completed with exit code {ExitCode}", exitCode);
}
finally
{
((Process)sender).Dispose();
}
}

View File

@@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
@@ -30,7 +31,7 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
private bool _hasExited;
private Stream _logFileStream;
private string _targetPath;
private IProcess _process;
private Process _process;
private readonly IProcessFactory _processFactory;
private readonly IJsonSerializer _json;
private readonly TaskCompletionSource<bool> _taskCompletionSource = new TaskCompletionSource<bool>();
@@ -80,7 +81,7 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
_targetPath = targetFile;
Directory.CreateDirectory(Path.GetDirectoryName(targetFile));
var process = _processFactory.Create(new ProcessOptions
_process = _processFactory.Create(new ProcessStartInfo
{
CreateNoWindow = true,
UseShellExecute = false,
@@ -91,14 +92,11 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
FileName = _mediaEncoder.EncoderPath,
Arguments = GetCommandLineArgs(mediaSource, inputFile, targetFile, duration),
IsHidden = true,
ErrorDialog = false,
EnableRaisingEvents = true
WindowStyle = ProcessWindowStyle.Hidden,
ErrorDialog = false
});
_process = process;
var commandLineLogMessage = process.StartInfo.FileName + " " + process.StartInfo.Arguments;
var commandLineLogMessage = _process.StartInfo.FileName + " " + _process.StartInfo.Arguments;
_logger.LogInformation(commandLineLogMessage);
var logFilePath = Path.Combine(_appPaths.LogDirectoryPath, "record-transcode-" + Guid.NewGuid() + ".txt");
@@ -110,16 +108,17 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
var commandLineLogMessageBytes = Encoding.UTF8.GetBytes(_json.SerializeToString(mediaSource) + Environment.NewLine + Environment.NewLine + commandLineLogMessage + Environment.NewLine + Environment.NewLine);
_logFileStream.Write(commandLineLogMessageBytes, 0, commandLineLogMessageBytes.Length);
process.Exited += (sender, args) => OnFfMpegProcessExited(process, inputFile);
_process.EnableRaisingEvents = true;
_process.Exited += (sender, args) => OnFfMpegProcessExited(_process, inputFile);
process.Start();
_process.Start();
cancellationToken.Register(Stop);
onStarted();
// Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
StartStreamingLog(process.StandardError.BaseStream, _logFileStream);
StartStreamingLog(_process.StandardError.BaseStream, _logFileStream);
_logger.LogInformation("ffmpeg recording process started for {0}", _targetPath);
@@ -293,7 +292,7 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
/// <summary>
/// Processes the exited.
/// </summary>
private void OnFfMpegProcessExited(IProcess process, string inputFile)
private void OnFfMpegProcessExited(Process process, string inputFile)
{
_hasExited = true;