コード例 #1
0
        /// <summary>
        /// dispatch a notification
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="store"></param>
        private void Dispatch(Notification notification,IDocumentStore store)
        {
            _log.Information(string.Format("About to dispatch a notification {0} from store {1}", notification.ToString(), store.Url));
            var dispatched = false;
            //a notification can have multiple recipients e.g. send notification to employee and manager
            foreach (var recipient in notification.NotificationRecipients)
            {
                bool emailDispatched;
                if (recipient.RemainingNotificationDeliveryTypes.Has(NotificationDeliveryTypes.Email))
                {
                    emailDispatched = _emailDispatcher.Dispatch(notification, recipient.Users, store);
                    if (emailDispatched)
                    {
                        recipient.RemainingNotificationDeliveryTypes = recipient.RemainingNotificationDeliveryTypes.Remove(NotificationDeliveryTypes.Email);
                        _log.Information(string.Format("dispatched an email notification to {0}", string.Join(",", recipient.Users.Select(u => u.EmailAddress))));
                    }
                    else
                    {
                        _log.Warning(string.Format("could not dispatch an email notification to {0}", string.Join(",", recipient.Users.Select(u => u.EmailAddress))));
                    }
                        
                }
                else
                {
                    emailDispatched = true;
                }

                bool toastDispatched;
                if (recipient.RemainingNotificationDeliveryTypes.Has(NotificationDeliveryTypes.Toast))
                {
                    toastDispatched = _toastDispatcher.Dispatch(notification,store);
                    if (toastDispatched)
                    {
                        recipient.RemainingNotificationDeliveryTypes = recipient.RemainingNotificationDeliveryTypes.Remove(NotificationDeliveryTypes.Toast);
                        _log.Information(string.Format("dispatched an toast notification to {0}", string.Join(",", recipient.Users.Select(u => u.FullName))));
                    }
                    else
                    {
                        _log.Warning(string.Format("could not dispatch a toast notification to {0}", string.Join(",", recipient.Users.Select(u => u.FullName))));
                    }
                }
                else
                {
                    toastDispatched = true;
                }

                bool smsDispatched;
                if (recipient.RemainingNotificationDeliveryTypes.Has(NotificationDeliveryTypes.Sms))
                {
                    smsDispatched = _smsDispatcher.Dispatch(notification,store);
                    if (smsDispatched)
                    {
                        recipient.RemainingNotificationDeliveryTypes = recipient.RemainingNotificationDeliveryTypes.Remove(NotificationDeliveryTypes.Sms);
                        _log.Information(string.Format("dispatched an SMS notification to {0}", string.Join(",", recipient.Users.Select(u => u.MobilePhoneNumber))));
                    }
                    else
                    {
                        _log.Warning(string.Format("could not dispatch an SMS notification to {0}", string.Join(",", recipient.Users.Select(u => u.MobilePhoneNumber))));
                    }
                }
                else
                {
                    smsDispatched = true;
                }
                dispatched = emailDispatched && smsDispatched && toastDispatched;

            }
            

           
            //update the notification to say its been dealt with
            if (dispatched)
            {
                notification.Sent = true;
                notification.ActualSentDate = DateTime.Now;
                _log.Information(string.Format("sucessfully dispatched a notification {0}", notification));
            }
            else
            {
                notification.NumDeliveryAttempts--;
                notification.SendDate = DateTime.Now.AddSeconds(_numSecondsBackoff);
                _log.Warning(string.Format("problem dispatching a notification {0} - backing off {1} more attempts left", notification,notification.NumDeliveryAttempts));
            }
            using (var session= store.OpenSession())
            {
                 session.Store(notification);
                 session.SaveChanges();
            }
           
        }