public async Task InitializeAsync(GatewayInfo gatewayInfo, TimeSpan leaseDuration, CancellationToken cancellationToken) { //Add GatewayInfo in RD with key as GatewayID var stateDictionary = await GetGatewayInformationDictionaryAsync().ConfigureAwait(false); using (var tx = StateManager.CreateTransaction()) { await stateDictionary.AddOrUpdateAsync(tx, gatewayInfo.Id.InstanceId, gatewayInfo, (k, v) => gatewayInfo); await tx.CommitAsync(); } //update after gateway info store is updated. EnsureServiceIsReady(); var currentVal = this.gatewayMemoryInfoStore.GetOrAdd(gatewayInfo.Id.InstanceId, (key) => { var info = new GatewayMemoryInfo(0, 0); return(info); }); currentVal.resetEvent.TrySetResult(true); }
private async Task FillUpdateKeyInfoAsync(string gatewayId, CancellationToken cancellationToken = default(CancellationToken)) { ServiceEventSource.Current.ServiceMessage(this.Context, "FillUpdateKeyInfoAsync for gateway {0}", gatewayId); var myDictionary = await this.GetDeviceDictionaryAsync(gatewayId); bool firstKey = true; long startKey = 0; long lastKey = 0; using (var tx = base.StateManager.CreateTransaction()) { var enumerable = await myDictionary.CreateKeyEnumerableAsync(tx).ConfigureAwait(false); var asyncEnumerable = enumerable.GetAsyncEnumerator(); while (await asyncEnumerable.MoveNextAsync(cancellationToken)) { if (firstKey) { startKey = asyncEnumerable.Current - 1; lastKey = asyncEnumerable.Current; firstKey = false; } else { lastKey = asyncEnumerable.Current; } } var currentVal = this.gatewayMemoryInfoStore.GetOrAdd(gatewayId, (key) => { var info = new GatewayMemoryInfo(lastKey, startKey); return(info); }); currentVal.resetEvent.TrySetResult(true); ServiceEventSource.Current.ServiceMessage(this.Context, "FillUpdateKeyInfoAsync completed for gateway {0} with First and LastKey {1} {2}", gatewayId, startKey, lastKey); } }