| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for license information. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using Microsoft.Azure.Batch.Common; |
| | 7 | |
|
| | 8 | | namespace Microsoft.Azure.Batch |
| | 9 | | { |
| | 10 | | // TODO: the original documentation stated that this is for controlling parallelism but it's not clear what |
| | 11 | | // you would do here in order to do that - that seems to be more governed by the parallelOptions argument to |
| | 12 | | // AddTaskAsync. |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// A <see cref="BatchClientBehavior"/> which you can use to specify under what conditions an operation to |
| | 16 | | /// add multiple tasks to a job should retry, terminate or be considered successful. |
| | 17 | | /// </summary> |
| | 18 | | /// <remarks>You do not need to specify this behavior explicitly; if you do not, a default behavior |
| | 19 | | /// is used. This behavior uses the <see cref="DefaultAddTaskCollectionResultHandler(AddTaskResult, CancellationTok |
| | 20 | | /// criteria.</remarks> |
| | 21 | | public class AddTaskCollectionResultHandler : BatchClientBehavior |
| | 22 | | { |
| | 23 | | /// <summary> |
| | 24 | | /// Gets or sets the function that defines whether a particular Add Task operation is considered |
| | 25 | | /// successful or unsuccessful, and whether it may be retried. |
| | 26 | | /// </summary> |
| 651 | 27 | | public Func<AddTaskResult, CancellationToken, AddTaskResultStatus> ResultHandler { get; set; } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Initializes a new instance of the <see cref="AddTaskCollectionResultHandler"/> class with the specified resu |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="resultHandler">A function that defines whether a particular Add Task operation is considered |
| | 33 | | /// successful or unsuccessful, and whether it may be retried.</param> |
| 653 | 34 | | public AddTaskCollectionResultHandler(Func<AddTaskResult, CancellationToken, AddTaskResultStatus> resultHandler) |
| | 35 | | { |
| 653 | 36 | | if (resultHandler == null) |
| | 37 | | { |
| 0 | 38 | | throw new ArgumentNullException("resultHandler"); |
| | 39 | | } |
| | 40 | |
|
| 653 | 41 | | this.ResultHandler = resultHandler; |
| 653 | 42 | | } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// The default result handler for the <see cref="AddTaskCollectionResultHandler"/> behavior. This handler |
| | 46 | | /// treats success and 'TaskExists' errors as successful, retries server errors (HTTP 5xx), and throws |
| | 47 | | /// <see cref="AddTaskCollectionTerminatedException"/> on client error (HTTP 4xx). |
| | 48 | | /// </summary> |
| | 49 | | /// <param name="addTaskResult">The result of a single Add Task operation.</param> |
| | 50 | | /// <param name="cancellationToken">The cancellation token associated with the AddTaskCollection operation.</par |
| | 51 | | /// <returns>An <see cref="AddTaskResultStatus"/> which indicates whether the <paramref name="addTaskResult"/> |
| | 52 | | /// is classified as a success or as requiring a retry.</returns> |
| | 53 | | public static AddTaskResultStatus DefaultAddTaskCollectionResultHandler(AddTaskResult addTaskResult, Cancellatio |
| | 54 | | { |
| 2 | 55 | | if (addTaskResult == null) |
| | 56 | | { |
| 0 | 57 | | throw new ArgumentNullException("addTaskResult"); |
| | 58 | | } |
| | 59 | |
|
| 2 | 60 | | AddTaskResultStatus status = AddTaskResultStatus.Success; |
| 2 | 61 | | if (addTaskResult.Error != null) |
| | 62 | | { |
| | 63 | | //Check status code |
| 1 | 64 | | if (addTaskResult.Status == AddTaskStatus.ServerError) |
| | 65 | | { |
| 0 | 66 | | status = AddTaskResultStatus.Retry; |
| | 67 | | } |
| 1 | 68 | | else if (addTaskResult.Status == AddTaskStatus.ClientError && addTaskResult.Error.Code == BatchErrorCode |
| | 69 | | { |
| 0 | 70 | | status = AddTaskResultStatus.Success; //Count TaskExists as a success always |
| | 71 | | } |
| | 72 | | else |
| | 73 | | { |
| | 74 | | //Anything else is a failure -- abort the work flow |
| 1 | 75 | | throw new AddTaskCollectionTerminatedException(addTaskResult); |
| | 76 | | } |
| | 77 | | } |
| 1 | 78 | | return status; |
| | 79 | | } |
| | 80 | | } |
| | 81 | | } |