簡體   English   中英

Azure 批處理與 C# 應用程序使用 System.Diagnostics.Process

[英]Azure Batch with C# Application using System.Diagnostics.Process

我正在使用 Azure Batch 和 C# 應用程序。 整個 C# 應用程序負責做很多事情。 不幸的是,在我的本地機器上運行的 1 個操作在 Azure 批處理應用程序中不起作用。

在本地工作但不是作為 Azure C# 應用程序的操作是以編程方式啟動 System.Diagnostics.Process 執行 FFmpeg 參數的屏幕截圖參數。 該過程似乎正在運行,但是未創建 jpg 文件。 在本地,代碼將創建一個 jpg,但作為 Azure 批處理應用程序,不會創建該文件。

我已在 AZ_BATCH_TASK_WORKING_DIR 文件夾中設置要創建的圖像文件。 我已將代碼減少為僅執行不能作為 Azure 應用程序工作的 1 操作,這里是:

using System;
using System.Diagnostics;
using System.IO;

namespace BatchAppWithFfmpegProcess
{
    internal class Program
    {
        static void Main(string[] args)
        {

#if DEBUG
            string workingDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
#else
           /*
                Within each Tasks directory, the Batch service creates a working directory (wd) whose unique path is 
                specified by the AZ_BATCH_TASK_WORKING_DIR environment variable. This directory provides read/write access 
                to the task. The task can create, read, update, and delete files under this directory. 
                This directory is retained based on the RetentionTime constraint that is specified for the task.
                The stdout.txt and stderr.txt files are written to the Tasks folder during the execution of the task.
             */
            string workingDirectory = Environment.GetEnvironmentVariable("AZ_BATCH_TASK_WORKING_DIR");
#endif
            var applicationPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
            string ffmpegPath = Path.Combine(applicationPath, @"lib\");

            string videoFilePath = Path.Combine(applicationPath, "MichaelJacksonSmoothCriminal_Trimmed.mp4");
            string thumbnailFilePath = Path.Combine(workingDirectory, "MichaelJacksonSmoothCriminal_Trimmed.jpg");

            if (File.Exists(thumbnailFilePath))
            {
                File.Delete(thumbnailFilePath);
            }

            string arguments =  $"-i \"{videoFilePath}\" -ss 00:00:01.000 -frames:v 1 \"{thumbnailFilePath}\"";

            var startInfo = new ProcessStartInfo
            {
                FileName = ffmpegPath + $"\\ffmpeg.exe",
                Arguments = arguments,
                RedirectStandardError = false,
                RedirectStandardOutput = true,
                CreateNoWindow = true,
                UseShellExecute = false,
                WorkingDirectory = ffmpegPath
            };

            using (var process = new Process { StartInfo = startInfo })
            {

                process.Start();
                process.WaitForExit();
            }
            if (File.Exists(thumbnailFilePath))
            {
                Console.WriteLine("Hurray, it worked!!!");
            }
            else
            {
                Console.WriteLine("File was not created.");
            }
        }
    }
}

也許不可能使用 System.Diagnostics.Process 來創建文件? 我試圖通過以下方式盡可能輕松地重現這一點:

克隆代碼: https://github.com/Dapp3rDanH/AzBatchFfmpegProcess.git

使用 Visual Studio 2022,使用“部署模式”將 BatchAppWithFfmpegProcess 代碼“發布”到文件夾 = net5.0 目標運行時的自包含目標框架 = win-x64。

創建發布文件夾的“BatchAppWithFfmpegProcess.zip”zip 文件。 確保 BatchAppWithFfmpegProcess.exe 位於 zip 的根目錄中。

創建一個批處理帳戶。

使用 BatchAppWithFfmpegProcess.zip 文件添加 Batch 應用程序,appId 為 BatchAppWithFfmpegProcess,版本為 1。

使用 microsoftwindowsserver windowsserver 2022-datacenter-core(最新)添加一個名為“Pool1”的池和 1 個專用節點。 將 BatchAppWithFfmpegProcess 版本 1 添加到 Pool1。

使用以下命令創建具有任務的新作業:cmd /c %AZ_BATCH_APP_PACKAGE_BatchAppWithFfmpegProcess#1%\BatchAppWithFfmpegProcess.exe

如果您檢查任務的 stdout.txt 文件,您將看到“文件未創建”。 有什么辦法可以讓它工作嗎?

謝謝!

該問題與我的池基於“microsoftwindowsserver windowsserver 2022-datacenter-core (latest)”有關。

切換到 microsoftwindowsserver windowsserver 2016-datacenter (latest) 解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM