@@ -7,6 +7,7 @@ namespace BatchDotnetTutorialFfmpeg
77 using System . Collections . Generic ;
88 using System . Diagnostics ;
99 using System . IO ;
10+ using System . Linq ;
1011 using System . Threading . Tasks ;
1112 using Microsoft . Azure . Batch ;
1213 using Microsoft . Azure . Batch . Auth ;
@@ -423,8 +424,10 @@ private static async Task<List<CloudTask>> AddTasksAsync(BatchClient batchClient
423424 private static async Task < bool > MonitorTasks ( BatchClient batchClient , string jobId , TimeSpan timeout )
424425 {
425426 bool allTasksSuccessful = true ;
426- const string successMessage = "All tasks reached state Completed." ;
427- const string failureMessage = "One or more tasks failed to reach the Completed state within the timeout period." ;
427+ const string completeMessage = "All tasks reached state Completed." ;
428+ const string incompleteMessage = "One or more tasks failed to reach the Completed state within the timeout period." ;
429+ const string successMessage = "Success! All tasks completed successfully. Output files uploaded to output container." ;
430+ const string failureMessage = "One or more tasks failed." ;
428431
429432 // Obtain the collection of tasks currently managed by the job.
430433 // Use a detail level to specify that only the "id" property of each task should be populated.
@@ -446,47 +449,34 @@ private static async Task<bool> MonitorTasks(BatchClient batchClient, string job
446449 }
447450 catch ( TimeoutException )
448451 {
449- await batchClient . JobOperations . TerminateJobAsync ( jobId , failureMessage ) ;
450- Console . WriteLine ( failureMessage ) ;
452+ await batchClient . JobOperations . TerminateJobAsync ( jobId ) ;
453+ Console . WriteLine ( incompleteMessage ) ;
451454 return false ;
452455 }
453- await batchClient . JobOperations . TerminateJobAsync ( jobId , successMessage ) ;
456+ await batchClient . JobOperations . TerminateJobAsync ( jobId ) ;
457+ Console . WriteLine ( completeMessage ) ;
454458
455459 // All tasks have reached the "Completed" state, however, this does not guarantee all tasks completed successfully.
456- // Here we further check each task's ExecutionInformation property to ensure that it did not encounter a scheduling error
457- // or return a non-zero exit code.
458-
459- // Update the detail level to populate only the task id and executionInfo properties.
460- detail . SelectClause = "id, executionInfo" ;
460+ // Here we further check for any tasks with an execution result of "Failure".
461461
462- List < CloudTask > completedTasks = await batchClient . JobOperations . ListTasks ( jobId , detail ) . ToListAsync ( ) ;
462+ // Update the detail level to populate only the executionInfo property.
463+ detail . SelectClause = "executionInfo" ;
464+ // Filter for tasks with 'Failure' result.
465+ detail . FilterClause = "executionInfo/result eq 'Failure'" ;
463466
464- foreach ( CloudTask task in completedTasks )
467+ List < CloudTask > failedTasks = await batchClient . JobOperations . ListTasks ( jobId , detail ) . ToListAsync ( ) ;
468+
469+ if ( failedTasks . Any ( ) )
465470 {
466- if ( task . ExecutionInformation . Result == TaskExecutionResult . Failure )
467- {
468- // A task with failure information set indicates there was a problem with the task. It is important to note that
469- // the task's state can be "Completed," yet still have encountered a failure.
470-
471- allTasksSuccessful = false ;
472-
473- Console . WriteLine ( "WARNING: Task [{0}] encountered a failure: {1}" , task . Id , task . ExecutionInformation . FailureInformation . Message ) ;
474- if ( task . ExecutionInformation . ExitCode != 0 )
475- {
476- // A non-zero exit code may indicate that the application executed by the task encountered an error
477- // during execution. As not every application returns non-zero on failure by default (e.g. robocopy),
478- // your implementation of error checking may differ from this example.
479-
480- Console . WriteLine ( "WARNING: Task [{0}] returned a non-zero exit code - this may indicate task execution or completion failure." , task . Id ) ;
481- }
482- }
471+ allTasksSuccessful = false ;
472+ Console . WriteLine ( failureMessage ) ;
483473 }
484474
485475 if ( allTasksSuccessful )
486476 {
487- Console . WriteLine ( "Success! All tasks completed successfully within the specified timeout period. Output files uploaded to output container." ) ;
477+ Console . WriteLine ( successMessage ) ;
488478 }
489- return allTasksSuccessful ;
479+ return allTasksSuccessful ;
490480 }
491481 }
492482}
0 commit comments