示例#1
0
        public async ValueTask WatchObjectChangesAsync <T>(string url, CancellationToken cancellationToken, IKubernetesWatcher <T> watcher) where T : new()
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                throw new ArgumentNullException(nameof(url));
            }

            if (watcher == null)
            {
                throw new ArgumentNullException(nameof(watcher));
            }

            using (var stream = await StreamAsync(url))
            {
                using (cancellationToken.Register(() => stream.Dispose()))
                {
                    using (var streamReader = new StreamReader(stream))
                    {
                        try
                        {
                            var line = default(string);

                            while ((line = await streamReader.ReadLineAsync().ConfigureAwait(false)) != null)
                            {
                                var ms   = new MemoryStream(Encoding.UTF8.GetBytes(line));
                                var item = await JsonSerializer.DeserializeAsync <Watch <T> >(ms, this.serializerOptions);

                                if (item != null)
                                {
                                    await watcher.Change(item).ConfigureAwait(false);
                                }
                            }
                        }
                        catch (IOException) { } // TODO: Check if this happens always or only at tests
                        catch (ObjectDisposedException)
                        {
                            // If cancellation signs the end of reading operation we need to swallow it
                        }
                        catch (OperationCanceledException)
                        {
                            // If cancellation signs the end of reading operation we need to swallow it
                        }
                        catch (Exception ex)
                        {
                            await watcher.Error(ex).ConfigureAwait(false);
                        }
                    }
                }
            }
        }
示例#2
0
        public async Task WatchObjectChangesAsync <T>(string url, CancellationToken cancellationToken, IKubernetesWatcher <T> watcher) where T : new()
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                throw new ArgumentNullException(nameof(url));
            }

            if (watcher == null)
            {
                throw new ArgumentNullException(nameof(watcher));
            }

            using (var stream = await StreamAsync(url))
            {
                using (cancellationToken.Register(() => stream.Dispose()))
                {
                    using (var streamReader = new StreamReader(stream))
                    {
                        try
                        {
                            var line = default(string);

                            while ((line = await streamReader.ReadLineAsync().ConfigureAwait(false)) != null)
                            {
                                var item = JsonConvert.DeserializeObject <Watch <T> >(line);

                                if (item != null)
                                {
                                    await watcher.Change(item).ConfigureAwait(false);
                                }
                            }
                        }
                        catch (ObjectDisposedException)
                        {
                            // If cancellation signs the end of reading operation we need to swallow it
                        }
                        catch (OperationCanceledException)
                        {
                            // If cancellation signs the end of reading operation we need to swallow it
                        }
                        catch (Exception ex)
                        {
                            await watcher.Error(ex).ConfigureAwait(false);
                        }
                    }
                }
            }
        }