Use Process.WaitForExitAsync added in .NET 5
This commit is contained in:
parent
d5695efad9
commit
4757ce105b
|
@ -15,65 +15,13 @@ namespace MediaBrowser.Common.Extensions
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="process">The process to wait for.</param>
|
/// <param name="process">The process to wait for.</param>
|
||||||
/// <param name="timeout">The duration to wait before cancelling waiting for the task.</param>
|
/// <param name="timeout">The duration to wait before cancelling waiting for the task.</param>
|
||||||
/// <returns>True if the task exited normally, false if the timeout elapsed before the process exited.</returns>
|
/// <returns>A task that will complete when the process has exited, cancellation has been requested, or an error occurs.</returns>
|
||||||
/// <exception cref="InvalidOperationException">If <see cref="Process.EnableRaisingEvents"/> is not set to true for the process.</exception>
|
/// <exception cref="OperationCanceledException">The timeout ended.</exception>
|
||||||
public static async Task<bool> WaitForExitAsync(this Process process, TimeSpan timeout)
|
public static async Task WaitForExitAsync(this Process process, TimeSpan timeout)
|
||||||
{
|
{
|
||||||
using (var cancelTokenSource = new CancellationTokenSource(timeout))
|
using (var cancelTokenSource = new CancellationTokenSource(timeout))
|
||||||
{
|
{
|
||||||
return await WaitForExitAsync(process, cancelTokenSource.Token).ConfigureAwait(false);
|
await process.WaitForExitAsync(cancelTokenSource.Token).ConfigureAwait(false);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously wait for the process to exit.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="process">The process to wait for.</param>
|
|
||||||
/// <param name="cancelToken">A <see cref="CancellationToken"/> to observe while waiting for the process to exit.</param>
|
|
||||||
/// <returns>True if the task exited normally, false if cancelled before the process exited.</returns>
|
|
||||||
public static async Task<bool> WaitForExitAsync(this Process process, CancellationToken cancelToken)
|
|
||||||
{
|
|
||||||
if (!process.EnableRaisingEvents)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("EnableRisingEvents must be enabled to async wait for a task to exit.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add an event handler for the process exit event
|
|
||||||
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
||||||
process.Exited += (_, _) => tcs.TrySetResult(true);
|
|
||||||
|
|
||||||
// Return immediately if the process has already exited
|
|
||||||
if (process.HasExitedSafe())
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Register with the cancellation token then await
|
|
||||||
using (var cancelRegistration = cancelToken.Register(() => tcs.TrySetResult(process.HasExitedSafe())))
|
|
||||||
{
|
|
||||||
return await tcs.Task.ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets a value indicating whether the associated process has been terminated using
|
|
||||||
/// <see cref="Process.HasExited"/>. This is safe to call even if there is no operating system process
|
|
||||||
/// associated with the <see cref="Process"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="process">The process to check the exit status for.</param>
|
|
||||||
/// <returns>
|
|
||||||
/// True if the operating system process referenced by the <see cref="Process"/> component has
|
|
||||||
/// terminated, or if there is no associated operating system process; otherwise, false.
|
|
||||||
/// </returns>
|
|
||||||
private static bool HasExitedSafe(this Process process)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return process.HasExited;
|
|
||||||
}
|
|
||||||
catch (InvalidOperationException)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -174,24 +174,18 @@ namespace MediaBrowser.MediaEncoding.Attachments
|
||||||
|
|
||||||
process.Start();
|
process.Start();
|
||||||
|
|
||||||
var ranToCompletion = await ProcessExtensions.WaitForExitAsync(process, cancellationToken).ConfigureAwait(false);
|
|
||||||
|
|
||||||
if (!ranToCompletion)
|
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Killing ffmpeg attachment extraction process");
|
await process.WaitForExitAsync(cancellationToken).ConfigureAwait(false);
|
||||||
process.Kill();
|
exitCode = process.ExitCode;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (OperationCanceledException)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error killing attachment extraction process");
|
process.Kill(true);
|
||||||
|
exitCode = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exitCode = ranToCompletion ? process.ExitCode : -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
var failed = false;
|
var failed = false;
|
||||||
|
|
||||||
if (exitCode != 0)
|
if (exitCode != 0)
|
||||||
|
@ -322,24 +316,18 @@ namespace MediaBrowser.MediaEncoding.Attachments
|
||||||
|
|
||||||
process.Start();
|
process.Start();
|
||||||
|
|
||||||
var ranToCompletion = await ProcessExtensions.WaitForExitAsync(process, cancellationToken).ConfigureAwait(false);
|
|
||||||
|
|
||||||
if (!ranToCompletion)
|
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Killing ffmpeg attachment extraction process");
|
await process.WaitForExitAsync(cancellationToken).ConfigureAwait(false);
|
||||||
process.Kill();
|
exitCode = process.ExitCode;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (OperationCanceledException)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error killing attachment extraction process");
|
process.Kill(true);
|
||||||
|
exitCode = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exitCode = ranToCompletion ? process.ExitCode : -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
var failed = false;
|
var failed = false;
|
||||||
|
|
||||||
if (exitCode != 0)
|
if (exitCode != 0)
|
||||||
|
|
|
@ -760,11 +760,15 @@ namespace MediaBrowser.MediaEncoding.Encoder
|
||||||
timeoutMs = enableHdrExtraction ? DefaultHdrImageExtractionTimeout : DefaultSdrImageExtractionTimeout;
|
timeoutMs = enableHdrExtraction ? DefaultHdrImageExtractionTimeout : DefaultSdrImageExtractionTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
ranToCompletion = await process.WaitForExitAsync(TimeSpan.FromMilliseconds(timeoutMs)).ConfigureAwait(false);
|
try
|
||||||
|
|
||||||
if (!ranToCompletion)
|
|
||||||
{
|
{
|
||||||
StopProcess(processWrapper, 1000);
|
await process.WaitForExitAsync(TimeSpan.FromMilliseconds(timeoutMs)).ConfigureAwait(false);
|
||||||
|
ranToCompletion = true;
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
process.Kill(true);
|
||||||
|
ranToCompletion = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
|
@ -999,7 +1003,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ProcessWrapper : IDisposable
|
private sealed class ProcessWrapper : IDisposable
|
||||||
{
|
{
|
||||||
private readonly MediaEncoder _mediaEncoder;
|
private readonly MediaEncoder _mediaEncoder;
|
||||||
|
|
||||||
|
@ -1042,14 +1046,8 @@ namespace MediaBrowser.MediaEncoding.Encoder
|
||||||
_mediaEncoder._runningProcesses.Remove(this);
|
_mediaEncoder._runningProcesses.Remove(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
process.Dispose();
|
process.Dispose();
|
||||||
}
|
}
|
||||||
catch
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
|
|
@ -420,25 +420,18 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
var ranToCompletion = await process.WaitForExitAsync(TimeSpan.FromMinutes(30)).ConfigureAwait(false);
|
|
||||||
|
|
||||||
if (!ranToCompletion)
|
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Killing ffmpeg subtitle conversion process");
|
await process.WaitForExitAsync(TimeSpan.FromMinutes(30)).ConfigureAwait(false);
|
||||||
|
exitCode = process.ExitCode;
|
||||||
process.Kill();
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (OperationCanceledException)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error killing subtitle conversion process");
|
process.Kill(true);
|
||||||
|
exitCode = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exitCode = ranToCompletion ? process.ExitCode : -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
var failed = false;
|
var failed = false;
|
||||||
|
|
||||||
if (exitCode == -1)
|
if (exitCode == -1)
|
||||||
|
@ -574,25 +567,18 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
var ranToCompletion = await process.WaitForExitAsync(TimeSpan.FromMinutes(30)).ConfigureAwait(false);
|
|
||||||
|
|
||||||
if (!ranToCompletion)
|
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Killing ffmpeg subtitle extraction process");
|
await process.WaitForExitAsync(TimeSpan.FromMinutes(30)).ConfigureAwait(false);
|
||||||
|
exitCode = process.ExitCode;
|
||||||
process.Kill();
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (OperationCanceledException)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error killing subtitle extraction process");
|
process.Kill(true);
|
||||||
|
exitCode = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exitCode = ranToCompletion ? process.ExitCode : -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
var failed = false;
|
var failed = false;
|
||||||
|
|
||||||
if (exitCode == -1)
|
if (exitCode == -1)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user