Пример #1
0
        protected Task WaitForJobCancellation(List <string> jobIDs, DocumentTranslationClient client)
        {
            if (Mode == RecordedTestMode.Playback)
            {
                return(Task.CompletedTask);
            }

            using (Recording.DisableRecording())
            {
                var options = new GetTranslationStatusesOptions();

                foreach (var result in jobIDs)
                {
                    options.Ids.Add(result);
                }

                bool cancellationHasPropagated = false; // flag for successful cancelling

                return(TestRetryHelper.RetryAsync(async() =>
                {
                    var statuses = await client.GetTranslationStatusesAsync(options: options).ToEnumerableAsync();
                    cancellationHasPropagated = statuses.TrueForAll(status => status.Status == DocumentTranslationStatus.Canceled);

                    if (!cancellationHasPropagated)
                    {
                        throw new InvalidOperationException("Cancellation not propagated to all documents");
                    }
                    else
                    {
                        return (Response)null;
                    }
                },
                                                  maxIterations: 100, delay: TimeSpan.FromSeconds(5)));
            }
        }
Пример #2
0
        public void OperationsHistory()
        {
#if SNIPPET
            string endpoint = "<Document Translator Resource Endpoint>";
            string apiKey   = "<Document Translator Resource API Key>";
#else
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;
#endif

            var client = new DocumentTranslationClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

            int operationsCount = 0;
            int totalDocs       = 0;
            int docsCanceled    = 0;
            int docsSucceeded   = 0;
            int docsFailed      = 0;

            TimeSpan pollingInterval = new(1000);

            DateTimeOffset lastWeekTimestamp = DateTimeOffset.Now.AddDays(-7);

            var options = new GetTranslationStatusesOptions
            {
                CreatedAfter = lastWeekTimestamp
            };

            foreach (TranslationStatusResult translationStatus in client.GetTranslationStatuses(options))
            {
                if (translationStatus.Status == DocumentTranslationStatus.NotStarted ||
                    translationStatus.Status == DocumentTranslationStatus.Running)
                {
                    DocumentTranslationOperation operation = new DocumentTranslationOperation(translationStatus.Id, client);
                    operation.UpdateStatus();

                    while (!operation.HasCompleted)
                    {
                        if (operation.GetRawResponse().Headers.TryGetValue("Retry-After", out string value))
                        {
                            pollingInterval = TimeSpan.FromSeconds(Convert.ToInt32(value));
                        }

                        Thread.Sleep(pollingInterval);
                        operation.UpdateStatus();
                    }
                }

                operationsCount++;
                totalDocs     += translationStatus.DocumentsTotal;
                docsCanceled  += translationStatus.DocumentsCanceled;
                docsSucceeded += translationStatus.DocumentsSucceeded;
                docsFailed    += translationStatus.DocumentsFailed;
            }

            Console.WriteLine($"# of operations: {operationsCount}");
            Console.WriteLine($"Total Documents: {totalDocs}");
            Console.WriteLine($"Succeeded Document: {docsSucceeded}");
            Console.WriteLine($"Failed Document: {docsFailed}");
            Console.WriteLine($"Cancelled Documents: {docsCanceled}");
        }
        public async Task OperationsHistoryAsync()
        {
#if SNIPPET
            string endpoint = "<Document Translator Resource Endpoint>";
            string apiKey   = "<Document Translator Resource API Key>";
#else
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;
#endif

            var client = new DocumentTranslationClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

            #region Snippet:OperationsHistoryAsync

            int operationsCount = 0;
            int totalDocs       = 0;
            int docsCanceled    = 0;
            int docsSucceeded   = 0;
            int docsFailed      = 0;

            DateTimeOffset lastWeekTimestamp = DateTimeOffset.Now.AddDays(-7);

            var options = new GetTranslationStatusesOptions
            {
                CreatedAfter = lastWeekTimestamp
            };

            await foreach (TranslationStatusResult translationStatus in client.GetTranslationStatusesAsync(options))
            {
                if (translationStatus.Status == DocumentTranslationStatus.NotStarted ||
                    translationStatus.Status == DocumentTranslationStatus.Running)
                {
                    DocumentTranslationOperation operation = new DocumentTranslationOperation(translationStatus.Id, client);
                    await operation.WaitForCompletionAsync();
                }

                operationsCount++;
                totalDocs     += translationStatus.DocumentsTotal;
                docsCanceled  += translationStatus.DocumentsCanceled;
                docsSucceeded += translationStatus.DocumentsSucceeded;
                docsFailed    += translationStatus.DocumentsFailed;
            }

            Console.WriteLine($"# of operations: {operationsCount}");
            Console.WriteLine($"Total Documents: {totalDocs}");
            Console.WriteLine($"Succeeded Document: {docsSucceeded}");
            Console.WriteLine($"Failed Document: {docsFailed}");
            Console.WriteLine($"Canceled Documents: {docsCanceled}");

            #endregion
        }