Example #1
0
        private void MatchDataChangeEvents(Data.EventBrokerDataContext dataContext, Data.DataChangeEvent[] dataChangeEvents, DateTime now)
        {
            var criteriaSubscriptions = dataContext.Subscriptions.Where(sub => sub.Criteria != null).ToArray();

            foreach (var subscription in criteriaSubscriptions)
            {
                subscription.GetDataChangeEventMatches(dataChangeEvents);
            }
            dataContext.SubmitChanges();
        }
Example #2
0
        public void PushNotifications()
        {
            var batchSize = CprBroker.Config.Properties.Settings.Default.DataChangeDequeueBatchSize;

            Admin.LogFormattedSuccess("DataChangeEventEnqueuer.PushNotifications() started, batch size <{0}>", batchSize);
            bool moreChangesExist = true;

            while (moreChangesExist)
            {
                var resp          = EventsService.DequeueDataChangeEvents(batchSize);
                var changedPeople = resp.Item;
                if (changedPeople == null)
                {
                    changedPeople = new EventBroker.EventsService.DataChangeEventInfo[0];
                }
                moreChangesExist = changedPeople.Length == batchSize;

                Admin.LogFormattedSuccess("DataChangeEventEnqueuer.PushNotifications(): <{0}> data changes found", changedPeople.Length);

                using (var dataContext = new Data.EventBrokerDataContext())
                {
                    var dbObjects = Array.ConvertAll <EventsService.DataChangeEventInfo, Data.DataChangeEvent>(
                        changedPeople,
                        p => new Data.DataChangeEvent()
                    {
                        DataChangeEventId    = p.EventId,
                        DueDate              = p.ReceivedDate,
                        PersonUuid           = p.PersonUuid,
                        PersonRegistrationId = p.PersonRegistrationId,
                        ReceivedDate         = DateTime.Now
                    }
                        );

                    dataContext.DataChangeEvents.InsertAllOnSubmit(dbObjects);
                    dataContext.SubmitChanges();

                    DateTime now = DateTime.Now;
                    MatchDataChangeEvents(dataContext, dbObjects, now);

                    dataContext.UpdatePersonLists(now, (int)Data.SubscriptionType.SubscriptionTypes.DataChange);
                    dataContext.EnqueueDataChangeEventNotifications(now, (int)Data.SubscriptionType.SubscriptionTypes.DataChange);

                    //TODO: Move this logic to above stored procedure
                    dataContext.DataChangeEvents.DeleteAllOnSubmit(dbObjects);
                    dataContext.SubmitChanges();
                }
            }
        }
Example #3
0
        private void SynchronisePersonBirthdates()
        {
            bool morePersons    = true;
            Guid?lastPersonGuid = null;

            while (morePersons)
            {
                var resp             = EventsService.GetPersonBirthdates(lastPersonGuid, BatchSize);
                var personBirthdates = resp.Item;

                if (personBirthdates.Length > 0)
                {
                    lastPersonGuid = personBirthdates[personBirthdates.Length - 1].PersonUuid;
                }
                morePersons = personBirthdates.Length == BatchSize;

                using (var dataContext = new Data.EventBrokerDataContext())
                {
                    var personswithoutBirthdates = personBirthdates.Where(p => !p.Birthdate.HasValue).ToArray();
                    if (personswithoutBirthdates.Length > 0)
                    {
                        // TODO: Shall this be a critical error?
                        CprBroker.Engine.Local.Admin.LogFormattedError("Unable to get birthdates for <{0}> persons, first UUID <{1}>, last UUID <{2}>", personswithoutBirthdates.Length, personswithoutBirthdates.First().PersonUuid, personswithoutBirthdates.Last().PersonUuid);
                    }
                    var newPersons =
                        (
                            from pb in personBirthdates
                            where
                            pb.Birthdate.HasValue &&
                            !(from dpb in dataContext.PersonBirthdates select dpb.PersonUuid).Contains(pb.PersonUuid)

                            select new Data.PersonBirthdate()
                    {
                        PersonUuid = pb.PersonUuid,
                        Birthdate = pb.Birthdate.Value
                    }
                        );

                    dataContext.PersonBirthdates.InsertAllOnSubmit(newPersons);
                    dataContext.SubmitChanges();
                }
            }
        }
Example #4
0
        private void EnqueueBirthdateEvents()
        {
            using (var dataContext = new Data.EventBrokerDataContext())
            {
                DateTime today = DateTime.Today;

                foreach (var subscription in dataContext.Subscriptions)
                {
                    try
                    {
                        dataContext.EnqueueBirthdateEventNotifications(subscription.SubscriptionId, today);
                    }
                    catch (Exception ex)
                    {
                        string message = string.Format("Failed to enqueue birthdate notifications for {0}", subscription.SubscriptionId);
                        CprBroker.Engine.Local.Admin.LogException(ex, message);
                    }
                }
            }
        }
Example #5
0
        public override StandardReturType ValidateInput()
        {
            if (SubscriptionId == Guid.Empty)
            {
                return(StandardReturType.NullInput());
            }

            using (var dataContext = new Data.EventBrokerDataContext())
            {
                var subscription = (from sub in dataContext.Subscriptions
                                    where sub.SubscriptionId == SubscriptionId && sub.SubscriptionTypeId == (int)this.SubscriptionType
                                    select sub
                                    ).SingleOrDefault();

                if (subscription == null)
                {
                    return(StandardReturType.InvalidUuid(SubscriptionId.ToString()));
                }
            }
            return(StandardReturType.OK());
        }
        protected override void PerformTimerAction()
        {
            using (var dataContext = new Data.EventBrokerDataContext())
            {
                int batchSize = CprBroker.Config.Properties.Settings.Default.EventBrokerNotificationBatchSize;
                Data.EventNotification[] dueNotifications = new CprBroker.EventBroker.Data.EventNotification[0];
                do
                {
                    dueNotifications =
                        (
                            from eventNotification in dataContext.EventNotifications
                            where eventNotification.Succeeded == null
                            orderby eventNotification.CreatedDate
                            select eventNotification
                        ).Take(batchSize).ToArray();


                    foreach (var eventNotification in dueNotifications)
                    {
                        eventNotification.NotificationDate = DateTime.Now;
                        try
                        {
                            Channel channel = Channel.Create(eventNotification.Subscription.Channels.Single());
                            channel.Notify(eventNotification);
                            eventNotification.Succeeded = true;
                        }
                        catch (Exception ex)
                        {
                            string message = string.Format("Notification {0} failed", eventNotification.EventNotificationId);
                            //TODO: use LogNotificationFailure after simplifying its parameters
                            CprBroker.Engine.Local.Admin.LogException(ex, message);
                            eventNotification.Succeeded = false;
                        }
                    }
                    dataContext.SubmitChanges();
                } while (dueNotifications.Length == batchSize);
            }
        }
Example #7
0
        public static void UpdateSubscriptionCriteriaLists()
        {
            int batchSize = CprBroker.Config.Properties.Settings.Default.SubscriptionCriteriaMatchingBatchSize;

            Admin.LogFormattedSuccess("DataChangeEventEnqueuer.UpdateSubscriptionCriteriaLists() started, batch size <{0}>", batchSize);

            using (var eventDataContext = new Data.EventBrokerDataContext())
            {
                var subscriptions = eventDataContext.Subscriptions.Where(s => s.Deactivated == null && s.LastCheckedUUID != null).OrderBy(s => s.Created).ToArray();
                Admin.LogFormattedSuccess("Found <{0}> pending criteria subscriptions", subscriptions.Length);
                foreach (var sub in subscriptions)
                {
                    while (sub.LastCheckedUUID.HasValue)
                    {
                        // TODO: Merge this log entry with the one at the end
                        Admin.LogFormattedSuccess("Adding persons to subscription <{0}>, start UUID <{1}>, batch size <{2}>", sub.SubscriptionId, sub.LastCheckedUUID.Value, batchSize);
                        var added = sub.AddMatchingSubscriptionPersons(eventDataContext, batchSize);
                        eventDataContext.SubmitChanges();
                        Admin.LogFormattedSuccess("Added <{0}> persons to subscription <{1}>, next start UUID <{2}>", added, sub.SubscriptionId, sub.LastCheckedUUID);
                    }
                }
            }
            Admin.LogFormattedSuccess("DataChangeEventEnqueuer.UpdateSubscriptionCriteriaLists() finished");
        }