Inheritance: NotificationsDataContext
コード例 #1
0
ファイル: Subscription.cs プロジェクト: maxpavlov/FlexNet
        public void Save()
        {
            using (var context = new DataHandler())
            {
                var existing = context.Subscriptions.Where(x =>
                    x.ContentPath == this.ContentPath &&
                    x.UserPath == this.UserPath);

                var count = existing.Count();
                if (count == 1)
                {
                    // update
                    var item = existing.First();
                    item.Active = this.Active;
                    item.ContentPath = this.ContentPath;
                    item.Frequency = this.Frequency;
                    item.IsActive = this.IsActive;
                    item.UserEmail = this.UserEmail;
                    item.UserId = this.UserId;
                    item.UserName = this.UserName;
                    item.UserPath = this.UserPath;
                    item.Language = this.Language;
                }
                else
                {
                    if (count > 1)
                        context.Subscriptions.DeleteAllOnSubmit(existing);
                    context.Subscriptions.InsertOnSubmit(this);
                }
                context.SubmitChanges();
            }
        }
コード例 #2
0
ファイル: Message.cs プロジェクト: maxpavlov/FlexNet
 internal static IEnumerable<Message> GetAllMessages()
 {
     using (var context = new DataHandler())
     {
         return context.Messages.ToArray();
     }
 }
コード例 #3
0
ファイル: Message.cs プロジェクト: maxpavlov/FlexNet
 internal static void DeleteAllMessages()
 {
     using (var context = new DataHandler())
     {
         context.ExecuteCommand("DELETE FROM [Messaging.Messages]");
     }
 }
コード例 #4
0
 private static void ClearMessages()
 {
     using (var context = new DataHandler())
     {
         context.Messages.DeleteAllOnSubmit(context.Messages);
         context.SubmitChanges();
     }
 }
コード例 #5
0
        private static void InsertMessages(int from, int to)
        {
            using (var context = new DataHandler())
            {
                for (int i = from; i < to; i++)
                {
                    var message = new Message
                    {
                        Address = "[email protected]",
                        Body = i.ToString(),
                        Subject = i.ToString()
                    };

                    context.Messages.InsertOnSubmit(message);
                }

                context.SubmitChanges();
            }
        }
コード例 #6
0
        private static void ProcessQueuedMessages()
        {
                try
                {
                    while (true)
                    {
                        using (var context = new DataHandler())
                        {
                            try
                            {
                                var messages = GetMessagesToSend(context);
                                if (messages.Count() == 0)
                                    break;

                                foreach (var message in messages)
                                {
                                    ProcessMessage(message, context);
                                }
                            }
                            finally
                            {
                                context.SubmitChanges();
                            }
                        }
                        Debug.WriteLine("#Notification> End of iteration");
                    }
                }
                catch (Exception exception)
                {
                    Debug.WriteLine("#Notification> Rootlevel Exception:" + exception.Message);
                    Logger.WriteException(exception);
                }
                finally
                {
                    ProcessQueuedMessagesFinished();
                }
        }
コード例 #7
0
ファイル: Subscription.cs プロジェクト: maxpavlov/FlexNet
 public static void DeleteAllSubscriptions()
 {
     using (var context = new DataHandler())
     {
         context.ExecuteCommand("DELETE FROM [Messaging.Subscriptions]");
     }
 }
コード例 #8
0
ファイル: Subscription.cs プロジェクト: maxpavlov/FlexNet
        public static void UnSubscribeFrom(Node target)
        {
            using (var context = new DataHandler())
            {
                var existing = context.Subscriptions.Where(
                    x => x.ContentPath == target.Path);

                if (existing.Count() > 0)
                {
                    context.Subscriptions.DeleteAllOnSubmit(existing);
                    context.SubmitChanges();
                }
            }
        }
コード例 #9
0
ファイル: Event.cs プロジェクト: maxpavlov/FlexNet
 internal static int GetCountOfEvents()
 {
     using (var context = new DataHandler())
     {
         return context.Events.Count();
     }
 }
コード例 #10
0
ファイル: LastProcessTime.cs プロジェクト: maxpavlov/FlexNet
 internal static void Reset()
 {
     using (var context = new DataHandler())
     {
         context.ExecuteCommand("DELETE FROM [Messaging.LastProcessTime]");
     }
 }
コード例 #11
0
ファイル: Subscription.cs プロジェクト: maxpavlov/FlexNet
 internal static IEnumerable<Subscription> GetActiveSubscriptionsByFrequency(NotificationFrequency frequency)
 {
     using (var context = new DataHandler())
     {
         return context.Subscriptions.Where(x => x.FrequencyId == (int)frequency && x.Active != 0).ToArray();
     }
 }
コード例 #12
0
ファイル: Subscription.cs プロジェクト: maxpavlov/FlexNet
 public static Subscription GetSubscriptionByUser(string subscriberPath, string contentPath, bool isActive)
 {
     using (var context = new DataHandler())
     {
         return context.Subscriptions.Where(x =>
             x.UserPath == subscriberPath &&
             x.ContentPath == contentPath &&
             x.Active == (byte)(isActive ? 1 : 0)).FirstOrDefault();
     }
 }
コード例 #13
0
ファイル: Subscription.cs プロジェクト: maxpavlov/FlexNet
 public static int GetCountOfSubscriptions()
 {
     using (var context = new DataHandler())
     {
         return context.Subscriptions.Count();
     }
 }
コード例 #14
0
        internal static void RefreshLock(double timeframe)
        {
            using (var context = new DataHandler())
            {
                var notificationLock = context.Synchronizations.SingleOrDefault(lockItem => lockItem.LockName == NOTIFICATIONLOCKNAME);
                if (notificationLock == null)
                    throw new InvalidOperationException("Could not update the Notification lock, because it doesn't exist. (the lock must be present when RefreshLock is called.)");

                notificationLock.LockedUntil = DateTime.Now.AddMinutes(timeframe);
                context.SubmitChanges();
            }
            Debug.WriteLine("#Notification> ** lock refreshed");
        }
コード例 #15
0
        //-------------------------------------------------

        //TODO: AcquireLock, RefreshLock, Release lock csak a teszteles miatt internal, private jobb lenne
        internal static bool AcquireLock(double timeframe)
        {
            Synchronization notificationLock;
            
            using (var context = new DataHandler())
            {
                notificationLock = context.Synchronizations.SingleOrDefault(lockItem => lockItem.LockName == NOTIFICATIONLOCKNAME);
                if (notificationLock == null)
                    return InsertLock(timeframe);
            }
            
            if (notificationLock.Locked && (notificationLock.LockId != AppDomainId) && notificationLock.LockedUntil > DateTime.Now)
                return false;

            return UpdateLock(notificationLock, timeframe);
        }
コード例 #16
0
 private static IEnumerable<Message> GetMessagesToSend(DataHandler context)
 {
     //var messages = context.Messages.Take(Configuration.TakeCount);
     //return messages;
     var lockId = Guid.NewGuid().ToString(); //TODO: #notif: LockId (machine+domain+thread?)
     if(SignMessagesToSend(lockId))
         return SelectSignedMessages(context, lockId);
     return Message.EmptyMessages;
 }
コード例 #17
0
        private static void ProcessMessage(Message message, DataHandler context)
        {
            if (String.IsNullOrEmpty(message.Address))
            {
                Debug.WriteLine("#Notification> Message is not sent and removed from the queue. Reason: Address is not provided. ");
                Logger.WriteWarning("Message is not sent. Reason: Address is not provided.");
                context.Messages.DeleteOnSubmit(message);
                return;
            }

            var retryCount = Configuration.RetryCount;
            while (true)
            {
                try
                {
                    SendMessage(message);
                    context.Messages.DeleteOnSubmit(message);
                    break;
                }
                catch (SmtpFailedRecipientException exception)
                {
                    SmtpStatusCode statusCode = exception.StatusCode;

                    if ((--retryCount != 0) &&
                        (statusCode == SmtpStatusCode.MailboxBusy || statusCode == SmtpStatusCode.MailboxUnavailable || statusCode == SmtpStatusCode.TransactionFailed))
                    {
                        Debug.WriteLine("#Notification SmtpFailedRecipientException RETRY> " + exception.Message + exception.StatusCode);
                        Logger.WriteException(exception);
                        Thread.Sleep(Configuration.RetryDelay);
                    }
                    else
                    {
                        context.Messages.DeleteOnSubmit(message);
                        Debug.WriteLine("#Notification SmtpFailedRecipientException> " + exception.Message + exception.StatusCode);
                        Debug.WriteLine("#Notification SmtpFailedRecipientException> Message is removed from the queue.");
                        Logger.WriteException(exception);
                        Logger.WriteWarning("Message is removed from queue.");
                        break;
                    }
                }
            }
        }
コード例 #18
0
 internal static IEnumerable<Message> SelectSignedMessages(DataHandler context, string lockId)
 {
     return context.Messages.Where(m => m.LockId == lockId);
 }
コード例 #19
0
ファイル: Subscription.cs プロジェクト: maxpavlov/FlexNet
 private static void SetActivation(User subscriber, Node target, bool value)
 {
     using (var context = new DataHandler())
     {
         var subscription = context.Subscriptions.Where(x =>
             x.UserPath == subscriber.Path &&
             x.ContentPath == target.Path &&
             x.Active == (byte)(value ? 0 : 1)).FirstOrDefault();
         if (subscription == null)
             return;
         subscription.IsActive = value;
         context.SubmitChanges();
     }
 }
コード例 #20
0
        internal static void ReleaseLock()
        {
            using (var context = new DataHandler())
            {
                var notificationLock = context.Synchronizations.SingleOrDefault(lockItem => lockItem.LockName == NOTIFICATIONLOCKNAME);
                if (notificationLock == null)
                    throw new Exception(); //TODO: #notifB: ?? what is this?

                notificationLock.Locked = false;
                context.SubmitChanges();
            }
            Debug.WriteLine("#Notification> ** lock released");
        }
コード例 #21
0
ファイル: Subscription.cs プロジェクト: maxpavlov/FlexNet
 public static IEnumerable<Subscription> GetAllSubscriptions()
 {
     using (var context = new DataHandler())
     {
         return context.Subscriptions.ToArray();
     }
 }
コード例 #22
0
        private static bool InsertLock(double timeframe)
        {
            using (var context = new DataHandler())
            {
                var success = false;
                var notifLock = new Synchronization
                                    {
                                        LockName = NOTIFICATIONLOCKNAME,
                                        Locked = true,
                                        ComputerName = Environment.MachineName,
                                        LockedUntil =
                                            DateTime.Now.AddMinutes(timeframe),
                                        LockId = AppDomainId
                                    };
                try
                {
                    context.Synchronizations.InsertOnSubmit(notifLock);
                    context.SubmitChanges();
                    success = true;
                }
                catch (Exception ex)
                {
                    Logger.WriteException(ex);
                }

                return success;
            }
        }
コード例 #23
0
ファイル: Subscription.cs プロジェクト: maxpavlov/FlexNet
 internal static IEnumerable<Subscription> GetSubscriptionsByUser(string subscriberPath, bool isActive)
 {
     using (var context = new DataHandler())
     {
         return context.Subscriptions.Where(
             x => x.UserPath == subscriberPath && x.Active == (byte)(isActive ? 1 : 0))
             .ToArray();
     }
 }
コード例 #24
0
ファイル: Event.cs プロジェクト: maxpavlov/FlexNet
 internal static void DeleteOldEvents(DateTime now)
 {
     using (var context = new DataHandler())
     {
         context.ExecuteCommand(String.Format("DELETE FROM [Messaging.Events] WHERE [When] < '{0}'", now.AddMonths(-2).ToString("yyyy-MM-dd HH:mm:ss")));
     }
 }
コード例 #25
0
ファイル: Subscription.cs プロジェクト: maxpavlov/FlexNet
 public static IEnumerable<Subscription> GetSubscriptionsByContent(string contentPath)
 {
     using (var context = new DataHandler())
     {
         return context.Subscriptions.Where(x => x.ContentPath == contentPath).ToArray();
     }
 }
コード例 #26
0
        private static void GenerateMessages(IEnumerable<Subscription> subscriptions)
        {
            var configs = new Dictionary<string, NotificationConfig>();

            using (var context = new DataHandler())
            {
                foreach (var subscription in subscriptions)
                {
                    // gather notification configs (handler: NotificationConfig.cs) for all related content of subscriptions
                    GatherConfigsForSubscription(subscription, configs);

                    // generate messges for content whose notification are controlled by a notification config
                    var msgs = GenerateMessagesForConfig(subscription, configs);
                    foreach (var message in msgs)
                    {
                        context.Messages.InsertOnSubmit(message);
                    }

                    // generate messages for content whose notification are NOT controlled by a notification config (send a generic notification message)
                    var msg = GenerateMessage(subscription, configs);
                    if (msg != null)
                        context.Messages.InsertOnSubmit(msg);
                }
                context.SubmitChanges();
            }
        }
コード例 #27
0
ファイル: Event.cs プロジェクト: maxpavlov/FlexNet
 private void Save()
 {
     using (var context = new DataHandler())
     {
         context.Events.InsertOnSubmit(this);
         context.SubmitChanges();
     }
 }
コード例 #28
0
ファイル: Subscription.cs プロジェクト: maxpavlov/FlexNet
        public static void UnSubscribeAll(User subscriber)
        {
            using (var context = new DataHandler())
            {
                var existing = context.Subscriptions.Where(
                    x => x.UserPath == subscriber.Path);

                if (existing.Count() > 0)
                {
                    context.Subscriptions.DeleteAllOnSubmit(existing);
                    context.SubmitChanges();
                }
            }
        }
コード例 #29
0
        private static IEnumerable<Subscription> CollectEventsPerSubscription(NotificationFrequency freq, DateTime now)
        {
            var lastTime = now;
            var subscriptions = Subscription.GetActiveSubscriptionsByFrequency(freq);
            if (subscriptions.Count() == 0)
                return null;
            //var time = GetTimeLimit(freq);
            var time = LastProcessTime.GetLastProcessTime(freq);

            using (var context = new DataHandler())
            {
                var events = (time == DateTime.MinValue)?
                    context.Events.Where(x => x.When <= lastTime) :
                    context.Events.Where(x => x.When > time && x.When <= lastTime);
                foreach (var @event in events.OrderBy(x => x.When))
                    foreach (var subscription in subscriptions)
                        if (IsRelatedPath(@event.ContentPath, subscription.ContentPath))
                            if (HasPermission(subscription, @event))
                                subscription.AddRelatedEvent(@event);
            }
            LastProcessTime.SetLastProcessTime(freq, lastTime);
            return subscriptions;
        }
コード例 #30
0
ファイル: Event.cs プロジェクト: maxpavlov/FlexNet
 internal static IEnumerable<Event> GetAllEvents()
 {
     using (var context = new DataHandler())
     {
         return context.Events.ToArray();
     }
 }