| | | 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 | | namespace Microsoft.Azure.Batch |
| | | 5 | | { |
| | | 6 | | using System; |
| | | 7 | | using Microsoft.Azure.Batch.Protocol; |
| | | 8 | | using Protocol.Models; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Class which provides easy access to the <see cref="IBatchRequest.Timeout"/> property and the <see cref="Protocol |
| | | 12 | | /// </summary> |
| | | 13 | | public class BatchRequestTimeout : Protocol.RequestInterceptor |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Gets or sets the server timeout to be applied to each request issued to the Batch service. |
| | | 17 | | /// </summary> |
| | 36 | 18 | | public TimeSpan? ServerTimeout { get; set; } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Gets or sets the client timeout to be applied to each request issued to the Batch service. |
| | | 22 | | /// </summary> |
| | 31 | 23 | | public TimeSpan? ClientTimeout { get; set; } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Initializes a new instance of the <see cref="BatchRequestTimeout"/> class. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="serverTimeout">The server timeout to be applied to each request.</param> |
| | | 29 | | /// <param name="clientTimeout">The client timeout to be applied to each request.</param> |
| | 12 | 30 | | public BatchRequestTimeout(TimeSpan? serverTimeout = null, TimeSpan? clientTimeout = null) |
| | | 31 | | { |
| | 12 | 32 | | this.ServerTimeout = serverTimeout; |
| | 12 | 33 | | this.ClientTimeout = clientTimeout; |
| | | 34 | | |
| | 12 | 35 | | base.ModificationInterceptHandler = this.SetTimeoutInterceptor; |
| | 12 | 36 | | } |
| | | 37 | | |
| | | 38 | | private void SetTimeoutInterceptor(Protocol.IBatchRequest request) |
| | | 39 | | { |
| | 12 | 40 | | if (this.ClientTimeout.HasValue) |
| | | 41 | | { |
| | 7 | 42 | | request.Timeout = this.ClientTimeout.Value; |
| | | 43 | | } |
| | 12 | 44 | | ITimeoutOptions timeoutOptions = request.Options as ITimeoutOptions; |
| | 12 | 45 | | if (this.ServerTimeout.HasValue && timeoutOptions != null) |
| | | 46 | | { |
| | | 47 | | //TODO: It would be nice if the Parameters.ServerTimeout property were a TimeSpan not an int |
| | 12 | 48 | | timeoutOptions.Timeout = (int)this.ServerTimeout.Value.TotalSeconds; |
| | | 49 | | } |
| | 12 | 50 | | } |
| | | 51 | | } |
| | | 52 | | } |