protected override async Task RunAsync(CancellationToken cancellationToken) { Logger.Debug(nameof(this.RunAsync)); try { INotificationActor notificationActor = ConnectionFactory.CreateNotificationActor(); await notificationActor.SubscribeAsync(new StockEventsHandler(this.StateManager, ProductsCollectionName)); await base.RunAsync(cancellationToken); } catch (Exception ex) { Logger.Error(ex, nameof(this.RunAsync)); throw; } }
private async Task RunNotificationAsync(object arg) { try { Dictionary <int, ProductStockTrend> productTrends = await this.StateManager.GetStateAsync <Dictionary <int, ProductStockTrend> >("ProductStockTrends"); // select only products to be notified of IEnumerable <KeyValuePair <int, ProductStockTrend> > list = productTrends .Where(x => x.Value.NotificationCounter > 0); int count = list.Count(); if (count > 0) { Logger.Debug("RunNotificationAsync: notifying on {0} products", count); INotificationActor notificationActor = ActorProxy.Create <INotificationActor>(new ActorId(0)); await notificationActor.NotifyLowStockProducts( list.Select(x => x.Value.ToProductPrediction()).ToList() ); // decrement the counter of notification attempts foreach (int key in list.Select(x => x.Key)) { productTrends[key].NotificationCounter--; } await this.StateManager.SetStateAsync <Dictionary <int, ProductStockTrend> >("ProductStockTrends", productTrends); } } catch (Exception ex) { Logger.Error(ex, nameof(this.RunNotificationAsync)); } }