| | 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.Diagnostics; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Threading; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | | using Microsoft.Rest.Azure; |
| | 11 | | using Models = Microsoft.Azure.Batch.Protocol.Models; |
| | 12 | |
|
| | 13 | | internal class AsyncListJobsEnumerator : PagedEnumeratorBase<CloudJob> |
| | 14 | | { |
| | 15 | | private readonly BatchClient _parentBatchClient; |
| | 16 | | private readonly BehaviorManager _behaviorMgr; |
| | 17 | | private readonly string _jobScheduleIdForcesListByJobSchedule; // if non-null this triggers list-jobs-by-schedul |
| | 18 | | private readonly DetailLevel _detailLevel; |
| | 19 | |
|
| | 20 | | #region // constructors |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// An iterator that lists-jobs (not by schedule) |
| | 24 | | /// </summary> |
| 2 | 25 | | internal AsyncListJobsEnumerator( |
| 2 | 26 | | BatchClient parentBatchClient, |
| 2 | 27 | | BehaviorManager behaviorMgr, |
| 2 | 28 | | DetailLevel detailLevel) |
| | 29 | | { |
| 2 | 30 | | this._parentBatchClient = parentBatchClient; |
| 2 | 31 | | this._behaviorMgr = behaviorMgr; |
| 2 | 32 | | this._detailLevel = detailLevel; |
| 2 | 33 | | } |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// An iterator that calls list-jobs-by-schedule |
| | 37 | | /// </summary> |
| 2 | 38 | | internal AsyncListJobsEnumerator( |
| 2 | 39 | | BatchClient parentBatchClient, |
| 2 | 40 | | string jobScheduleId, |
| 2 | 41 | | BehaviorManager behaviorMgr, |
| 2 | 42 | | DetailLevel detailLevel) |
| | 43 | | { |
| 2 | 44 | | this._parentBatchClient = parentBatchClient; |
| 2 | 45 | | this._jobScheduleIdForcesListByJobSchedule = jobScheduleId; |
| 2 | 46 | | this._behaviorMgr = behaviorMgr; |
| 2 | 47 | | this._detailLevel = detailLevel; |
| 2 | 48 | | } |
| | 49 | |
|
| | 50 | | #endregion // constructors |
| | 51 | |
|
| | 52 | | public override CloudJob Current // for IPagedEnumerator<T> and IEnumerator<T> |
| | 53 | | { |
| | 54 | | get |
| | 55 | | { |
| | 56 | | // start with the current object off of base |
| 0 | 57 | | object curObj = base._currentBatch[base._currentIndex]; |
| | 58 | |
|
| | 59 | | // it must be a protocol object from previous call |
| 0 | 60 | | Models.CloudJob protocolObj = curObj as Models.CloudJob; |
| | 61 | |
|
| | 62 | | Debug.Assert(null != protocolObj); |
| | 63 | |
|
| | 64 | | // wrap protocol object |
| 0 | 65 | | CloudJob wrapped = new CloudJob(this._parentBatchClient, protocolObj, _behaviorMgr.BaseBehaviors); |
| | 66 | |
|
| 0 | 67 | | return wrapped; |
| | 68 | | } |
| | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// fetch another batch of objects from the server |
| | 73 | | /// </summary> |
| | 74 | | protected async override System.Threading.Tasks.Task GetNextBatchFromServerAsync(SkipTokenHandler skipHandler, C |
| | 75 | | { |
| | 76 | | do |
| | 77 | | { |
| | 78 | | // start the protocol layer call |
| 6 | 79 | | if (string.IsNullOrEmpty(_jobScheduleIdForcesListByJobSchedule)) |
| | 80 | | { |
| 4 | 81 | | Task<AzureOperationResponse<IPage<Models.CloudJob>, Models.JobListHeaders>> asyncTask = |
| 4 | 82 | | this._parentBatchClient.ProtocolLayer.ListJobsAll( |
| 4 | 83 | | skipHandler.SkipToken, |
| 4 | 84 | | _behaviorMgr, |
| 4 | 85 | | _detailLevel, |
| 4 | 86 | | cancellationToken); |
| | 87 | |
|
| 4 | 88 | | var response = await asyncTask.ConfigureAwait(continueOnCapturedContext: false); |
| | 89 | |
|
| 2 | 90 | | this.ProcessPage(response.Body, skipHandler); |
| | 91 | | } |
| | 92 | | else |
| | 93 | | { |
| 2 | 94 | | Task<AzureOperationResponse<IPage<Models.CloudJob>, Models.JobListFromJobScheduleHeaders>> asyncTask |
| 2 | 95 | | this._parentBatchClient.ProtocolLayer.ListJobsBySchedule( |
| 2 | 96 | | _jobScheduleIdForcesListByJobSchedule, |
| 2 | 97 | | skipHandler.SkipToken, |
| 2 | 98 | | _behaviorMgr, |
| 2 | 99 | | _detailLevel, |
| 2 | 100 | | cancellationToken); |
| | 101 | |
|
| 2 | 102 | | var response = await asyncTask.ConfigureAwait(continueOnCapturedContext: false); |
| | 103 | |
|
| 2 | 104 | | this.ProcessPage(response.Body, skipHandler); |
| | 105 | | } |
| | 106 | |
|
| | 107 | | } |
| | 108 | | // it is possible for there to be no results so we keep trying |
| 4 | 109 | | while (skipHandler.ThereIsMoreData && ((null == _currentBatch) || _currentBatch.Length <= 0)); |
| 2 | 110 | | } |
| | 111 | |
|
| | 112 | | private void ProcessPage(IPage<Models.CloudJob> currentPage, SkipTokenHandler skipHandler) |
| | 113 | | { |
| | 114 | | // remember any skiptoken returned. This also sets the bool |
| 4 | 115 | | skipHandler.SkipToken = currentPage.NextPageLink; |
| | 116 | |
|
| | 117 | | // remember the protocol tasks returned |
| 4 | 118 | | base._currentBatch = null; |
| | 119 | |
|
| 4 | 120 | | if (null != currentPage.GetEnumerator()) |
| | 121 | | { |
| 4 | 122 | | base._currentBatch = currentPage.ToArray(); |
| | 123 | | } |
| 4 | 124 | | } |
| | 125 | | } |
| | 126 | | } |