| | 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.Collections.Generic; |
| | 7 | | using System.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using Microsoft.Rest.Azure; |
| | 10 | | using Models = Microsoft.Azure.Batch.Protocol.Models; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Performs operations on Azure Batch job schedules. |
| | 14 | | /// </summary> |
| | 15 | | /// <seealso cref="CloudJobSchedule"/> |
| | 16 | | public class JobScheduleOperations : IInheritedBehaviors |
| | 17 | | { |
| | 18 | |
|
| | 19 | | #region // constructors |
| | 20 | |
|
| 0 | 21 | | private JobScheduleOperations() |
| | 22 | | { |
| 0 | 23 | | } |
| | 24 | |
|
| 21 | 25 | | internal JobScheduleOperations(BatchClient parentBatchScheduler, IEnumerable<BatchClientBehavior> inheritedBehav |
| | 26 | | { |
| 21 | 27 | | this.ParentBatchClient = parentBatchScheduler; |
| | 28 | |
|
| | 29 | | // set up the behavior inheritance |
| 21 | 30 | | InheritUtil.InheritClientBehaviorsAndSetPublicProperty(this, inheritedBehaviors); |
| 21 | 31 | | } |
| | 32 | |
|
| | 33 | | #endregion //constructors |
| | 34 | |
|
| | 35 | |
|
| | 36 | | #region IInheritedBehaviors |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Gets or sets a list of behaviors that modify or customize requests to the Batch service |
| | 40 | | /// made via this <see cref="JobScheduleOperations"/>. |
| | 41 | | /// </summary> |
| | 42 | | /// <remarks> |
| | 43 | | /// <para>These behaviors are inherited by child objects.</para> |
| | 44 | | /// <para>Modifications are applied in the order of the collection. The last write wins.</para> |
| | 45 | | /// </remarks> |
| 167 | 46 | | public IList<BatchClientBehavior> CustomBehaviors { get; set; } |
| | 47 | |
|
| | 48 | | #endregion IInheritedBehaviors |
| | 49 | |
|
| | 50 | | #region // JobScheduleOperations |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Enumerates the <see cref="CloudJobSchedule">job schedules</see> in the Batch account. |
| | 54 | | /// </summary> |
| | 55 | | /// <param name="detailLevel">A <see cref="DetailLevel"/> used for filtering the list and for controlling which |
| | 56 | | /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are appli |
| | 57 | | /// <returns>An <see cref="IPagedEnumerable{CloudJobSchedule}"/> that can be used to enumerate job schedules asy |
| | 58 | | /// <remarks>This method returns immediately; the job schedules are retrieved from the Batch service only when t |
| | 59 | | /// Retrieval is non-atomic; schedules are retrieved in pages during enumeration of the collection.</remarks> |
| | 60 | | public IPagedEnumerable<CloudJobSchedule> ListJobSchedules(DetailLevel detailLevel = null, IEnumerable<BatchClie |
| | 61 | | { |
| | 62 | | // set up behavior manager |
| 3 | 63 | | BehaviorManager bhMgr = new BehaviorManager(this.CustomBehaviors, additionalBehaviors); |
| | 64 | |
|
| 3 | 65 | | PagedEnumerable<CloudJobSchedule> enumerable = new PagedEnumerable<CloudJobSchedule>( // the lamda will be t |
| 3 | 66 | | () => |
| 3 | 67 | | { |
| 3 | 68 | | // here is the actual strongly typed enumerator |
| 6 | 69 | | AsyncListJobSchedulesEnumerator typedEnumerator = new AsyncListJobSchedulesEnumerator(this, bhMgr, d |
| 3 | 70 | |
|
| 3 | 71 | | // here is the base |
| 3 | 72 | | PagedEnumeratorBase<CloudJobSchedule> enumeratorBase = typedEnumerator; |
| 3 | 73 | |
|
| 6 | 74 | | return enumeratorBase; |
| 3 | 75 | | }); |
| | 76 | |
|
| 3 | 77 | | return enumerable; |
| | 78 | | } |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Gets the specified <see cref="CloudJobSchedule"/>. |
| | 82 | | /// </summary> |
| | 83 | | /// <param name="jobScheduleId">The id of the job schedule to get.</param> |
| | 84 | | /// <param name="detailLevel">A <see cref="DetailLevel"/> used for controlling which properties are retrieved fr |
| | 85 | | /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are appli |
| | 86 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 87 | | /// <returns>A <see cref="CloudJobSchedule"/> containing information about the specified Azure Batch job schedul |
| | 88 | | /// <remarks>The get job schedule operation runs asynchronously.</remarks> |
| | 89 | | public async System.Threading.Tasks.Task<CloudJobSchedule> GetJobScheduleAsync( |
| | 90 | | string jobScheduleId, |
| | 91 | | DetailLevel detailLevel = null, |
| | 92 | | IEnumerable<BatchClientBehavior> additionalBehaviors = null, |
| | 93 | | CancellationToken cancellationToken = default(CancellationToken)) |
| | 94 | | { |
| | 95 | | // set up behavior manager |
| 11 | 96 | | BehaviorManager bhMgr = new BehaviorManager(this.CustomBehaviors, additionalBehaviors, detailLevel); |
| | 97 | |
|
| 11 | 98 | | Task<AzureOperationResponse<Models.CloudJobSchedule, Models.JobScheduleGetHeaders>> asyncTask = |
| 11 | 99 | | this.ParentBatchClient.ProtocolLayer.GetJobSchedule(jobScheduleId, bhMgr, cancellationToken); |
| | 100 | |
|
| 11 | 101 | | AzureOperationResponse<Models.CloudJobSchedule, Models.JobScheduleGetHeaders> result = await asyncTask.Confi |
| | 102 | |
|
| | 103 | | // construct a new object bound to the protocol layer object |
| 10 | 104 | | CloudJobSchedule newWI = new CloudJobSchedule(this.ParentBatchClient, result.Body, this.CustomBehaviors); |
| | 105 | |
|
| 10 | 106 | | return newWI; |
| 10 | 107 | | } |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Gets the specified <see cref="CloudJobSchedule"/>. |
| | 111 | | /// </summary> |
| | 112 | | /// <param name="jobScheduleId">The id of the job schedule to get.</param> |
| | 113 | | /// <param name="detailLevel">A <see cref="DetailLevel"/> used for controlling which properties are retrieved fr |
| | 114 | | /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are appli |
| | 115 | | /// <returns>A <see cref="CloudJobSchedule"/> containing information about the specified Azure Batch job schedul |
| | 116 | | /// <remarks>This is a blocking operation. For a non-blocking equivalent, see <see cref="GetJobScheduleAsync"/>. |
| | 117 | | public CloudJobSchedule GetJobSchedule(string jobScheduleId, DetailLevel detailLevel = null, IEnumerable<BatchCl |
| | 118 | | { |
| 8 | 119 | | Task<CloudJobSchedule> asyncTask = this.GetJobScheduleAsync(jobScheduleId, detailLevel, additionalBehaviors) |
| 8 | 120 | | CloudJobSchedule result = asyncTask.WaitAndUnaggregateException(this.CustomBehaviors, additionalBehaviors); |
| | 121 | |
|
| 8 | 122 | | return result; |
| | 123 | | } |
| | 124 | |
|
| | 125 | | /// <summary> |
| | 126 | | /// Creates an instance of CloudJobSchedule that is unbound and does not have a consistency relationship to any |
| | 127 | | /// </summary> |
| | 128 | | /// <returns>A <see cref="CloudJobSchedule"/> representing a new job schedule that has not been submitted to the |
| | 129 | | public CloudJobSchedule CreateJobSchedule() |
| | 130 | | { |
| 4 | 131 | | CloudJobSchedule newJobSchedule = new CloudJobSchedule(this.ParentBatchClient, this.CustomBehaviors); |
| | 132 | |
|
| 4 | 133 | | return newJobSchedule; |
| | 134 | | } |
| | 135 | |
|
| | 136 | | /// <summary> |
| | 137 | | /// Creates an instance of CloudJobSchedule that is unbound and does not have a consistency relationship to any |
| | 138 | | /// </summary> |
| | 139 | | /// <param name="jobScheduleId">The id of the job schedule.</param> |
| | 140 | | /// <param name="schedule">The schedule that determines when jobs will be created.</param> |
| | 141 | | /// <param name="jobSpecification">a <see cref="JobSpecification" /> containing details of the jobs to be create |
| | 142 | | /// <returns>A <see cref="CloudJobSchedule"/> representing a new job schedule that has not been submitted to the |
| | 143 | | public CloudJobSchedule CreateJobSchedule(string jobScheduleId, Schedule schedule, JobSpecification jobSpecifica |
| | 144 | | { |
| 2 | 145 | | CloudJobSchedule newJobSchedule = new CloudJobSchedule(this.ParentBatchClient, this.CustomBehaviors) |
| 2 | 146 | | { |
| 2 | 147 | | Id = jobScheduleId, |
| 2 | 148 | | Schedule = schedule, |
| 2 | 149 | | JobSpecification = jobSpecification |
| 2 | 150 | | }; |
| | 151 | |
|
| 2 | 152 | | return newJobSchedule; |
| | 153 | | } |
| | 154 | |
|
| | 155 | | /// <summary> |
| | 156 | | /// Enables the specified job schedule, allowing jobs to be created according to its <see cref="CloudJobSchedule |
| | 157 | | /// </summary> |
| | 158 | | /// <param name="jobScheduleId">The id of the job schedule.</param> |
| | 159 | | /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are appli |
| | 160 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 161 | | /// <returns>A <see cref="System.Threading.Tasks.Task"/> that represents the asynchronous operation.</returns> |
| | 162 | | /// <remarks> |
| | 163 | | /// <para>The enable operation runs asynchronously.</para> |
| | 164 | | /// </remarks> |
| | 165 | | public System.Threading.Tasks.Task EnableJobScheduleAsync(string jobScheduleId, IEnumerable<BatchClientBehavior> |
| | 166 | | { |
| | 167 | | // set up behavior manager |
| 1 | 168 | | BehaviorManager bhMgr = new BehaviorManager(this.CustomBehaviors, additionalBehaviors); |
| | 169 | |
|
| 1 | 170 | | System.Threading.Tasks.Task asyncTask = this.ParentBatchClient.ProtocolLayer.EnableJobSchedule(jobScheduleId |
| | 171 | |
|
| 1 | 172 | | return asyncTask; |
| | 173 | | } |
| | 174 | |
|
| | 175 | | /// <summary> |
| | 176 | | /// Enables the specified job schedule, allowing jobs to be created according to its <see cref="CloudJobSchedule |
| | 177 | | /// </summary> |
| | 178 | | /// <param name="jobScheduleId">The id of the job schedule.</param> |
| | 179 | | /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are appli |
| | 180 | | /// <remarks> |
| | 181 | | /// <para>This is a blocking operation. For a non-blocking equivalent, see <see cref="EnableJobScheduleAsync"/>. |
| | 182 | | /// </remarks> |
| | 183 | | public void EnableJobSchedule(string jobScheduleId, IEnumerable<BatchClientBehavior> additionalBehaviors = null) |
| | 184 | | { |
| 0 | 185 | | Task asyncTask = this.EnableJobScheduleAsync(jobScheduleId, additionalBehaviors); |
| 0 | 186 | | asyncTask.WaitAndUnaggregateException(this.CustomBehaviors, additionalBehaviors); |
| 0 | 187 | | } |
| | 188 | |
|
| | 189 | | /// <summary> |
| | 190 | | /// Disables the specified job schedule. Disabled schedules do not create new jobs, but may be re-enabled later |
| | 191 | | /// </summary> |
| | 192 | | /// <param name="jobScheduleId">The id of the job schedule.</param> |
| | 193 | | /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are appli |
| | 194 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 195 | | /// <returns>A <see cref="System.Threading.Tasks.Task"/> that represents the asynchronous operation.</returns> |
| | 196 | | /// <remarks> |
| | 197 | | /// <para>The disable operation runs asynchronously.</para> |
| | 198 | | /// <para>To re-enable the schedule, call <see cref="EnableJobScheduleAsync"/>.</para> |
| | 199 | | /// </remarks> |
| | 200 | | public System.Threading.Tasks.Task DisableJobScheduleAsync(string jobScheduleId, IEnumerable<BatchClientBehavior |
| | 201 | | { |
| | 202 | | // set up behavior manager |
| 1 | 203 | | BehaviorManager bhMgr = new BehaviorManager(this.CustomBehaviors, additionalBehaviors); |
| | 204 | |
|
| 1 | 205 | | System.Threading.Tasks.Task asyncTask = this.ParentBatchClient.ProtocolLayer.DisableJobSchedule(jobScheduleI |
| | 206 | |
|
| 1 | 207 | | return asyncTask; |
| | 208 | | } |
| | 209 | |
|
| | 210 | | /// <summary> |
| | 211 | | /// Disables the specified job schedule. Disabled schedules do not create new jobs, but may be re-enabled later |
| | 212 | | /// </summary> |
| | 213 | | /// <param name="jobScheduleId">The id of the job schedule.</param> |
| | 214 | | /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are appli |
| | 215 | | /// <remarks> |
| | 216 | | /// <para>This is a blocking operation. For a non-blocking equivalent, see <see cref="DisableJobScheduleAsync"/> |
| | 217 | | /// <para>To re-enable the schedule, call <see cref="EnableJobSchedule"/>.</para> |
| | 218 | | /// </remarks> |
| | 219 | | public void DisableJobSchedule(string jobScheduleId, IEnumerable<BatchClientBehavior> additionalBehaviors = null |
| | 220 | | { |
| 0 | 221 | | Task asyncTask = this.DisableJobScheduleAsync(jobScheduleId, additionalBehaviors); |
| 0 | 222 | | asyncTask.WaitAndUnaggregateException(this.CustomBehaviors, additionalBehaviors); |
| 0 | 223 | | } |
| | 224 | |
|
| | 225 | | /// <summary> |
| | 226 | | /// Deletes the specified job schedule. |
| | 227 | | /// </summary> |
| | 228 | | /// <param name="jobScheduleId">The id of the job schedule.</param> |
| | 229 | | /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are appli |
| | 230 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 231 | | /// <returns>A <see cref="System.Threading.Tasks.Task"/> that represents the asynchronous operation.</returns> |
| | 232 | | /// <remarks> |
| | 233 | | /// <para>The delete operation requests that the job schedule be deleted. The request puts the schedule in the |
| | 234 | | /// The Batch service will delete any existing jobs and tasks under the schedule, including any active job, and |
| | 235 | | /// <para>The delete operation runs asynchronously.</para> |
| | 236 | | /// </remarks> |
| | 237 | | public async System.Threading.Tasks.Task DeleteJobScheduleAsync( |
| | 238 | | string jobScheduleId, |
| | 239 | | IEnumerable<BatchClientBehavior> additionalBehaviors = null, |
| | 240 | | CancellationToken cancellationToken = default(CancellationToken)) |
| | 241 | | { |
| | 242 | | // set up behavior manager |
| 1 | 243 | | BehaviorManager bhMgr = new BehaviorManager(this.CustomBehaviors, additionalBehaviors); |
| | 244 | |
|
| 1 | 245 | | Task asyncTask = this.ParentBatchClient.ProtocolLayer.DeleteJobSchedule(jobScheduleId, bhMgr, cancellationTo |
| | 246 | |
|
| 1 | 247 | | await asyncTask.ConfigureAwait(continueOnCapturedContext: false); |
| 0 | 248 | | } |
| | 249 | |
|
| | 250 | | /// <summary> |
| | 251 | | /// Deletes the specified job schedule. |
| | 252 | | /// </summary> |
| | 253 | | /// <param name="jobScheduleId">The id of the job schedule.</param> |
| | 254 | | /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are appli |
| | 255 | | /// <remarks> |
| | 256 | | /// <para>The delete operation requests that the job schedule be deleted. The request puts the schedule in the |
| | 257 | | /// The Batch service will delete any existing jobs and tasks under the schedule, including any active job, and |
| | 258 | | /// <para>This is a blocking operation. For a non-blocking equivalent, see <see cref="DeleteJobScheduleAsync"/>. |
| | 259 | | /// </remarks> |
| | 260 | | public void DeleteJobSchedule(string jobScheduleId, IEnumerable<BatchClientBehavior> additionalBehaviors = null) |
| | 261 | | { |
| 0 | 262 | | Task asyncTask = this.DeleteJobScheduleAsync(jobScheduleId, additionalBehaviors); |
| 0 | 263 | | asyncTask.WaitAndUnaggregateException(this.CustomBehaviors, additionalBehaviors); |
| 0 | 264 | | } |
| | 265 | |
|
| | 266 | | /// <summary> |
| | 267 | | /// Terminates the specified job schedule. |
| | 268 | | /// </summary> |
| | 269 | | /// <param name="jobScheduleId">The id of the job schedule.</param> |
| | 270 | | /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are appli |
| | 271 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 272 | | /// <returns>A <see cref="System.Threading.Tasks.Task"/> that represents the asynchronous operation.</returns> |
| | 273 | | /// <remarks> |
| | 274 | | /// <para>The terminate operation requests that the job schedule be terminated. The request puts the schedule i |
| | 275 | | /// The Batch service will wait for any active job to terminate, and perform the actual job schedule termination |
| | 276 | | /// <para>The terminate operation runs asynchronously.</para> |
| | 277 | | /// </remarks> |
| | 278 | | public async System.Threading.Tasks.Task TerminateJobScheduleAsync( |
| | 279 | | string jobScheduleId, |
| | 280 | | IEnumerable<BatchClientBehavior> additionalBehaviors = null, |
| | 281 | | CancellationToken cancellationToken = default(CancellationToken)) |
| | 282 | | { |
| | 283 | | // set up behavior manager |
| 1 | 284 | | BehaviorManager bhMgr = new BehaviorManager(this.CustomBehaviors, additionalBehaviors); |
| | 285 | |
|
| 1 | 286 | | Task asyncTask = this.ParentBatchClient.ProtocolLayer.TerminateJobSchedule(jobScheduleId, bhMgr, cancellatio |
| | 287 | |
|
| 1 | 288 | | await asyncTask.ConfigureAwait(continueOnCapturedContext: false); |
| 0 | 289 | | } |
| | 290 | |
|
| | 291 | | /// <summary> |
| | 292 | | /// Terminates the specified job schedule. |
| | 293 | | /// </summary> |
| | 294 | | /// <param name="jobScheduleId">The id of the job schedule.</param> |
| | 295 | | /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are appli |
| | 296 | | /// <remarks> |
| | 297 | | /// <para>The terminate operation requests that the job schedule be terminated. The request puts the schedule i |
| | 298 | | /// The Batch service will wait for any active job to terminate, and perform the actual job schedule termination |
| | 299 | | /// <para>This is a blocking operation. For a non-blocking equivalent, see <see cref="TerminateJobScheduleAsync" |
| | 300 | | /// </remarks> |
| | 301 | | public void TerminateJobSchedule(string jobScheduleId, IEnumerable<BatchClientBehavior> additionalBehaviors = nu |
| | 302 | | { |
| 0 | 303 | | Task asyncTask = this.TerminateJobScheduleAsync(jobScheduleId, additionalBehaviors); |
| 0 | 304 | | asyncTask.WaitAndUnaggregateException(this.CustomBehaviors, additionalBehaviors); |
| 0 | 305 | | } |
| | 306 | |
|
| | 307 | | internal IPagedEnumerable<CloudJob> ListJobsImpl(string jobScheduleId, BehaviorManager bhMgr, DetailLevel detail |
| | 308 | | { |
| 2 | 309 | | PagedEnumerable<CloudJob> enumerable = new PagedEnumerable<CloudJob>( // the lamda will be the enumerator fa |
| 2 | 310 | | () => |
| 2 | 311 | | { |
| 2 | 312 | | // here is the actual strongly typed enumerator that lists-by-job-schedule |
| 4 | 313 | | AsyncListJobsEnumerator typedEnumerator = new AsyncListJobsEnumerator(this.ParentBatchClient, jobSch |
| 2 | 314 | |
|
| 2 | 315 | | // here is the base |
| 2 | 316 | | PagedEnumeratorBase<CloudJob> enumeratorBase = typedEnumerator; |
| 2 | 317 | |
|
| 4 | 318 | | return enumeratorBase; |
| 2 | 319 | | }); |
| | 320 | |
|
| 2 | 321 | | return enumerable; |
| | 322 | | } |
| | 323 | |
|
| | 324 | | /// <summary> |
| | 325 | | /// Enumerates the <see cref="CloudJob">jobs</see> created under the specified job schedule. |
| | 326 | | /// </summary> |
| | 327 | | /// <param name="jobScheduleId">The id of the job schedule.</param> |
| | 328 | | /// <param name="detailLevel">A <see cref="DetailLevel"/> used for filtering the list and for controlling which |
| | 329 | | /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are appli |
| | 330 | | /// <returns>An <see cref="IPagedEnumerable{CloudJob}"/> that can be used to enumerate jobs asynchronously or sy |
| | 331 | | /// <remarks>This method returns immediately; the jobs are retrieved from the Batch service only when the collec |
| | 332 | | /// Retrieval is non-atomic; jobs are retrieved in pages during enumeration of the collection.</remarks> |
| | 333 | | public IPagedEnumerable<CloudJob> ListJobs(string jobScheduleId, DetailLevel detailLevel = null, IEnumerable<Bat |
| | 334 | | { |
| | 335 | | // set up behavior manager |
| 2 | 336 | | BehaviorManager bhMgr = new BehaviorManager(this.CustomBehaviors, additionalBehaviors); |
| | 337 | |
|
| 2 | 338 | | IPagedEnumerable<CloudJob> enumerable = ListJobsImpl(jobScheduleId, bhMgr, detailLevel); |
| | 339 | |
|
| 2 | 340 | | return enumerable; |
| | 341 | | } |
| | 342 | |
|
| | 343 | | #endregion // JobScheduleOperations |
| | 344 | |
|
| | 345 | | #region // internal/private |
| | 346 | |
|
| 58 | 347 | | internal BatchClient ParentBatchClient { get; set;} |
| | 348 | |
|
| | 349 | | #endregion // internal/private |
| | 350 | | } |
| | 351 | | } |