| | 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 System.Collections.Generic; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using Protocol; |
| | 10 | | using Rest; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// The dispose pattern sets all references to null. |
| | 14 | | /// Put all references into this box. |
| | 15 | | /// |
| | 16 | | /// ONLY ACCESS VIA GetStateThrowIfNotOpen() method! |
| | 17 | | /// |
| | 18 | | /// </summary> |
| | 19 | | internal class BatchClientDisposableStateBox |
| | 20 | | { |
| | 21 | | private readonly BatchClient _parentBatchClient; |
| | 22 | | private readonly Lazy<ApplicationOperations> _applicationOperations; |
| | 23 | | private readonly Lazy<CertificateOperations> _certificateOperations; |
| | 24 | | private readonly Lazy<JobOperations> _jobOperations; |
| | 25 | | private readonly Lazy<JobScheduleOperations> _jobScheduleOperations; |
| | 26 | | private readonly Lazy<PoolOperations> _poolOperations; |
| | 27 | | private readonly Lazy<Utilities> _utilities; |
| | 28 | |
|
| | 29 | | public IList<BatchClientBehavior> CustomBehaviors; |
| | 30 | | public IProtocolLayer ProtocolLayer; |
| | 31 | |
|
| | 32 | | public BatchClientDisposableStateBox(BatchClient parentBatchClient) |
| | 33 | | { |
| | 34 | | this._parentBatchClient = parentBatchClient; |
| | 35 | | this._applicationOperations = new Lazy<ApplicationOperations>(() => new ApplicationOperations(this._parentBa |
| | 36 | | this._certificateOperations = new Lazy<CertificateOperations>(() => new CertificateOperations(this._parentBa |
| | 37 | | this._jobOperations = new Lazy<JobOperations>(() => new JobOperations(this._parentBatchClient, this.CustomBe |
| | 38 | | this._jobScheduleOperations = new Lazy<JobScheduleOperations>(() => new JobScheduleOperations(this._parentBa |
| | 39 | | this._poolOperations = new Lazy<PoolOperations>(() => new PoolOperations(this._parentBatchClient, this.Custo |
| | 40 | | this._utilities = new Lazy<Utilities>(() => new Utilities(this._parentBatchClient, this.CustomBehaviors)); |
| | 41 | |
|
| | 42 | | this.CustomBehaviors = new List<BatchClientBehavior>(); |
| | 43 | | } |
| | 44 | |
|
| | 45 | | public ApplicationOperations ApplicationOperations => this._applicationOperations.Value; |
| | 46 | | public CertificateOperations CertificateOperations => this._certificateOperations.Value; |
| | 47 | | public JobOperations JobOperations => this._jobOperations.Value; |
| | 48 | | public JobScheduleOperations JobScheduleOperations => this._jobScheduleOperations.Value; |
| | 49 | | public PoolOperations PoolOperations => this._poolOperations.Value; |
| | 50 | | public Utilities Utilities => this._utilities.Value; |
| | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// A client for an Azure Batch account, used to access the Batch service. |
| | 55 | | /// </summary> |
| | 56 | | public class BatchClient : IDisposable |
| | 57 | | { |
| | 58 | | private BatchClientDisposableStateBox _disposableStateBox; // null state box signals that the instance is close |
| | 59 | | private bool _disposed = false; // used for dispose pattern |
| 653 | 60 | | private readonly object _closeLocker = new object(); |
| | 61 | |
|
| | 62 | | #region // constructors |
| | 63 | |
|
| 653 | 64 | | private BatchClient() |
| | 65 | | { |
| 653 | 66 | | _disposableStateBox = new BatchClientDisposableStateBox(this); |
| | 67 | |
|
| | 68 | | // |
| | 69 | | // Add custom behaviors which are by default on every batch client |
| | 70 | | // |
| 653 | 71 | | this.CustomBehaviors.Add(RetryPolicyProvider.ExponentialRetryProvider(TimeSpan.FromSeconds(1), 6)); |
| | 72 | |
|
| | 73 | | //Add default AddTaskResultHandler |
| 653 | 74 | | this.CustomBehaviors.Add(new AddTaskCollectionResultHandler(AddTaskCollectionResultHandler.DefaultAddTaskCol |
| 653 | 75 | | } |
| | 76 | |
|
| 138 | 77 | | private BatchClient(Auth.BatchSharedKeyCredentials credentials) : this() |
| | 78 | | { |
| 138 | 79 | | ServiceClientCredentials proxyCredentials = new Protocol.BatchSharedKeyCredential(credentials.AccountName, c |
| 138 | 80 | | this.ProtocolLayer = new ProtocolLayer(credentials.BaseUrl, proxyCredentials); |
| 138 | 81 | | } |
| | 82 | |
|
| 0 | 83 | | private BatchClient(Auth.BatchTokenCredentials credentials) : this() |
| | 84 | | { |
| 0 | 85 | | ServiceClientCredentials proxyCredentials = new TokenCredentials(new BatchTokenProvider(credentials.TokenPro |
| 0 | 86 | | this.ProtocolLayer = new ProtocolLayer(credentials.BaseUrl, proxyCredentials); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | private BatchClient(Protocol.BatchServiceClient customRestClient) |
| 9 | 90 | | : this() |
| | 91 | | { |
| 9 | 92 | | this.ProtocolLayer = new ProtocolLayer(customRestClient); |
| 9 | 93 | | } |
| | 94 | |
|
| | 95 | | /// <summary> |
| | 96 | | /// Holds the protocol layer to be used for this client instance. |
| | 97 | | /// This enables "mock"ing the protocol layer for testing. |
| | 98 | | /// </summary> |
| | 99 | | internal BatchClient(IProtocolLayer protocolLayer) |
| 0 | 100 | | : this() |
| | 101 | | { |
| 0 | 102 | | this.ProtocolLayer = protocolLayer; |
| 0 | 103 | | } |
| | 104 | |
|
| | 105 | | #endregion Constructors |
| | 106 | |
|
| | 107 | | #region IInheritedBehaviors |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Gets or sets a list of behaviors that modify or customize requests to the Batch service. |
| | 111 | | /// </summary> |
| | 112 | | /// <remarks> |
| | 113 | | /// <para>These behaviors are inherited by child objects.</para> |
| | 114 | | /// <para>Modifications are applied in the order of the collection. The last write wins.</para> |
| | 115 | | /// </remarks> |
| | 116 | | public IList<BatchClientBehavior> CustomBehaviors |
| | 117 | | { |
| | 118 | | get |
| | 119 | | { |
| 7505 | 120 | | return GetStateThrowIfNotOpen().CustomBehaviors; |
| | 121 | | } |
| | 122 | | set |
| | 123 | | { |
| 393 | 124 | | GetStateThrowIfNotOpen().CustomBehaviors = value; |
| 393 | 125 | | } |
| | 126 | | } |
| | 127 | |
|
| | 128 | | #endregion IInheritedBehaviors |
| | 129 | |
|
| | 130 | | #region // BatchClient |
| | 131 | |
|
| | 132 | | /// <summary> |
| | 133 | | /// Gets an <see cref="ApplicationOperations"/> for performing application-related operations on the associated |
| | 134 | | /// </summary> |
| 5 | 135 | | public ApplicationOperations ApplicationOperations => GetStateThrowIfNotOpen().ApplicationOperations; |
| | 136 | |
|
| | 137 | | /// <summary> |
| | 138 | | /// Gets a <see cref="CertificateOperations"/> for performing certificate-related operations on the associated a |
| | 139 | | /// </summary> |
| 13 | 140 | | public CertificateOperations CertificateOperations => GetStateThrowIfNotOpen().CertificateOperations; |
| | 141 | |
|
| | 142 | | /// <summary> |
| | 143 | | /// Gets a <see cref="JobOperations"/> for performing job-related operations on the associated account. |
| | 144 | | /// </summary> |
| 1065 | 145 | | public JobOperations JobOperations => GetStateThrowIfNotOpen().JobOperations; |
| | 146 | |
|
| | 147 | | /// <summary> |
| | 148 | | /// Gets a <see cref="JobScheduleOperations"/> for performing job schedule-related operations on the associated |
| | 149 | | /// </summary> |
| 25 | 150 | | public JobScheduleOperations JobScheduleOperations => GetStateThrowIfNotOpen().JobScheduleOperations; |
| | 151 | |
|
| | 152 | | /// <summary> |
| | 153 | | /// Gets a <see cref="PoolOperations"/> for performing pool-related operations on the associated account. |
| | 154 | | /// </summary> |
| 50 | 155 | | public PoolOperations PoolOperations => GetStateThrowIfNotOpen().PoolOperations; |
| | 156 | |
|
| | 157 | | /// <summary> |
| | 158 | | /// Gets a <see cref="Utilities"/> object containing utility methods for orchestrating multiple Batch operations |
| | 159 | | /// </summary> |
| 5 | 160 | | public Utilities Utilities => GetStateThrowIfNotOpen().Utilities; |
| | 161 | |
|
| | 162 | | /// <summary> |
| | 163 | | /// Creates an instance of <see cref="BatchClient" />. |
| | 164 | | /// </summary> |
| | 165 | | /// <param name="credentials">The Batch account credentials.</param> |
| | 166 | | /// <returns>An instance of <see cref="Microsoft.Azure.Batch.Protocol.BatchServiceClient"/>.</returns> |
| | 167 | | public static BatchClient Open(Auth.BatchSharedKeyCredentials credentials) |
| | 168 | | { |
| 138 | 169 | | if (null == credentials) |
| | 170 | | { |
| 0 | 171 | | throw new ArgumentNullException(nameof(credentials)); |
| | 172 | | } |
| | 173 | |
|
| 138 | 174 | | return new BatchClient(credentials); |
| | 175 | | } |
| | 176 | |
|
| | 177 | | /// <summary> |
| | 178 | | /// Creates an instance of <see cref="BatchClient" />. |
| | 179 | | /// </summary> |
| | 180 | | /// <param name="credentials">The Azure Active Directory Batch account credentials.</param> |
| | 181 | | /// <returns>An instance of <see cref="Microsoft.Azure.Batch.Protocol.BatchServiceClient"/>.</returns> |
| | 182 | | public static BatchClient Open(Auth.BatchTokenCredentials credentials) |
| | 183 | | { |
| 0 | 184 | | if (null == credentials) |
| | 185 | | { |
| 0 | 186 | | throw new ArgumentNullException(nameof(credentials)); |
| | 187 | | } |
| | 188 | |
|
| 0 | 189 | | return new BatchClient(credentials); |
| | 190 | | } |
| | 191 | |
|
| | 192 | | /// <summary> |
| | 193 | | /// Blocking call that creates an instance of <see cref="BatchClient"/> associated with the specified <see cref= |
| | 194 | | /// </summary> |
| | 195 | | /// <param name="restClient">The instance of <see cref="Microsoft.Azure.Batch.Protocol.BatchServiceClient"/> to |
| | 196 | | /// <returns>An instance of <see cref="Microsoft.Azure.Batch.Protocol.BatchServiceClient"/>.</returns> |
| | 197 | | public static BatchClient Open(Protocol.BatchServiceClient restClient) |
| | 198 | | { |
| 9 | 199 | | if (null == restClient) |
| | 200 | | { |
| 0 | 201 | | throw new ArgumentNullException(nameof(restClient)); |
| | 202 | | } |
| | 203 | |
|
| 9 | 204 | | return new BatchClient(restClient); |
| | 205 | | } |
| | 206 | |
|
| | 207 | | #endregion // BatchClient |
| | 208 | |
|
| | 209 | | #region // IDisposable |
| | 210 | |
|
| | 211 | | /// <summary> |
| | 212 | | /// Releases the unmanaged resources and disposes of the managed resources used by the <see cref="Microsoft.Azur |
| | 213 | | /// </summary> |
| | 214 | | public void Dispose() |
| | 215 | | { |
| 145 | 216 | | Dispose(true); |
| | 217 | |
|
| 145 | 218 | | GC.SuppressFinalize(this); |
| 145 | 219 | | } |
| | 220 | |
|
| | 221 | | /// <summary> |
| | 222 | | /// Releases unmanaged resources used by the <see cref="BatchClient"/>, and optionally disposes of managed resou |
| | 223 | | /// </summary> |
| | 224 | | /// <param name="disposing">Indicates whether the object is being disposed or finalized. If true, the object is |
| | 225 | | /// being disposed and can dispose managed resource. If false, the object is being finalized and should only |
| | 226 | | /// release unmanaged resources.</param> |
| | 227 | | protected virtual void Dispose(bool disposing) |
| | 228 | | { |
| 145 | 229 | | if (_disposed) |
| | 230 | | { |
| 0 | 231 | | return; |
| | 232 | | } |
| | 233 | |
|
| 145 | 234 | | if (disposing) |
| | 235 | | { |
| | 236 | | // IDisposable only section |
| | 237 | |
|
| 145 | 238 | | lock (this._closeLocker) |
| | 239 | | { |
| 145 | 240 | | if (this._disposableStateBox != null) |
| | 241 | | { |
| 145 | 242 | | IProtocolLayer localProto = this.ProtocolLayer; |
| 145 | 243 | | localProto.Dispose(); |
| | 244 | |
|
| 145 | 245 | | this._disposableStateBox = null; // null state box signals that the instance is closed |
| | 246 | | } |
| 145 | 247 | | } |
| | 248 | | } |
| | 249 | |
|
| 145 | 250 | | _disposed = true; |
| 145 | 251 | | } |
| | 252 | |
|
| | 253 | | #endregion // IDisposable |
| | 254 | |
|
| | 255 | | #region internal/private methods |
| | 256 | |
|
| | 257 | | /// <summary> |
| | 258 | | /// Enforces that current instance is not "close". |
| | 259 | | /// All access to disposable state should go through this routine. |
| | 260 | | /// </summary> |
| | 261 | | /// <returns></returns> |
| | 262 | | internal BatchClientDisposableStateBox GetStateThrowIfNotOpen() |
| | 263 | | { |
| 9584 | 264 | | BatchClientDisposableStateBox localState = _disposableStateBox; |
| | 265 | |
|
| 9584 | 266 | | if (null != localState) |
| | 267 | | { |
| 9572 | 268 | | return localState; |
| | 269 | | } |
| | 270 | |
|
| | 271 | | // TODO: BatchException is not yet ready for this... do we need to create simpler BatchExceptions for stuff |
| 12 | 272 | | throw new InvalidOperationException(BatchErrorMessages.BatchClientIsClosed); |
| | 273 | | } |
| | 274 | |
|
| | 275 | | /// <summary> |
| | 276 | | /// Holds the protocol layer to be used for this client instance. |
| | 277 | | /// This enables "mock"ing the protocol layer for testing. |
| | 278 | | /// |
| | 279 | | /// Since 100% of all calls indirect through this property, it |
| | 280 | | /// provides a single place to immediately stop all (new) call attempts |
| | 281 | | /// when the underlying BatchClient is closed. |
| | 282 | | /// </summary> |
| | 283 | | internal IProtocolLayer ProtocolLayer |
| | 284 | | { |
| | 285 | | get |
| | 286 | | { |
| 376 | 287 | | IProtocolLayer localProto = GetStateThrowIfNotOpen().ProtocolLayer; |
| | 288 | |
|
| 376 | 289 | | return localProto; |
| | 290 | | } |
| | 291 | |
|
| | 292 | | private set |
| | 293 | | { |
| 147 | 294 | | GetStateThrowIfNotOpen().ProtocolLayer = value; |
| 147 | 295 | | } |
| | 296 | | } |
| | 297 | |
|
| | 298 | | #endregion internal/private methods |
| | 299 | | } |
| | 300 | | } |