예제 #1
0
        /// <inheritdoc />
        public async Task NotifyAsync(INotificationMetadata notificationMetadata, CancellationToken cancellationToken = default)
        {
            if (notificationMetadata == null)
            {
                throw new ArgumentNullException(nameof(notificationMetadata));
            }

            var metadataType = notificationMetadata.GetType();

            if (!_buildersPerType.TryGetValue(metadataType, out var builders))
            {
                throw new InvalidOperationException($"Notification builder for notification {metadataType} not found");
            }

            // build notification for all channels
            var notificationsMap = new Dictionary <string, List <INotification> >();

            foreach (var(channelType, builder) in builders)
            {
                if (!_channels.ContainsKey(channelType))
                {
                    throw new InvalidOperationException($"No channels for type {channelType}. Notification would not be build");
                }

                var notifications = await builder.BuildNotificationsAsync(notificationMetadata, cancellationToken);

                if (notifications.Count == 0)
                {
                    _logger.LogWarning($"No notification have been built for {metadataType} for channel {channelType}");
                    continue;
                }

                if (!notificationsMap.ContainsKey(channelType))
                {
                    notificationsMap[channelType] = new List <INotification>();
                }

                for (var i = 0; i < notifications.Count; i++)
                {
                    var notification = notifications[i];
                    if (notification != null)
                    {
                        notificationsMap[channelType].Add(notification);
                    }
                }
            }

            // create sending task for each notification
            var sendingTasks = new List <Task>(notificationsMap.Count);

            foreach (var(channelType, notifications) in notificationsMap)
            {
                var channel = _channels[channelType];
                for (var i = 0; i < notifications.Count; i++)
                {
                    var notification = notifications[i];
                    sendingTasks.Add(channel.SendNotificationAsync(notification, cancellationToken));
                }
            }

            // await all
            await Task.WhenAll(sendingTasks);
        }
예제 #2
0
        /// <inheritdoc />
        public void NotifyAndForgot(INotificationMetadata notificationMetadata)
        {
            if (notificationMetadata == null)
            {
                throw new ArgumentNullException(nameof(notificationMetadata));
            }

            NotifyAsync(notificationMetadata).ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    _logger.LogError(task.Exception, $"Notification task faulted (NotificationType=\"{notificationMetadata.GetType()}\")");
                }

                if (task.IsCanceled)
                {
                    _logger.LogError($"Notification task was canceled (NotificationType=\"{notificationMetadata.GetType()}\")");
                }
            });
        }