Exemple #1
0
        public ServiceResult RemoveMessage(int id)
        {
            return(RunAction(new UserContext(), null, () =>
            {
                Throws.IfArgumentNull(id, _ => id);
                var ctx = NotificationsModelDataContext.Get(_provider);
                var m = ctx.Messages.FirstOrDefault(x => x.Id == id);

                if (m != null)
                {
                    ctx.Messages.Remove(m);
                    ctx.SaveChanges();
                }
            }));
        }
Exemple #2
0
        public ServiceResult <List <Message> > GetMessagesToSend(string channel, int maxCount)
        {
            return(RunEnumeration <Message>(new UserContext(), null, () =>
            {
                Throws.IfArgumentNull(channel, _ => channel);
                Throws.IfArgumentNull(maxCount, _ => maxCount);

                var ctx = NotificationsModelDataContext.Get(_provider);

                return ctx.Messages
                .Where(x => x.Channel == channel &&
                       x.Created <=
                       DateTime.Now.AddSeconds(-10))          //чтобы не читать данные которые сейчас пишут
                .OrderBy(x => x.Created)
                .Take(maxCount)
                .ToList()
                .Select(x => new Message
                {
                    Channel = x.Channel, Created = x.Created, Id = x.Id, Method = x.Method,
                    Xml = x.Data, UserId = x.UserId, UserName = x.UserName, Key = x.DataKey
                }).ToList();
            }));
        }
Exemple #3
0
        public void PushNotifications(NotificationItem[] notifications, bool isStage, int userId, string userName, string method, string customerCode)
        {
            _identityProvider.Identity = new Identity(customerCode);

            RunAction(new UserContext(), null, () =>
            {
                Throws.IfArgumentNull(notifications, _ => notifications);

                List <NotificationChannel> channels = null;
                var provider = ObjectFactoryBase.Resolve <INotificationProvider>();
                if (notifications.Any(n => n.Channels == null))
                {
                    channels = provider.GetConfiguration().Channels;

                    if (channels == null)
                    {
                        var productIds = notifications.Where(n => n.Channels == null).Select(n => n.ProductId);
                        _logger.Error(
                            "Products {0} have not been enqueued because there are no channels for publishing",
                            string.Join(", ", string.Join(", ", productIds))
                            );
                        throw new Exception("No channels for publishing");
                    }
                }

                var messages = new List <DAL.Message>();

                foreach (var notification in notifications)
                {
                    if (notification.Channels == null)
                    {
                        foreach (var channel in channels.Where(c => c.IsStage == isStage))
                        {
                            if (channel.XPathFilterExpression == null || DocumentMatchesFilter(notification.Data, channel.XPathFilterExpression))
                            {
                                messages.Add(new DAL.Message
                                {
                                    Created  = DateTime.Now,
                                    Channel  = channel.Name,
                                    Data     = notification.Data,
                                    DataKey  = notification.ProductId,
                                    Method   = method,
                                    UserId   = userId,
                                    UserName = userName
                                });
                            }
                        }
                    }
                    else
                    {
                        foreach (var channel in notification.Channels)
                        {
                            messages.Add(new DAL.Message
                            {
                                Created  = DateTime.Now,
                                Channel  = channel,
                                Data     = notification.Data,
                                DataKey  = notification.ProductId,
                                Method   = method,
                                UserId   = userId,
                                UserName = userName
                            });
                        }
                    }
                }

                if (messages.Any())
                {
                    var ctx = NotificationsModelDataContext.Get(_connectionProvider);
                    ctx.Messages.AddRange(messages);

                    var productIds = notifications.Select(n => n.ProductId).Distinct();
                    _logger.Info()
                    .Message("Message {message} for products {productIds} is enqueueing",
                             method, string.Join(", ", productIds)
                             )
                    .Property("isStage", isStage)
                    .Write();

                    ctx.SaveChanges();
                }
            });
        }
Exemple #4
0
        public ConfigurationInfo GetConfigurationInfo(string customerCode)
        {
            _identityProvider.Identity = new Identity(customerCode);
            var provider             = ObjectFactoryBase.Resolve <INotificationProvider>();
            var channelService       = ObjectFactoryBase.Resolve <INotificationChannelService>();
            var currentConfiguration = GetCurrentConfiguration(customerCode);
            var actualConfiguration  = provider.GetConfiguration();

            var channels = actualConfiguration.Channels.Select(c => c.Name)
                           .Union(currentConfiguration == null ? new string[0] : currentConfiguration.Channels.Select(c => c.Name))
                           .Distinct();

            var ctx      = NotificationsModelDataContext.Get(_connectionProvider);
            var countMap = ctx.Messages
                           .GroupBy(m => m.Channel)
                           .Select(g => new { g.Key, Count = g.Count() })
                           .ToDictionary(g => g.Key, g => g.Count);

            var channelsStatistic = channelService.GetNotificationChannels(customerCode);

            return(new ConfigurationInfo
            {
                Started = NotificationSender.Started,
                NotificationProvider = provider.GetType().Name,
                IsActual = actualConfiguration.IsEqualTo(currentConfiguration),
                ActualSettings = new SettingsInfo
                {
                    Autopublish = actualConfiguration.Autopublish,
                    CheckInterval = actualConfiguration.CheckInterval,
                    ErrorCountBeforeWait = actualConfiguration.ErrorCountBeforeWait,
                    PackageSize = actualConfiguration.PackageSize,
                    TimeOut = actualConfiguration.TimeOut,
                    WaitIntervalAfterErrors = actualConfiguration.WaitIntervalAfterErrors
                },
                CurrentSettings = currentConfiguration == null ?
                                  new SettingsInfo() :
                                  new SettingsInfo
                {
                    Autopublish = currentConfiguration.Autopublish,
                    CheckInterval = currentConfiguration.CheckInterval,
                    ErrorCountBeforeWait = currentConfiguration.ErrorCountBeforeWait,
                    PackageSize = currentConfiguration.PackageSize,
                    TimeOut = currentConfiguration.TimeOut,
                    WaitIntervalAfterErrors = currentConfiguration.WaitIntervalAfterErrors
                },
                Channels = (from channel in channels
                            join s in channelsStatistic on channel equals s.Name into d
                            from s in d.DefaultIfEmpty()
                            select new ChannelInfo
                {
                    Name = channel,
                    State = GetState(actualConfiguration.Channels.FirstOrDefault(c => c.Name == channel), currentConfiguration?.Channels.FirstOrDefault(c => c.Name == channel)),
                    Count = countMap.ContainsKey(channel) ? countMap[channel] : 0,
                    LastId = s?.LastId,
                    LastQueued = s?.LastQueued,
                    LastPublished = s?.LastPublished,
                    LastStatus = s?.LastStatus
                })
                           .ToArray()
                           .ToArray()
            });
        }