private static async Task WriteFinalOutput(int number, string taskId, TaskOutputStorage taskStorage, string index) { Console.WriteLine("Write output to task storage from EXE"); using (ITrackedSaveOperation stdout = await taskStorage.SaveTrackedAsync( TaskOutputKind.TaskLog, RootDir("stdout.txt"), "stdout.txt", TimeSpan.FromSeconds(15))) { Console.WriteLine("Dump output to file from EXE - start"); string outputFile = "results.txt"; using (StreamWriter output = File.CreateText(WorkingDir(outputFile))) { output.WriteLine($"final task {taskId}"); output.WriteLine($"element: {number}"); } // Persist the task output to Azure Storage Task.WaitAll(taskStorage.SaveAsync(TaskOutputKind.TaskOutput, outputFile)); // We are tracking the disk file to save our standard output, but the node agent may take // up to 3 seconds to flush the stdout stream to disk. So give the file a moment to catch up. await Task.Delay(stdoutFlushDelay); Console.WriteLine("Dump output to file from EXE - finish"); } }
public static async Task <int> RunTaskAsync() { // Obtain service-defined environment variables string jobId = Environment.GetEnvironmentVariable("AZ_BATCH_JOB_ID"); string taskId = Environment.GetEnvironmentVariable("AZ_BATCH_TASK_ID"); // Obtain the custom environment variable we set in the client application string jobContainerUrl = Environment.GetEnvironmentVariable("JOB_CONTAINER_URL"); // The task will use the TaskOutputStorage to store both its output and log updates TaskOutputStorage taskStorage = new TaskOutputStorage(new Uri(jobContainerUrl), taskId); // The primary task logic is wrapped in a using statement that sends updates to the // stdout.txt blob in Storage every 15 seconds while the task code runs. using (ITrackedSaveOperation stdout = await taskStorage.SaveTrackedAsync( TaskOutputKind.TaskLog, RootDir("stdout.txt"), "stdout.txt", TimeSpan.FromSeconds(15))) { string outputFile = $"results_{taskId}.txt"; string summaryFile = $"summary_{taskId}.txt"; using (StreamWriter output = File.CreateText(WorkingDir(outputFile))) { using (StreamWriter summary = File.CreateText(WorkingDir(summaryFile))) { output.WriteLine($"# Task {taskId}"); const int runCount = 1000000; int[] results = new int[runCount]; double resultTotal = 0; for (int i = 0; i < runCount; ++i) { int runResult = PerformSingleRunMonteCarloSimulation(); output.WriteLine($"{i}, {runResult}"); results[i] = runResult; resultTotal += runResult; if (i % 5000 == 0) { Console.WriteLine($"{DateTime.UtcNow}: Processing... done {i}"); } } double mean = resultTotal / runCount; double stddev = Math.Sqrt((from r in results let d = r - mean select d * d).Average()); summary.WriteLine($"Task: {taskId}"); summary.WriteLine($"Run count: {runCount}"); summary.WriteLine($"Mean: {mean}"); summary.WriteLine($"Std dev: {stddev}"); } } // Persist the task output to Azure Storage Task.WaitAll( taskStorage.SaveAsync(TaskOutputKind.TaskOutput, outputFile), taskStorage.SaveAsync(TaskOutputKind.TaskPreview, summaryFile) ); // We are tracking the disk file to save our standard output, but the node agent may take // up to 3 seconds to flush the stdout stream to disk. So give the file a moment to catch up. await Task.Delay(stdoutFlushDelay); return(0); } }