//
        // GET: /Scheduler/
        public ActionResult Index()
        {
            var schedule = RavenSession.Load<ScheduleSettings>("scheduleSettings/1");
            var data = new List<EmailReminderData>();

            int delayHours = 4;

            if (schedule == null)
            {
                schedule = new ScheduleSettings { Id = "scheduleSettings/1" }; RavenSession.Store(schedule); ViewBag.Message = "This is the first run!";
                ViewBag.Error="First run, creating session";
                return View();
            }

            if ((DateTime.Now - schedule.LastRun).TotalHours > delayHours)
            {
                try
                {
                    var logic = new ReminderHelper(RavenSession, Server.MapPath("/Views/Templates/"));

                    var reminders = logic.GetAllRemindersDueBefore(DateTime.Now);
                    var msgs = logic.ComposeMessages(reminders);
                    ViewBag.Messages = msgs;
                    var emails = logic.SendEmails(msgs);
                    ViewBag.SentMessages = emails;
                    logic.UpdateMsgCount(reminders);

                }
                catch (Exception ex)
                {
                    ViewBag.Error = ex.Message;
                }

                    ViewBag.Message = "Last run: " + schedule.LastRun;

                if (data.Count > 0)
                {
                    string emailText = GetEmailText(data);
                    RavenSession.Store(new TempTextEmail { Text = emailText });
                }
                schedule.LastNumberOfNotifications = data.Count;
                schedule.LastRun = DateTime.Now;
                RavenSession.SaveChanges();
            }
            else
            {
                ViewBag.Error = "Scheduler will not run before" + (delayHours-(DateTime.Now - schedule.LastRun).TotalHours).ToString() + " hours";
            }
            return View(data);
        }
Beispiel #2
0
        public void verify_increase_sentcount()
        {
            RavenSession.Store(new Reminder { UserId = "users/99", Due = new DateTime(2012, 10, 20), RemindersSent = 0, LastNoteText = "Created 20.10", ProjectId = "projects/99" });
            RavenSession.Store(new Reminder { UserId = "users/99", Due = new DateTime(2012, 10, 19), RemindersSent = 1, LastNoteText = "Created 19.10", ProjectId = "projects/99" });
            RavenSession.Store(new Reminder { UserId = "users/99", Due = new DateTime(2012, 10, 18), RemindersSent = 2, LastNoteText = "Created 18.10", ProjectId = "projects/99" });
            RavenSession.SaveChanges();

            var target = new ReminderHelper(RavenSession, templateRootPath);
            var result = target.GetAllRemindersDueBefore(new DateTime(2012, 10, 20));
            Assert.AreEqual(result.Count, 2);
            target.UpdateMsgCount(result);
            RavenSession.SaveChanges();
            result = target.GetAllRemindersDueBefore(new DateTime(2012, 10, 20));
            Assert.AreEqual(result.Count, 1);
        }