Exemplo n.º 1
0
        public override void DoJob()
        {
            using (var session = SharpArch.NHibernate.NHibernateSession.GetDefaultSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    UserRepository         userRepository            = new UserRepository(session);
                    SiteResponseRepository siteResponseRepository    = new SiteResponseRepository(session);
                    IList <User>           usersWithPendingSnapshots = userRepository.FindWithPendingSnapshots();//.AsQueryable()
                    //.Include("Site")
                    //.Where(x => (x.LastSnapshotDate == null || EntityFunctions.AddDays(x.LastSnapshotDate, x.SnapshotInterval) <=    DateTime.Now) && x.Site.Count(s => s.Enabled) > 0);

                    foreach (var user in usersWithPendingSnapshots)
                    {
                        Logger.LogInfo("Sending snapshot...");
                        new EmailSender().SendSnapshotEmail(user, BuildSnapshotNotification(siteResponseRepository, user, user.Sites.Where(x => x.Enabled)));
                        user.LastSnapshotDate = DateTime.Now;
                        userRepository.SaveOrUpdate(user);
                        //UnitOfWork.Commit();
                    }

                    transaction.Commit();
                }
            }
        }
Exemplo n.º 2
0
        private void btnCheckNow_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (var session = SharpArch.NHibernate.NHibernateSession.GetDefaultSessionFactory().OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        SiteRepository          siteRepository          = new SiteRepository(session);
                        SiteResponseRepository  siteResponseRepository  = new SiteResponseRepository(session);
                        SiteErrorRepository     siteErrorRepository     = new SiteErrorRepository(session);
                        NotificationsRepository notificationsRepository = new NotificationsRepository(session);

                        Site         siteToUpdate = ((FrameworkElement)sender).DataContext as Site;
                        int          id           = siteToUpdate.Id;
                        Site         site         = siteRepository.Get(id);
                        SiteResponse lastResponse = siteResponseRepository.GetLastResponse(site.Id);

                        ResponseChecker checker = new ResponseChecker(ApplicationLogic.Utils.Logger.Instance, siteRepository, siteResponseRepository, siteErrorRepository, notificationsRepository);
                        checker.CheckResponse(site, lastResponse);
                        transaction.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                ApplicationLogic.Utils.Logger.Instance.LogError(ex);
            }

            UpdateDataGrid();
        }
Exemplo n.º 3
0
 public ResponseChecker(ILogger logger, SiteRepository siteRepository, SiteResponseRepository siteResponseRepository, SiteErrorRepository siteErrorRepository, NotificationsRepository notificationsRepository)
 {
     _logger                  = logger;
     _siteRepository          = siteRepository;
     _siteResponseRepository  = siteResponseRepository;
     _siteErrorRepository     = siteErrorRepository;
     _notificationsRepository = notificationsRepository;
 }
Exemplo n.º 4
0
        public override void DoJob()
        {
            using (var session = SharpArch.NHibernate.NHibernateSession.GetDefaultSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    SiteRepository          siteRepository          = new SiteRepository(session);
                    SiteResponseRepository  siteResponseRepository  = new SiteResponseRepository(session);
                    SiteErrorRepository     siteErrorRepository     = new SiteErrorRepository(session);
                    NotificationsRepository notificationsRepository = new NotificationsRepository(session);

                    List <Action> actions = new List <Action>();

                    foreach (var site in siteRepository.FindAllEnabled())
                    {
                        SiteResponse lastResponse = siteResponseRepository.GetLastResponse(site.Id);//.AsQueryable().Where(x => x.Site.Id == site.Id).OrderByDescending(x => x.CheckDate).FirstOrDefault();

                        if (lastResponse == null || lastResponse.CheckDate.AddMinutes(site.CheckInterval) < DateTime.Now)
                        {
                            ResponseCheckerContext responseContext = new ResponseCheckerContext()
                            {
                                LastResponse            = lastResponse,
                                Site                    = site,
                                SiteRepository          = siteRepository,
                                SiteResponseRepository  = siteResponseRepository,
                                SiteErrorRepository     = siteErrorRepository,
                                NotificationsRepository = notificationsRepository
                            };

                            actions.Add(() => CheckResponse(responseContext));
                        }
                    }

                    if (Assync)
                    {
                        Parallel.Invoke(actions.ToArray());
                    }
                    else
                    {
                        foreach (var action in actions)
                        {
                            action.Invoke();
                        }
                    }

                    transaction.Commit();
                }
            }
        }
Exemplo n.º 5
0
        private static string BuildSnapshotNotification(SiteResponseRepository siteResponseRepository, User user, IEnumerable <Site> sites)
        {
            StringBuilder sb = new StringBuilder();

            foreach (var site in sites)
            {
                string snapshotContents = null;

                var lastResponse = siteResponseRepository.GetLastResponse(site.Id);

                if (lastResponse != null)
                {
                    snapshotContents = SiteHtmlUtil.StripHTMLAdvanced(lastResponse.Contents);
                }
                else
                {
                    snapshotContents = "No response available right now.";
                }

                snapshotContents = snapshotContents.Replace("(\r|\n|\r\n)+", "<br //>");

                sb.AppendFormat(@"Snapshot for <b><a href='{0}'>{1}</a></b><br /><br />{2}<br /><br />", site.SiteUrl,
                                site.SiteName, snapshotContents);

                var siteErrorsForPeriod =
                    site.SiteErrors.Where(x => x.Date >= DateTime.Now.Subtract(TimeSpan.FromDays(user.SnapshotInterval))).OrderBy(x => x.Date).Take(10).ToList();

                if (siteErrorsForPeriod.Count > 0)
                {
                    sb.Append("Errors since last snapshot: <br />");

                    foreach (var siteError in siteErrorsForPeriod)
                    {
                        sb.AppendFormat("Em {0} ocorreu o erro: {1}. <br />", siteError.Date, siteError.ErrorDetails);
                    }
                }

                sb.Append("<br /><br />");
            }

            return(sb.ToString());
        }
Exemplo n.º 6
0
        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            using (var session = SharpArch.NHibernate.NHibernateSession.GetDefaultSessionFactory().OpenSession())
            {
                //SiteRepository siteRepository = new SiteRepository(session);
                //Site site = siteRepository.Get(SiteId);
                SiteResponseRepository siteResponseRepository = new SiteResponseRepository(session);
                var lastResponse = siteResponseRepository.GetLastResponse(SiteId);
                if (lastResponse != null)
                {
                    tbHtmlContents.Text = lastResponse.Contents;
                    tbTextContents.Text = SiteHtmlUtil.StripHTMLAdvanced(lastResponse.Contents);
                    //tbDiffContents.Text = lastResponse.Diff;
                    if (String.IsNullOrEmpty(lastResponse.Diff))
                    {
                        webBrowserDiffContents.NavigateToString("No contents... :(");
                    }
                    else
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append(NotificationHelper.GetBodyTopHtml());
                        sb.Append(lastResponse.Diff);
                        sb.Append(NotificationHelper.GetBottomHtml());
                        webBrowserDiffContents.NavigateToString(sb.ToString());
                    }


                    if (String.IsNullOrEmpty(lastResponse.Contents))
                    {
                        webBrowser.NavigateToString("No contents... :(");
                    }
                    else
                    {
                        webBrowser.NavigateToString(lastResponse.Contents);
                    }

                    liveBrowser.Navigate(lastResponse.Site.SiteUrl);
                    //webBrowser.Document
                }
            }
        }