Пример #1
0
        public static Task WatchAsync(this IQueryIndexes queryIndexes, string bucketName, IEnumerable <string> indexNames, Action <WatchQueryIndexOptions> configureOptions)
        {
            var options = new WatchQueryIndexOptions();

            configureOptions(options);

            return(queryIndexes.WatchAsync(bucketName, indexNames, options));
        }
        public async Task WatchAsync(string bucketName, IEnumerable <string> indexNames, WatchQueryIndexOptions options)
        {
            var indexesToWatch = string.Join(", ", indexNames.ToList());

            Logger.LogInformation($"Attempting to watch pending indexes ({indexesToWatch}) for bucket {bucketName}");

            try
            {
                while (!options.CancellationToken.IsCancellationRequested)
                {
                    var indexes = await this.GetAllAsync(bucketName,
                                                         queryOptions => queryOptions.WithCancellationToken(options.CancellationToken)
                                                         );

                    var pendingIndexes = indexes.Where(index => index.State != "online")
                                         .Select(index => index.Name)
                                         .Intersect(indexNames);
                    if (!pendingIndexes.Any())
                    {
                        break;
                    }

                    Logger.LogInformation($"Still waiting for indexes to complete building ({indexesToWatch})");
                    await Task.Delay(WatchIndexSleepDuration);
                }
            }
            catch (TaskCanceledException)
            {
                // application cancelled watch task
            }
            catch (Exception exception)
            {
                Logger.LogError(exception, $"Error trying to watch pending indexes ({indexesToWatch}) for bucket {bucketName}");
                throw;
            }
        }