/// <summary> /// Store Weather Request and response info in DB /// </summary> /// <param name="watchRequest">Watch request data</param> /// <param name="weatherInfo">Weather data</param> /// <param name="locationInfo">Location data</param> /// <param name="exchangeRateInfo">Exchange rate data</param> public virtual async Task SaveRequestInfo( [NotNull] WatchRequest watchRequest, [NotNull] WeatherInfo weatherInfo, [NotNull] LocationInfo locationInfo, [NotNull] ExchangeRateInfo exchangeRateInfo) { await using var dbWatchServer = _connectionFactory.Create(); var deviceData = dbWatchServer.QueryProc <DeviceData>( "add_device", new DataParameter("device_id", watchRequest.DeviceId ?? "unknown"), new DataParameter("device_name", watchRequest.DeviceName)) .Single(); var requestData = _mapper.Map <RequestData>(watchRequest); requestData = _mapper.Map(weatherInfo, requestData); requestData = _mapper.Map(locationInfo, requestData); requestData = _mapper.Map(exchangeRateInfo, requestData); requestData.DeviceDataId = deviceData.Id; requestData.RequestTime = DateTime.UtcNow; await dbWatchServer.GetTable <RequestData>().DataContext.InsertAsync(requestData); _logger.LogDebug("{@requestInfo}", requestData); }
private static async Task WaitForNextElection() { var watchId = 100500; var request = new WatchRequest() { CreateRequest = new WatchCreateRequest() { Key = ByteString.CopyFromUtf8(LeaderKey), WatchId = watchId } }; var canObtainLeadership = false; EtcdClient.Watch(request, async response => { foreach (var @event in response.Events) { if (@event.Type == Event.Types.EventType.Delete) { Console.WriteLine("Leader died. Election needed."); canObtainLeadership = true; } } }); while (!canObtainLeadership) { Console.WriteLine("Waiting for leader to die."); await DoWork(); } }
/// <summary> /// Watches a key according to the specified watch request and /// passes the watch response to the method provided. /// </summary> /// <param name="request">Watch Request containing key to be watched</param> /// <param name="method">Method to which watch response should be passed on</param> public async void Watch(WatchRequest request, Action <WatchResponse> method) { try { using (AsyncDuplexStreamingCall <WatchRequest, WatchResponse> watcher = _watchClient.Watch()) { Task watcherTask = Task.Run(async() => { while (await watcher.ResponseStream.MoveNext()) { WatchResponse update = watcher.ResponseStream.Current; method(update); } }); await watcher.RequestStream.WriteAsync(request); await watcher.RequestStream.CompleteAsync(); await watcherTask; } } catch (RpcException) { // If connection issue, then re-initate the watch ResetConnection(); Watch(request, method); } catch { throw; } }
/// <summary> /// Watches a key range according to the specified watch request and /// passes the minimal watch event data to the methods provided. /// </summary> /// <param name="request">Watch Request containing key to be watched</param> /// <param name="methods">Methods to which minimal watch events data should be passed on</param> public async void WatchRange(WatchRequest request, Action <WatchEvent[]>[] methods, Metadata headers = null) { using (AsyncDuplexStreamingCall <WatchRequest, WatchResponse> watcher = _balancer.GetConnection().watchClient.Watch(headers)) { Task watcherTask = Task.Run(async() => { while (await watcher.ResponseStream.MoveNext()) { WatchResponse update = watcher.ResponseStream.Current; foreach (Action <WatchEvent[]> method in methods) { method(update.Events.Select(i => { return(new WatchEvent { Key = i.Kv.Key.ToStringUtf8(), Value = i.Kv.Value.ToStringUtf8(), Type = i.Type }); }).ToArray() ); } } }); await watcher.RequestStream.WriteAsync(request); await watcher.RequestStream.CompleteAsync(); await watcherTask; } }
/// <summary> /// Watches the specified key range and passes the minimal watch events data to the method provided. /// </summary> /// <param name="keys">Keys to be watched</param> /// <param name="methods">Methods to which minimal watch events data should be passed on</param> public async void WatchRange(string[] paths, Action <WatchEvent[]>[] methods) { try { List <WatchRequest> requests = new List <WatchRequest>(); foreach (string path in paths) { WatchRequest request = new WatchRequest() { CreateRequest = new WatchCreateRequest() { Key = ByteString.CopyFromUtf8(path), RangeEnd = ByteString.CopyFromUtf8(GetRangeEnd(path)) } }; requests.Add(request); } Watch(requests.ToArray(), methods); } catch { throw; } }
private void ReloadData() { WatchRequest request = new WatchRequest() { CreateRequest = new WatchCreateRequest() { //需要转换一个格式,因为etcd v3版本的接口都包含在grpc的定义中 Key = ByteString.CopyFromUtf8() } }; _etcdClient.Watch(request, rsp => { if (rsp.Events.Any()) { var @event = rsp.Events[0]; //需要转换一个格式,因为etcd v3版本的接口都包含在grpc的定义中 Data = ConvertData(@event.Kv.Value.ToStringUtf8()); //需要调用ConfigurationProvider的OnReload方法触发ConfigurationReloadToken通知 //这样才能对使用Configuration的类发送数据变更通知 //比如IOptionsMonitor就是通过ConfigurationReloadToken通知变更数据的 OnReload(); } }); }
public void Watch(string key, Action <string?, string?> callback) { var req = new WatchRequest { CreateRequest = new WatchCreateRequest { Key = ByteString.CopyFromUtf8(key), }, }; client.Watch(key, resp => { previousWatchedValues.TryGetValue(key, out string?previousWatchedValue); if (resp.Events.Any()) { var evt = resp.Events.First(); var value = evt.Kv.Value.ToStringUtf8(); callback?.Invoke(previousWatchedValue, value); previousWatchedValues[key] = value; } else { callback?.Invoke(previousWatchedValue, null); previousWatchedValues.TryRemove(key, out _); } }); }
/// <summary> /// Save Data into persistent storage /// </summary> private async Task SaveData( WatchRequest watchRequest, WeatherInfo weatherInfo, LocationInfo locationInfo, ExchangeRateInfo exchangeRateInfo) { await using var dbConnection = _dbFactory.Create(); var deviceData = await dbConnection.GetTable <DeviceData>() .SingleOrDefaultAsync(_ => _.DeviceId == watchRequest.DeviceId); if (deviceData == null) { deviceData = new DeviceData { DeviceId = watchRequest.DeviceId, DeviceName = watchRequest.DeviceName, FirstRequestTime = watchRequest.RequestTime }; deviceData.Id = await dbConnection.GetTable <DeviceData>().DataContext.InsertWithInt32IdentityAsync(deviceData); } var requestData = _mapper.Map <RequestData>(watchRequest); requestData = _mapper.Map(weatherInfo, requestData); requestData = _mapper.Map(locationInfo, requestData); requestData = _mapper.Map(exchangeRateInfo, requestData); requestData.DeviceDataId = deviceData.Id; await dbConnection.GetTable <RequestData>().DataContext.InsertAsync(requestData); }
public async Task SaveDbShouldCorrectlyGetDataFromQuery() { // Arrange // var connectionSettings = _factory.Services.GetRequiredService <IConnectionSettings>(); var dbConnection = new DataConnectionFactory(connectionSettings.GetDataProvider(), connectionSettings.BuildConnectionString()) .Create(); var dataProvider = new PostgresDataProvider( TestHelper.GetLoggerMock <PostgresDataProvider>().Object, new DataConnectionFactory(connectionSettings), MapperConfig.CreateMapper(), null); var watchRequest = new WatchRequest { DeviceId = "device-id", DeviceName = "device-name", Version = "version", CiqVersion = "ciq-version", Framework = "framework", WeatherProvider = "weather-provider", DarkskyKey = "dark-key", Lat = (decimal)1.1, Lon = (decimal)2.2, BaseCurrency = "USD", TargetCurrency = "EUR" }; // Act // await dataProvider.SaveRequestInfo(watchRequest, new WeatherInfo { WindSpeed = (decimal)5.5, Temperature = (decimal)4.4 }, new LocationInfo { CityName = "city-name" }, new ExchangeRateInfo { ExchangeRate = (decimal)3.3 }); // Assert // var actualDevice = await dbConnection.GetTable <DeviceData>().SingleAsync(d => d.DeviceId == watchRequest.DeviceId); var actualRequest = await dbConnection.GetTable <RequestData>().SingleAsync(r => r.DeviceDataId == actualDevice.Id); Assert.Equal("device-name", actualDevice.DeviceName); Assert.Equal("device-id", actualDevice.DeviceId); Assert.Equal(watchRequest.Framework, actualRequest.Framework); Assert.Equal(watchRequest.CiqVersion, actualRequest.CiqVersion); Assert.Equal(watchRequest.Version, actualRequest.Version); Assert.Equal(watchRequest.Lon, actualRequest.Lon); Assert.Equal(watchRequest.Lat, actualRequest.Lat); Assert.Equal(watchRequest.BaseCurrency, actualRequest.BaseCurrency); Assert.Equal(watchRequest.TargetCurrency, actualRequest.TargetCurrency); Assert.Equal((decimal)4.4, actualRequest.Temperature); Assert.Equal("city-name", actualRequest.CityName); Assert.Equal((decimal)3.3, actualRequest.ExchangeRate); }
public void Watch(WatchRequest request) { var req = request.ToProto(); client.Watch(req, new Action <Etcdserverpb.WatchResponse>(p => { })); }
/// <summary> /// 检测Client上下线 /// </summary> /// <returns></returns> private static async Task ClientWatcher() { if (m_client != null) { try { var childs = await m_client.GetRangeAsync("Client/"); foreach (var child in childs.Kvs) { string temp = $"方法 ClientWatcher 开始Watch{child.Key.ToStringUtf8()}"; Console.WriteLine(temp); WatchRequest request = new WatchRequest() { CreateRequest = new WatchCreateRequest() { Key = child.Key } }; m_client.WatchRange(request, (response => { foreach (WatchEvent e1 in response) { if (e1.Type == Event.Types.EventType.Put) { string client = e1.Key.Replace("Client/", ""); string onlineresult = e1.Value; string result = $"方法 ClientWatcher Put 通知:{client}状态是{onlineresult}"; Console.WriteLine(result); AddWatcher(e1.Key); } else if (e1.Type == Event.Types.EventType.Delete) { string client = e1.Key.Replace("Client/", ""); string onlineresult = e1.Value; string result = $"方法 ClientWatcher Delete 通知:{client}Delete"; Console.WriteLine(result); } } }) , null, exceptionAction); } } catch (Exception e) { Console.WriteLine(e); throw; } } }
public void Watch(string key, Action <WatchResponse> action) { WatchRequest request = new WatchRequest() { CreateRequest = new WatchCreateRequest() { Key = ByteString.CopyFromUtf8(key) } }; _etcdClient.Watch(request, action); }
/// <summary> /// Watches the specified key and passes the watch response to the method provided. /// </summary> /// <param name="key">Key to be watched</param> /// <param name="method">Method to which watch response should be passed on</param> public async void Watch(string key, Action <WatchResponse> method, Metadata headers = null) { WatchRequest request = new WatchRequest() { CreateRequest = new WatchCreateRequest() { Key = ByteString.CopyFromUtf8(key) } }; Watch(request, method); }
/// <summary> /// Watches the specified key and passes the minimal watch events data to the methods provided. /// </summary> /// <param name="key">Key to be watched</param> /// <param name="methods">Methods to which minimal watch events data should be passed on</param> public void Watch(string key, Action <WatchEvent[]>[] methods, Metadata headers = null) { WatchRequest request = new WatchRequest() { CreateRequest = new WatchCreateRequest() { Key = ByteString.CopyFromUtf8(key) } }; Watch(request, methods, headers); }
public void Watch(string key) { WatchRequest request = new WatchRequest() { CreateRequest = new WatchCreateRequest() { Key = key } }; Watch(request); }
private async Task WatchCoreAsync(WatchRequest request, Action <WatchResponse> method, Metadata headers = null, Action <Exception> exceptionHandler = null) { var success = false; var retryCount = 0; while (!success) { try { using (AsyncDuplexStreamingCall <WatchRequest, WatchResponse> watcher = _balancer.GetConnection().watchClient.Watch(headers)) { await watcher.RequestStream.WriteAsync(request); await watcher.RequestStream.CompleteAsync(); try { while (await watcher.ResponseStream.MoveNext()) { WatchResponse update = watcher.ResponseStream.Current; method(update); } } catch (Exception ex) { if (exceptionHandler != null) { exceptionHandler(ex); } else { throw ex; } } ; } success = true; } catch (RpcException ex) when(ex.StatusCode == StatusCode.Unavailable) { retryCount++; if (retryCount >= _balancer._numNodes) { throw ex; } } } }
/// <summary> /// Watches the specified key range and passes the minimal watch events data to the methods provided. /// </summary> /// <param name="key">Key to be watched</param> /// <param name="methods">Methods to which minimal watch events data should be passed on</param> public void WatchRange(string path, Action <WatchEvent[]>[] methods, Metadata headers = null) { WatchRequest request = new WatchRequest() { CreateRequest = new WatchCreateRequest() { Key = ByteString.CopyFromUtf8(path), RangeEnd = ByteString.CopyFromUtf8(GetRangeEnd(path)) } }; Watch(request, methods, headers); }
/// <summary> /// Watches the specified key range and passes the watch response to the method provided. /// </summary> /// <param name="key">Key to be watched</param> /// <param name="method">Method to which watch response should be passed on</param> public async void WatchRange(string path, Action <WatchResponse> method, Metadata headers = null) { WatchRequest request = new WatchRequest() { CreateRequest = new WatchCreateRequest() { Key = ByteString.CopyFromUtf8(path), RangeEnd = ByteString.CopyFromUtf8(GetRangeEnd(path)) } }; Watch(request, method); }
public void Watch(string key, Action <WatchResponse> method, CancellationToken cancellationToken, Metadata headers = null, Action <Exception> exceptionHandler = null) { var request = new WatchRequest() { CreateRequest = new WatchCreateRequest() { Key = ByteString.CopyFromUtf8(key) } }; Task.Factory.StartNew(() => WatchAsync(request, method, headers, exceptionHandler), cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default); }
public async Task <EtcdWatcher> Watch(string key) { var watchRequest = new WatchRequest() { CreateRequest = new WatchCreateRequest() { Key = ByteString.CopyFromUtf8(key) } }; var watcher = watchClient.Watch(); await watcher.RequestStream.WriteAsync(watchRequest); return(new EtcdWatcher(watcher)); }
/// <summary> /// Watches a key range according to the specified watch request and /// passes the minimal watch event data to the methods provided. /// </summary> /// <param name="request">Watch Request containing key to be watched</param> /// <param name="methods">Methods to which minimal watch events data should be passed on</param> public async void WatchRange(WatchRequest request, Action <WatchEvent[]>[] methods, Metadata headers = null) { bool success = false; int retryCount = 0; while (!success) { try { using (AsyncDuplexStreamingCall <WatchRequest, WatchResponse> watcher = _balancer.GetConnection().watchClient.Watch(headers)) { Task watcherTask = Task.Run(async() => { while (await watcher.ResponseStream.MoveNext()) { WatchResponse update = watcher.ResponseStream.Current; foreach (Action <WatchEvent[]> method in methods) { method(update.Events.Select(i => { return(new WatchEvent { Key = i.Kv.Key.ToStringUtf8(), Value = i.Kv.Value.ToStringUtf8(), Type = i.Type }); }).ToArray() ); } } }); await watcher.RequestStream.WriteAsync(request); await watcher.RequestStream.CompleteAsync(); await watcherTask; } success = true; } catch (RpcException ex) when(ex.StatusCode == StatusCode.Unavailable) { retryCount++; if (retryCount >= _balancer._numNodes) { throw ex; } } } }
public async Task <EtcdWatcher> WatchRange(string prefixKey) { string rangeEnd = GetRangeEndForPrefix(prefixKey); var watchRequest = new WatchRequest() { CreateRequest = new WatchCreateRequest() { Key = ByteString.CopyFromUtf8(prefixKey), RangeEnd = ByteString.CopyFromUtf8(rangeEnd) } }; var watcher = watchClient.Watch(); await watcher.RequestStream.WriteAsync(watchRequest); return(new EtcdWatcher($"prefix-{prefixKey}", watcher)); }
public async Task <EtcdWatcher> WatchRange(string fromKey, string toKey) { var rangeEnd = GetRangeEndForPrefix(toKey); var watchRequest = new WatchRequest() { CreateRequest = new WatchCreateRequest() { Key = ByteString.CopyFromUtf8(fromKey), RangeEnd = ByteString.CopyFromUtf8(toKey) } }; var watcher = watchClient.Watch(); await watcher.RequestStream.WriteAsync(watchRequest); return(new EtcdWatcher(watcher)); }
private static void AddWatcher(string pathkey) { if (m_client != null) { try { WatchRequest request = new WatchRequest() { CreateRequest = new WatchCreateRequest() { Key = ByteString.CopyFromUtf8(pathkey) } }; string temp = $"方法 AddWatcher 开始Watch{pathkey}"; Console.WriteLine(temp); m_client.WatchRange(request, (response => { foreach (WatchEvent e1 in response) { if (e1.Type == Event.Types.EventType.Put) { string client = e1.Key.Replace("Client/", ""); string onlineresult = e1.Value; string result = $"方法 ClientWatcher Put 通知:{client}状态是{onlineresult}"; Console.WriteLine(result); AddWatcher(e1.Key); } else if (e1.Type == Event.Types.EventType.Delete) { string client = e1.Key.Replace("Client/", ""); string onlineresult = e1.Value; string result = $"方法 ClientWatcher Delete 通知:{client}Delete"; Console.WriteLine(result); } } }) , null, exceptionAction); } catch (Exception e) { Console.WriteLine(e); throw; } } }
/// <summary> /// Watches the specified keys and passes the minimal watch events data to the method provided. /// </summary> /// <param name="keys">Keys to be watched</param> /// <param name="methods">Methods to which minimal watch events data should be passed on</param> public void Watch(string[] keys, Action <WatchEvent[]>[] methods, Metadata headers = null) { List <WatchRequest> requests = new List <WatchRequest>(); foreach (string key in keys) { WatchRequest request = new WatchRequest() { CreateRequest = new WatchCreateRequest() { Key = ByteString.CopyFromUtf8(key) } }; requests.Add(request); } Watch(requests.ToArray(), methods, headers); }
public async Task HandleCallbackQStartNotify(CallbackQuery query, Service service) { var userSettings = await UserSettings(query.From); if (userSettings == null) { return; } if (userSettings.MailNotification) { return; } userSettings.MailNotification = true; await _dbWorker.UpdateUserSettingsRecordAsync(userSettings); if (string.IsNullOrEmpty(BotInitializer.Instance?.BotSettings?.Topic)) { throw new CommandHandlerException($"{nameof(BotInitializer.Instance.BotSettings.Token)} must be not null or empty."); } var watchRequest = new WatchRequest { LabelIds = new List <string> { Label.Unread }, TopicName = BotInitializer.Instance.BotSettings.Topic, LabelFilterAction = "include" }; var request = service.GmailService.Users.Watch(watchRequest, "me"); var watchResponse = await request.ExecuteAsync(); if (watchResponse.Expiration != null) { userSettings.Expiration = watchResponse.Expiration.Value; } if (watchResponse.HistoryId != null) { userSettings.HistoryId = Convert.ToInt64(watchResponse.HistoryId.Value); } await _dbWorker.UpdateUserSettingsRecordAsync(userSettings); await _botActions.NotificationStartedMessage(service.From); }
/// <summary> /// Watches a key according to the specified watch request and /// passes the minimal watch event data to the methods provided. /// </summary> /// <param name="request">Watch Request containing key to be watched</param> /// <param name="methods">Methods to which minimal watch events data should be passed on</param> public async void Watch(WatchRequest request, Action <WatchEvent[]>[] methods) { try { using (AsyncDuplexStreamingCall <WatchRequest, WatchResponse> watcher = _watchClient.Watch()) { Task watcherTask = Task.Run(async() => { while (await watcher.ResponseStream.MoveNext()) { WatchResponse update = watcher.ResponseStream.Current; foreach (Action <WatchEvent[]> method in methods) { method(update.Events.Select(i => { return(new WatchEvent { Key = i.Kv.Key.ToStringUtf8(), Value = i.Kv.Value.ToStringUtf8(), Type = i.Type }); }).ToArray() ); } } }); await watcher.RequestStream.WriteAsync(request); await watcher.RequestStream.CompleteAsync(); await watcherTask; } } catch (RpcException) { // If connection issue, then re-initate the watch ResetConnection(); Watch(request, methods); } catch { throw; } }
public static void watch() { try { WatchRequest body = new WatchRequest() { TopicName = "projects/awolr-213414/topics/awolr", LabelIds = new[] { "INBOX" } }; string userId = "*****@*****.**"; UsersResource.WatchRequest watchRequest = service.Users.Watch(body, userId); WatchResponse test = watchRequest.Execute(); } catch (Exception e) { Console.WriteLine("cannot initiate watch request " + e); } }
/// <summary> /// Watches the specified key and passes the minimal watch events data to the methods provided. /// </summary> /// <param name="key">Key to be watched</param> /// <param name="methods">Methods to which minimal watch events data should be passed on</param> public void Watch(string key, Action <WatchEvent[]>[] methods) { try { WatchRequest request = new WatchRequest() { CreateRequest = new WatchCreateRequest() { Key = ByteString.CopyFromUtf8(key) } }; Watch(request, methods); } catch { throw; } }
/// <summary> /// Watches the specified key range and passes the minimal watch events data to the method provided. /// </summary> /// <param name="keys">Keys to be watched</param> /// <param name="methods">Methods to which minimal watch events data should be passed on</param> public void WatchRange(string[] paths, Action <WatchEvent[]>[] methods, Metadata headers = null) { List <WatchRequest> requests = new List <WatchRequest>(); foreach (string path in paths) { WatchRequest request = new WatchRequest() { CreateRequest = new WatchCreateRequest() { Key = ByteString.CopyFromUtf8(path), RangeEnd = ByteString.CopyFromUtf8(GetRangeEnd(path)) } }; requests.Add(request); } Watch(requests.ToArray(), methods, headers); }