From 164879428379ef1de0008a2afde4c6de6ecc83e1 Mon Sep 17 00:00:00 2001 From: Fergal Moran Date: Tue, 6 Mar 2018 18:54:26 +0000 Subject: [PATCH] Update async process handling --- server/Dockerfile | 3 ++- server/Services/Downloader/AudioDownloader.cs | 17 ++++++++++--- server/Utils/Extensions/DateTime.cs | 12 ++++----- server/Utils/Extensions/ProcessExtensions.cs | 25 +++++++++++++++++++ 4 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 server/Utils/Extensions/ProcessExtensions.cs diff --git a/server/Dockerfile b/server/Dockerfile index 1846551..ec5e6cc 100755 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -19,7 +19,8 @@ RUN apk add --no-cache --update \ curl && \ ln -s /usr/lib/libuv.so.1 /usr/lib/libuv.so && \ curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl && \ - chmod a+rx /usr/local/bin/youtube-dl + chmod a+rx /usr/local/bin/youtube-dl && \ + youtube-dl -U COPY --from=build-env /out . ENTRYPOINT ["dotnet", "PodNoms.Api.dll"] diff --git a/server/Services/Downloader/AudioDownloader.cs b/server/Services/Downloader/AudioDownloader.cs index 8b4f108..3b968a9 100644 --- a/server/Services/Downloader/AudioDownloader.cs +++ b/server/Services/Downloader/AudioDownloader.cs @@ -1,11 +1,12 @@ using System; -using Newtonsoft.Json; -using PodNoms.Api.Models.ViewModels; using System.Diagnostics; using System.Dynamic; using System.IO; using System.Text; using System.Threading.Tasks; +using Newtonsoft.Json; +using PodNoms.Api.Models.ViewModels; +using PodNoms.Api.Utils.Extensions; namespace PodNoms.Api.Services.Downloader { public class AudioDownloader { @@ -55,8 +56,18 @@ namespace PodNoms.Api.Services.Downloader { CreateNoWindow = true } }; + proc.EnableRaisingEvents = true; + proc.Exited += ((e, s) => { + // Console.WriteLine ($"{e.ToString()} - {s.ToString()}"); + }); + proc.OutputDataReceived += ((e, s) => { + Console.WriteLine ($"{e.ToString()} - {s.ToString()}"); + }); + proc.ErrorDataReceived += ((e, s) => { + Console.WriteLine ($"{e.ToString()} - {s.ToString()}"); + }); proc.Start (); - await (Task.Run (() => proc.WaitForExit ())); + await proc.WaitForExitAsync (); return (proc.ExitCode == 0); } diff --git a/server/Utils/Extensions/DateTime.cs b/server/Utils/Extensions/DateTime.cs index 5ec84f1..7c6e23e 100755 --- a/server/Utils/Extensions/DateTime.cs +++ b/server/Utils/Extensions/DateTime.cs @@ -3,13 +3,11 @@ using System; namespace PodNoms.Api.Utils.Extensions { - public static class DateTimeExtensions - { - public static string ToRFC822String(this DateTime datetime) - { - return datetime.ToString("ddd',' d MMM yyyy HH':'mm':'ss") - + " " - + datetime.ToString("zzzz").Replace(":", ""); + public static class DateTimeExtensions { + public static string ToRFC822String (this DateTime datetime) { + return datetime.ToString ("ddd',' d MMM yyyy HH':'mm':'ss") + + " " + + datetime.ToString ("zzzz").Replace (":", ""); } } } \ No newline at end of file diff --git a/server/Utils/Extensions/ProcessExtensions.cs b/server/Utils/Extensions/ProcessExtensions.cs new file mode 100644 index 0000000..bcfd89e --- /dev/null +++ b/server/Utils/Extensions/ProcessExtensions.cs @@ -0,0 +1,25 @@ +using System.Diagnostics; +using System.Threading; +using System.Threading.Tasks; + +namespace PodNoms.Api.Utils.Extensions { + public static class ProcessExtensions { + /// + /// Waits asynchronously for the process to exit. + /// + /// The process to wait for cancellation. + /// A cancellation token. If invoked, the task will return + /// immediately as canceled. + /// A Task representing waiting for the process to end. + public static Task WaitForExitAsync (this Process process, + CancellationToken cancellationToken = default (CancellationToken)) { + var tcs = new TaskCompletionSource (); + process.EnableRaisingEvents = true; + process.Exited += (sender, args) => tcs.TrySetResult (null); + if (cancellationToken != default (CancellationToken)) + cancellationToken.Register (tcs.SetCanceled); + + return tcs.Task; + } + } +} \ No newline at end of file