-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
AssetConversionOperation.cs
127 lines (111 loc) · 5.41 KB
/
AssetConversionOperation.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
namespace Azure.MixedReality.ObjectAnchors.Conversion
{
/// <summary>
/// A long running asset conversion process for Object Anchors.
/// </summary>
public class AssetConversionOperation : Operation<AssetConversionProperties>
{
private static readonly TimeSpan defaultPollingInterval = TimeSpan.FromSeconds(15);
private readonly ObjectAnchorsConversionClient _objectAnchorsConversionClient;
private readonly Guid _jobId;
private Response<AssetConversionProperties> _lastConversionResponse;
private Response<AssetConversionProperties> _conclusiveConversionResponse;
private bool _conversionEnded;
private object _updateStatusLock = new object();
/// <summary>
/// Initializes a new instance of the <see cref="AssetConversionOperation"/> class.
/// You must call <see cref="UpdateStatus(CancellationToken)"/> or <see cref="UpdateStatusAsync(CancellationToken)"/> before you can get the <see cref="Value"/>.
/// </summary>
/// <param name="jobId">The ID of this operation.</param>
/// <param name="objectAnchorsConversionClient">The client used to check for completion.</param>
public AssetConversionOperation(Guid jobId, ObjectAnchorsConversionClient objectAnchorsConversionClient)
{
_objectAnchorsConversionClient = objectAnchorsConversionClient;
_jobId = jobId;
}
/// <summary> Initializes a new instance of <see cref="AssetConversionOperation" /> for mocking. </summary>
protected AssetConversionOperation() {}
/// <inheritdoc/>
public override string Id => _jobId.ToString();
/// <inheritdoc/>
public override AssetConversionProperties Value
{
get
{
lock (_updateStatusLock)
{
return OperationHelpers.GetValue(ref _lastConversionResponse);
}
}
}
/// <inheritdoc/>
public override bool HasCompleted => _conversionEnded;
/// <inheritdoc/>
public override bool HasValue => _lastConversionResponse != null;
/// <summary>
/// Whether the operation has completed and has a successful final status.
/// </summary>
public bool HasCompletedSuccessfully => HasCompleted && HasValue && (this.Value.ConversionStatus == AssetConversionStatus.Succeeded);
/// <inheritdoc/>
public override Response GetRawResponse() => _lastConversionResponse.GetRawResponse();
/// <inheritdoc/>
public override Response UpdateStatus(CancellationToken cancellationToken = default)
{
Response<AssetConversionProperties> updatedStatus = _objectAnchorsConversionClient.GetAssetConversionStatus(_jobId, cancellationToken: cancellationToken);
lock (_updateStatusLock)
{
_lastConversionResponse = updatedStatus;
switch (updatedStatus.Value.ConversionStatus)
{
case AssetConversionStatus.NotStarted:
case AssetConversionStatus.Running:
_conversionEnded = false;
_conclusiveConversionResponse = null;
return _lastConversionResponse.GetRawResponse();
}
_conversionEnded = true;
_conclusiveConversionResponse = updatedStatus;
return updatedStatus.GetRawResponse();
}
}
/// <inheritdoc/>
public async override ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default)
{
Response<AssetConversionProperties> updatedStatus = await _objectAnchorsConversionClient.GetAssetConversionStatusAsync(_jobId, cancellationToken: cancellationToken).ConfigureAwait(false);
lock (_updateStatusLock)
{
_lastConversionResponse = updatedStatus;
switch (updatedStatus.Value.ConversionStatus)
{
case AssetConversionStatus.NotStarted:
case AssetConversionStatus.Running:
_conversionEnded = false;
_conclusiveConversionResponse = null;
return _lastConversionResponse.GetRawResponse();
}
_conversionEnded = true;
_conclusiveConversionResponse = updatedStatus;
return updatedStatus.GetRawResponse();
}
}
/// <inheritdoc/>
public async override ValueTask<Response<AssetConversionProperties>> WaitForCompletionAsync(CancellationToken cancellationToken = default)
{
return await WaitForCompletionAsync(defaultPollingInterval, cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc/>
public async override ValueTask<Response<AssetConversionProperties>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken)
{
return await OperationHelpers.DefaultWaitForCompletionAsync(this, pollingInterval, cancellationToken).ConfigureAwait(false);
}
}
}