コード例 #1
0
        public async Task WatchLoop()
        {
            try
            {
                string consulIndex = "0";
                while (true)
                {
                    var result = await _client.GetAsync(_lookupUrl + consulIndex,
                                                        _cancel);

                    if (!result.IsSuccessStatusCode)
                    {
                        if (_state == WatcherState.UsingLiveValues)
                        {
                            _state = WatcherState.UsingCachedValues;
                        }
                        await Task.Delay(1000);

                        continue;
                    }
                    consulIndex = result.GetConsulIndex();
                    var content = await result.Content.ReadAsStringAsync();

                    var instance = JsonConvert.DeserializeObject <T>(content);
                    Interlocked.Exchange(ref _instances, instance);
                    _state = WatcherState.UsingLiveValues;
                    _haveFirstResults.Set(true);
                    _onNew?.Invoke(instance);
                }
            }
            catch (TaskCanceledException) { /*nom nom */ }
            catch (ObjectDisposedException) { /*nom nom */ }
        }
コード例 #2
0
        private async Task WatchLoop()
        {
            try
            {
                string consulIndex = "0";
                while (true)
                {
                    var result = await _serviceManager.Client.GetAsync(_lookupUrl + consulIndex, _serviceManager.Cancelled);

                    if (!result.IsSuccessStatusCode)
                    {
                        if (_state == WatcherState.UsingLiveValues)
                        {
                            _state = WatcherState.UsingCachedValues;
                        }
                        await Task.Delay(1000);

                        continue;
                    }
                    consulIndex = result.GetConsulIndex();
                    var content = await result.Content.ReadAsStringAsync();

                    var listOfServices = JsonConvert.DeserializeObject <InformationServiceSet[]>(content);
                    Interlocked.Exchange(ref _serviceInstances, listOfServices);
                    _state = WatcherState.UsingLiveValues;
                    _haveFirstResults.Set(true);
                }
            }
            catch (TaskCanceledException) { /*nom nom */ }
            catch (ObjectDisposedException) { /*nom nom */ }
        }
コード例 #3
0
        public void Dispose()
        {
            bool doCleanup = false;

            lock (lockObject)
            {
                if (state != WatcherState.Disposed)
                {
                    EnableRaisingEvents = false;
                    state     = WatcherState.Disposed;
                    doCleanup = true;
                }
            }

            if (doCleanup)
            {
                consumerCancellationTokenSource.Cancel();

                if (!consumerThread.ThreadState.HasFlag(ThreadState.Unstarted))
                {
                    consumerThread.Join();
                }

                consumerCancellationTokenSource.Dispose();
                producerConsumerQueue.Dispose();
            }
        }
コード例 #4
0
        private void Start()
        {
            if (state == WatcherState.Disposed)
            {
                throw new ObjectDisposedException("FileSystemWatcher");
            }

            if (state == WatcherState.Suspended || state == WatcherState.Unstarted)
            {
                if (!owner.Directory.Exists(Path))
                {
                    throw ErrorFactory.System.ErrorReadingTheDirectory(Path);
                }

                if (state == WatcherState.Unstarted)
                {
                    consumerThread.Start();
                }

                changeTracker.FileSystemChanged += HandleFileSystemChange;

                version++;
                state = WatcherState.Active;
            }
        }
コード例 #5
0
        private bool Stop()
        {
            if (state == WatcherState.Active)
            {
                changeTracker.FileSystemChanged -= HandleFileSystemChange;
                state = WatcherState.Suspended;

                return(true);
            }

            return(false);
        }
コード例 #6
0
        private async Task WatcherLoop(HttpClient client)
        {
            while (true)
            {
                try
                {
                    var consulIndex = "0";
                    while (!_cancelationToken.Token.IsCancellationRequested)
                    {
                        var result = await client.GetAsync(_url + consulIndex, _cancelationToken.Token);

                        if (!result.IsSuccessStatusCode)
                        {
                            if (_state == WatcherState.UsingLiveValues)
                            {
                                _state = WatcherState.UsingCachedValues;
                            }
                            await Task.Delay(1000);

                            continue;
                        }
                        var newConsulIndex = result.GetConsulIndex();
                        if (newConsulIndex == consulIndex)
                        {
                            continue;
                        }
                        consulIndex = newConsulIndex;
                        var content = await result.Content.ReadAsStringAsync();

                        var instance = JsonConvert.DeserializeObject <List <InformationServiceSet> >(content);
                        Volatile.Write(ref _instances, instance);
                        _listCallback?.Invoke(instance);
                        _state = WatcherState.UsingLiveValues;
                        _completionSource.TrySetResult(true);
                    }
                }
                catch (Exception ex)
                {
                    _logger?.LogWarning(0, ex, "Error in blocking watcher watching consul");
                }
                _state = _state == WatcherState.NotInitialized ? WatcherState.NotInitialized : WatcherState.UsingCachedValues;
                if (_cancelationToken.Token.IsCancellationRequested)
                {
                    _logger?.LogInformation("Cancelation requested exiting watcher");
                    return;
                }
                await Task.Delay(s_serviceReconnectDelay);
            }
        }
コード例 #7
0
        public void StartWatch()
        {
            lock (_watchStateLock)
            {
                if (_watchState != WatcherState.Stoped)
                {
                    throw new Exception("Reddit watcher already running!");
                }

                _watchState     = WatcherState.Starting;
                _taskCollection = new List <Task>();
                _redditSettings = SettingsController.SettingsStore.RedditSettings;

                _redditSettings.WatchedSubreddits = new List <string>();
                foreach (var subreddit in SettingsController.SettingsStore.TelegramUsers.Select(u => u.Subreddits))
                {
                    _redditSettings.WatchedSubreddits =
                        _redditSettings.WatchedSubreddits.Union(subreddit).ToList();
                }

                _watchState = WatcherState.Running;
                new Thread(GetNews).Start();
            }
        }
コード例 #8
0
 public WatcherStateChangedEventArgs(WatcherState state)
 {
     State = state;
 }
コード例 #9
0
        private async Task WatcherLoop()
        {
            try
            {
                while (_isEnable)
                {
                    try
                    {
                        ulong consulIndex = 0;
                        while (!_cancelationToken.Token.IsCancellationRequested)
                        {
                            //if no change,wait 10 min
                            var result = await GetResponseAsync(_watcherKey,
                                                                new QueryOptions { WaitIndex = consulIndex, WaitTime = TimeSpan.FromMinutes(_waitMaxMin) }, _cancelationToken.Token).ConfigureAwait(false);

                            if (result.StatusCode != HttpStatusCode.OK)
                            {
                                //if resource not found, this watcher will dispose
                                if (result.StatusCode == HttpStatusCode.NotFound)
                                {
                                    Dispose();
                                }
                                else
                                {
                                    if (_state == WatcherState.UsingLiveValues)
                                    {
                                        _state = WatcherState.UsingCachedValues;
                                    }
                                    await Task.Delay(1000);

                                    continue;
                                }
                            }
                            if (result.LastIndex == consulIndex)
                            {
                                continue;
                            }

                            consulIndex = result.LastIndex;
                            Volatile.Write(ref _instances, result.Response);
                            _state = WatcherState.UsingLiveValues;
                            _completionSource.TrySetResult(true);
                            var watcherEventArg = new WatcherEventArg <TTarget>
                            {
                                WatchKey   = _watcherKey,
                                WatchValue = result.Response,
                            };
                            WatchChanged?.Invoke(watcherEventArg);
                            continue;
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger?.LogWarning(0, ex, "Error in blocking watcher watching consul");
                    }
                    _state = _state == WatcherState.NotInitialized ? WatcherState.NotInitialized : WatcherState.UsingCachedValues;
                    if (_cancelationToken.Token.IsCancellationRequested)
                    {
                        _logger?.LogInformation("Cancelation requested exiting watcher");
                        return;
                    }
                    await Task.Delay(_reconnectDelay);
                }
            }
            catch (Exception ex)
            {
                _logger?.LogError(0, ex, "Exception in watcher loop for {_watcherKey}", _watcherKey);
            }
            finally
            {
                _logger?.LogWarning("Exiting watcher loop for {_watcherKey}", _watcherKey);
            }
        }