-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
PersonalizerCreateEvaluationOperation.cs
188 lines (165 loc) · 7.56 KB
/
PersonalizerCreateEvaluationOperation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
namespace Azure.AI.Personalizer
{
/// <summary>
/// An <see cref="Operation{PersonalizerEvaluation}"/> for tracking the status of a
/// <see cref="PersonalizerAdministrationClient.CreatePersonalizerEvaluationAsync(PersonalizerEvaluationOptions, CancellationToken)"/>
/// request. Its <see cref="Operation{PersonalizerEvaluation}.Value"/> upon successful
/// completion will be the completed personalizer evaluation.
/// </summary>
public class PersonalizerCreateEvaluationOperation : Operation<PersonalizerEvaluation>
{
/// <summary>
/// The client used to check for completion.
/// </summary>
private readonly PersonalizerAdministrationClient _client;
/// <summary>
/// The CancellationToken to use for all status checking.
/// </summary>
private readonly CancellationToken _cancellationToken;
/// <summary>
/// Whether the operation has completed.
/// </summary>
private bool _hasCompleted;
/// <summary>
/// Gets the personalizer evaluation once it is completed.
/// </summary>
private PersonalizerEvaluation _value;
private Response _rawResponse;
/// <summary>
/// Gets a value indicating whether the operation has completed.
/// </summary>
public override bool HasCompleted => _hasCompleted;
/// <summary>
/// Gets a value indicating whether the operation completed and
/// successfully produced a value. The <see cref="Operation{PersonalizerEvaluation}.Value"/>
/// property completed evaluation.
/// </summary>
public override bool HasValue => _value != null;
/// <inheritdoc />
public override string Id { get; }
/// <summary>
/// Gets the personalizer evaluation.
/// </summary>
public override PersonalizerEvaluation Value => OperationHelpers.GetValue(ref _value);
/// <inheritdoc />
public override Response GetRawResponse() => _rawResponse;
/// <inheritdoc />
public override ValueTask<Response<PersonalizerEvaluation>> WaitForCompletionAsync(CancellationToken cancellationToken = default) =>
this.DefaultWaitForCompletionAsync(cancellationToken);
/// <inheritdoc />
public override ValueTask<Response<PersonalizerEvaluation>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken) =>
this.DefaultWaitForCompletionAsync(pollingInterval, cancellationToken);
/// <summary>
/// Initializes a new <see cref="PersonalizerCreateEvaluationOperation"/> instance for
/// mocking.
/// </summary>
protected PersonalizerCreateEvaluationOperation()
{
}
/// <summary>
/// Initializes a new <see cref="PersonalizerCreateEvaluationOperation"/> instance.
/// </summary>
/// <param name="client">
/// The client used to check for completion.
/// </param>
/// <param name="evaluationId">The ID of the evaluation.</param>
public PersonalizerCreateEvaluationOperation(string evaluationId, PersonalizerAdministrationClient client) :
this(client, evaluationId, null, CancellationToken.None)
{
}
/// <summary>
/// Initializes a new <see cref="PersonalizerCreateEvaluationOperation"/> instance.
/// </summary>
/// <param name="client">
/// The client used to check for completion.
/// </param>
/// <param name="evaluationId">The ID of the evaluation.</param>
/// <param name="initialResponse">
/// Either the response from initiating the operation or getting the
/// status if we're creating an operation from an existing ID.
/// </param>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be cancelled.
/// </param>
internal PersonalizerCreateEvaluationOperation(
PersonalizerAdministrationClient client,
string evaluationId,
Response initialResponse,
CancellationToken cancellationToken)
{
Id = evaluationId;
_value = null;
_rawResponse = initialResponse;
_client = client;
_cancellationToken = cancellationToken;
}
/// <summary>
/// Check for the latest status of the personalizer evaluation.
/// </summary>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be cancelled.
/// </param>
/// <returns>The <see cref="Response"/> with the status update.</returns>
public override Response UpdateStatus(
CancellationToken cancellationToken = default) =>
UpdateStatusAsync(false, cancellationToken).EnsureCompleted();
/// <summary>
/// Check for the latest status of the personalizer evaluation.
/// </summary>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be cancelled.
/// </param>
/// <returns>The <see cref="Response"/> with the status update.</returns>
public override async ValueTask<Response> UpdateStatusAsync(
CancellationToken cancellationToken = default) =>
await UpdateStatusAsync(true, cancellationToken).ConfigureAwait(false);
/// <summary>
/// Check for the latest status of the personalizer evaluation.
/// </summary>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be cancelled.
/// </param>
/// <param name="async" />
/// <returns>The <see cref="Response"/> with the status update.</returns>
private async Task<Response> UpdateStatusAsync(bool async, CancellationToken cancellationToken)
{
// Short-circuit when already completed (which improves mocking
// scenarios that won't have a client).
if (HasCompleted)
{
return GetRawResponse();
}
// Use our original CancellationToken if the user didn't provide one
if (cancellationToken == default)
{
cancellationToken = _cancellationToken;
}
// Get the latest status
Response<PersonalizerEvaluation> evaluation = async
? await _client.GetPersonalizerEvaluationAsync(Id, cancellationToken: cancellationToken).ConfigureAwait(false)
: _client.GetPersonalizerEvaluation(Id, cancellationToken: cancellationToken);
// Check if the operation is no longer running
if (evaluation.Value.Status != PersonalizerEvaluationJobStatus.Pending)
{
_hasCompleted = true;
_value = evaluation.Value;
}
// Save this update as the latest raw response indicating the state
// of the copy operation
Response response = evaluation.GetRawResponse();
_rawResponse = response;
return response;
}
}
}