/// <summary>
        /// Periodically calls the server till the long-running operation completes.
        /// </summary>
        /// <param name="pollingInterval">
        /// The interval between status requests to the server.
        /// The interval can change based on information returned from the server.
        /// For example, the server might communicate to the client that there is not reason to poll for status change sooner than some time.
        /// </param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> used for the periodical service calls.</param>
        /// <returns>The last HTTP response received from the server.</returns>
        /// <remarks>
        /// This method will periodically call UpdateStatusAsync till HasCompleted is true.
        /// An API call is then made to retrieve the status of the documents.
        /// </remarks>
        public async override ValueTask <Response <AsyncPageable <DocumentStatusResult> > > WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken = default)
        {
            while (true)
            {
                await UpdateStatusAsync(cancellationToken).ConfigureAwait(false);

                if (HasCompleted)
                {
                    var response = await _serviceClient.GetOperationDocumentsStatusAsync(new Guid(Id), cancellationToken : cancellationToken).ConfigureAwait(false);

                    _firstPage = Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse());

                    async Task <Page <DocumentStatusResult> > NextPageFunc(string nextLink, int?pageSizeHint)
                    {
                        // TODO: diagnostics scope?
                        try
                        {
                            Response <DocumentStatusResponse> response = await _serviceClient.GetOperationDocumentsStatusNextPageAsync(nextLink, new Guid(Id), cancellationToken : cancellationToken).ConfigureAwait(false);

                            return(Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse()));
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                    }

                    var result = PageableHelpers.CreateAsyncEnumerable(_ => Task.FromResult(_firstPage), NextPageFunc);
                    return(Response.FromValue(result, response));
                }

                await Task.Delay(pollingInterval, cancellationToken).ConfigureAwait(false);
            }
        }
Ejemplo n.º 2
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(DocumentTranslationOperation)}.{nameof(UpdateStatus)}");
                scope.Start();

                try
                {
                    Response <TranslationStatusDetail> update = async
                        ? await _serviceClient.GetOperationStatusAsync(new Guid(Id), cancellationToken).ConfigureAwait(false)
                        : _serviceClient.GetOperationStatus(new Guid(Id), cancellationToken);

                    _response = update.GetRawResponse();

                    _createdOn           = update.Value.CreatedOn;
                    _lastModified        = update.Value.LastModified;
                    _status              = update.Value.Status;
                    _documentsTotal      = update.Value.DocumentsTotal;
                    _documentsFailed     = update.Value.DocumentsFailed;
                    _documentsInProgress = update.Value.DocumentsInProgress;
                    _documentsSucceeded  = update.Value.DocumentsSucceeded;
                    _documentsNotStarted = update.Value.DocumentsNotStarted;
                    _documentsCancelled  = update.Value.DocumentsCancelled;

                    if (update.Value.Status == TranslationStatus.Succeeded ||
                        update.Value.Status == TranslationStatus.Cancelled ||
                        update.Value.Status == TranslationStatus.Failed)
                    {
                        // we need to first assign a value and then mark the operation as completed to avoid race conditions
                        var response = async
                            ? await _serviceClient.GetOperationDocumentsStatusAsync(new Guid(Id), cancellationToken : cancellationToken).ConfigureAwait(false)
                            : _serviceClient.GetOperationDocumentsStatus(new Guid(Id), cancellationToken: cancellationToken);

                        _firstPage    = Page.FromValues(response.Value.Value, response.Value.NextLink, response.GetRawResponse());
                        _hasCompleted = true;
                    }
                    else if (update.Value.Status == TranslationStatus.ValidationFailed)
                    {
                        _requestFailedException = _diagnostics.CreateRequestFailedException(_response);
                        _hasCompleted           = true;
                        throw _requestFailedException;
                    }
                }
                catch (Exception e)
                {
                    scope.Failed(e);
                    throw;
                }
            }

            return(GetRawResponse());
        }