Long running process timing out before completion?

Hello,

I am attempting to use hangfire on one of my sites to handle some video processing, but I cannot seem to get it to fully complete my task. Here is an example of my code:

    // steps leading to this point ensure that the video file has been uploaded
    BackgroundJob.Enqueue(() => FileHelper.ProcessVideo(ffmpegPath, videoID, originalVideoServerPath, videoServerPath, thumbServerPath));

Here is an example of my synchronous ProcessVideo method (using ffmpeg.exe):

    /// <summary>
    /// Converts the uploaded video into .mp4 and generates a thumbnail preview.
    /// </summary>
    /// <param name="ffmpegPath">The server path of ffmpeg.exe</param>
    /// <param name="videoID">ID of the video record in database</param>
    /// <param name="originalVideoPath">Path of the uploaded video on the server</param>
    /// <param name="newVideoPath">Path of the newly converted video after it has been processed</param>
    /// <param name="thumbnailPath">Path of the thumbnail preview after it has been processed</param>
    public static void ProcessVideo(string ffmpegPath, int videoID, string originalVideoPath, string newVideoPath, string thumbnailPath)
    {
        var process = new System.Diagnostics.Process();
        var startInfo = new System.Diagnostics.ProcessStartInfo(ffmpegPath);

        var videos = new Videos();
        var video = videos.Get(videoID);
        if (!video.IsProcessed)
        {
            // convert the video to a lower quality .mp4
            startInfo.Arguments = string.Format("-i \"{0}\" -c:v libx264 -crf 30 -preset medium -c:a copy \"{1}\"",
                originalVideoPath, newVideoPath);
            process.StartInfo = startInfo;
            process.Start();
            process.WaitForExit();

            // generate a thumbnail preview of the video
            startInfo.Arguments = string.Format("-i \"{0}\" -f image2 \"{1}\"", originalVideoPath, thumbnailPath);
            process.StartInfo = startInfo;
            process.Start();
            process.WaitForExit();

            // update the video to show that processing has completed
            video.IsProcessed = true;
            videos.Update(video);

            // delete the original video
            Delete(originalVideoPath);
        }
    }

In this example, the compressed mp4 video will be converted, but the thumbnail is never generated. Also, the originally uploaded video is never deleted.

Do I need to do something else to ensure that the entire ProcessVideo method is completed before the process times out? I assume that is what happens during the conversion of the video, because if I re-arrange the thumbnail generation to come before the video compression, both are completed. However, the original file is still not deleted, and my database call to update video.IsProcessed is not reached either.

I would appreciate any suggestions on this. Thank you,

Chris

p.s. I should also mention that the example I have been using only takes a minute or so of actual processing time on the server.

As far as I know there’s no inherent timeout in HangFire, meaning the framework should theoretically wait until all processing is completed. My guess is that for some reason WaitForExit on the conversion process never completes. Can you verify that your method works as expected when executed directly (without HangFire).

It definitely does complete (when removing only the hangfire applicable code), however, I think I might know what is happening. I think (when spinning it off as an async task, rather than a synchronous one), the application pool might be noticing that a lot of resources are being consumed, but not recognizing that any processes are consuming them (the task is not registered)… which is causing it to recycle before the entire process is finished. I am not certain of this, but it is a hunch. Anyway, I found a solution that works for now… but I would definitely rather use Hangfire if I can find an applicable workaround.