/// <summary>
        /// Calls the server to get updated status of the long-running operation.
        /// </summary>
        /// <param name="async">When <c>true</c>, the method will be executed asynchronously; otherwise, it will execute synchronously.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> used for the service call.</param>
        /// <returns>The HTTP response received from the server.</returns>
        private async ValueTask <Response> UpdateStatusAsync(bool async, CancellationToken cancellationToken)
        {
            if (!_hasCompleted)
            {
                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(AnalyzeBatchActionsOperation)}.{nameof(UpdateStatus)}");
                scope.Start();

                try
                {
                    Response <AnalyzeJobState> update = async
                        ? await _serviceClient.AnalyzeStatusAsync(Id, _showStats, null, null, cancellationToken).ConfigureAwait(false)
                        : _serviceClient.AnalyzeStatus(Id, _showStats, null, null, cancellationToken);

                    _response = update.GetRawResponse();

                    _displayName       = update.Value.DisplayName;
                    _createdOn         = update.Value.CreatedDateTime;
                    _expiresOn         = update.Value.ExpirationDateTime;
                    _lastModified      = update.Value.LastUpdateDateTime;
                    _status            = update.Value.Status;
                    _actionsFailed     = update.Value.Tasks.Failed;
                    _actionsInProgress = update.Value.Tasks.InProgress;
                    _actionSucceeded   = update.Value.Tasks.Completed;
                    _totalActions      = update.Value.Tasks.Total;

                    if (update.Value.Status == TextAnalyticsOperationStatus.Succeeded || update.Value.Status == TextAnalyticsOperationStatus.PartiallySucceeded)
                    {
                        // we need to first assign a value and then mark the operation as completed to avoid race conditions
                        var nextLink = update.Value.NextLink;
                        var value    = Transforms.ConvertToAnalyzeOperationResult(update.Value, _idToIndexMap);
                        _firstPage = Page.FromValues(new List <AnalyzeBatchActionsResult>()
                        {
                            value
                        }, nextLink, _response);
                        _hasCompleted = true;
                    }
                    else if (update.Value.Status == TextAnalyticsOperationStatus.Failed)
                    {
                        _requestFailedException = await ClientCommon.CreateExceptionForFailedOperationAsync(async, _diagnostics, _response, update.Value.Errors).ConfigureAwait(false);

                        _hasCompleted = true;
                        throw _requestFailedException;
                    }
                }
                catch (Exception e)
                {
                    scope.Failed(e);
                    throw;
                }
            }

            return(GetRawResponse());
        }
        async ValueTask <OperationState <AsyncPageable <AnalyzeActionsResult> > > IOperation <AsyncPageable <AnalyzeActionsResult> > .UpdateStateAsync(bool async, CancellationToken cancellationToken)
        {
            Response <AnalyzeJobState> response = async
                ? await _serviceClient.AnalyzeStatusAsync(Id, _showStats, null, null, cancellationToken).ConfigureAwait(false)
                : _serviceClient.AnalyzeStatus(Id, _showStats, null, null, cancellationToken);

            // Add lock to avoid race condition?
            _displayName       = response.Value.DisplayName;
            _createdOn         = response.Value.CreatedDateTime;
            _expiresOn         = response.Value.ExpirationDateTime;
            _lastModified      = response.Value.LastUpdateDateTime;
            _status            = response.Value.Status;
            _actionsFailed     = response.Value.Tasks.Failed;
            _actionsInProgress = response.Value.Tasks.InProgress;
            _actionSucceeded   = response.Value.Tasks.Completed;
            _actionsTotal      = response.Value.Tasks.Total;

            Response rawResponse = response.GetRawResponse();

            if (response.Value.Status == TextAnalyticsOperationStatus.Failed)
            {
                if (CheckIfGenericError(response.Value))
                {
                    RequestFailedException requestFailedException = await ClientCommon.CreateExceptionForFailedOperationAsync(async, _diagnostics, rawResponse, response.Value.Errors).ConfigureAwait(false);

                    return(OperationState <AsyncPageable <AnalyzeActionsResult> > .Failure(rawResponse, requestFailedException));
                }
            }

            if (response.Value.Status == TextAnalyticsOperationStatus.Succeeded ||
                response.Value.Status == TextAnalyticsOperationStatus.Failed)
            {
                string nextLink            = response.Value.NextLink;
                AnalyzeActionsResult value = Transforms.ConvertToAnalyzeActionsResult(response.Value, _idToIndexMap);
                _firstPage = Page.FromValues(new List <AnalyzeActionsResult>()
                {
                    value
                }, nextLink, rawResponse);

                return(OperationState <AsyncPageable <AnalyzeActionsResult> > .Success(rawResponse, CreateOperationValueAsync(CancellationToken.None)));
            }

            return(OperationState <AsyncPageable <AnalyzeActionsResult> > .Pending(rawResponse));
        }
Beispiel #3
0
        /// <summary>
        /// Calls the server to get updated status of the long-running operation.
        /// </summary>
        /// <param name="async">When <c>true</c>, the method will be executed asynchronously; otherwise, it will execute synchronously.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> used for the service call.</param>
        /// <returns>The HTTP response received from the server.</returns>
        private async ValueTask <Response> UpdateStatusAsync(bool async, CancellationToken cancellationToken)
        {
            if (!_hasCompleted)
            {
                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(AnalyzeOperation)}.{nameof(UpdateStatus)}");
                scope.Start();

                try
                {
                    Response <AnalyzeJobState> update = async
                        ? await _serviceClient.AnalyzeStatusAsync(Id, _showStats, null, null, cancellationToken).ConfigureAwait(false)
                        : _serviceClient.AnalyzeStatus(Id, _showStats, null, null, cancellationToken);

                    _response = update.GetRawResponse();

                    if (update.Value.Status == JobStatus.Succeeded)
                    {
                        // we need to first assign a vaue and then mark the operation as completed to avoid race conditions
                        _value = Transforms.ConvertToAnalyzeOperationResult(update.Value, _idToIndexMap);

                        NextLink      = update.Value.NextLink;
                        _hasCompleted = true;
                    }
                    else if (update.Value.Status == JobStatus.Failed)
                    {
                        _requestFailedException = await ClientCommon.CreateExceptionForFailedOperationAsync(async, _diagnostics, _response, update.Value.Errors).ConfigureAwait(false);

                        _hasCompleted = true;
                        throw _requestFailedException;
                    }
                }
                catch (Exception e)
                {
                    scope.Failed(e);
                    throw;
                }
            }

            return(GetRawResponse());
        }
        async ValueTask <OperationState <AsyncPageable <AnalyzeActionsResult> > > IOperation <AsyncPageable <AnalyzeActionsResult> > .UpdateStateAsync(bool async, CancellationToken cancellationToken)
        {
            Response <AnalyzeJobState> response = async
                ? await _serviceClient.AnalyzeStatusAsync(Id, _showStats, null, null, cancellationToken).ConfigureAwait(false)
                : _serviceClient.AnalyzeStatus(Id, _showStats, null, null, cancellationToken);

            // Add lock to avoid race condition?
            _displayName       = response.Value.DisplayName;
            _createdOn         = response.Value.CreatedDateTime;
            _expiresOn         = response.Value.ExpirationDateTime;
            _lastModified      = response.Value.LastUpdateDateTime;
            _status            = response.Value.Status;
            _actionsFailed     = response.Value.Tasks.Failed;
            _actionsInProgress = response.Value.Tasks.InProgress;
            _actionSucceeded   = response.Value.Tasks.Completed;
            _actionsTotal      = response.Value.Tasks.Total;

            Response rawResponse = response.GetRawResponse();

            // TODO - Remove PartiallySucceeded once service deploys this to WestUS2
            if (response.Value.Status == TextAnalyticsOperationStatus.Succeeded ||
                response.Value.Status == TextAnalyticsOperationStatus.PartiallySucceeded ||
                response.Value.Status == TextAnalyticsOperationStatus.PartiallyCompleted ||
                response.Value.Status == TextAnalyticsOperationStatus.Failed)
            {
                string nextLink            = response.Value.NextLink;
                AnalyzeActionsResult value = Transforms.ConvertToAnalyzeActionsResult(response.Value, _idToIndexMap);
                _firstPage = Page.FromValues(new List <AnalyzeActionsResult>()
                {
                    value
                }, nextLink, rawResponse);

                return(OperationState <AsyncPageable <AnalyzeActionsResult> > .Success(rawResponse, CreateOperationValueAsync()));
            }

            return(OperationState <AsyncPageable <AnalyzeActionsResult> > .Pending(rawResponse));
        }