// // 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)); }
public void verify_sends_email() { 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 reminders = target.GetAllRemindersDueBefore(new DateTime(2012, 10, 20)); var msgs = target.ComposeMessages(reminders); // var emails = target.SendEmails(msgs); }
public void verify_compose_message_with_all_reminders_for_one_users() { var reminders = new List <Reminder>(); reminders.Add(new Reminder { UserId = "users/99", Due = new DateTime(2012, 10, 20), RemindersSent = 0, LastNoteText = "Created 20.10", ProjectId = "projects/99", ProjectTitle = "Project99" }); reminders.Add(new Reminder { UserId = "users/98", Due = new DateTime(2012, 10, 19), RemindersSent = 0, LastNoteText = "Created 19.10", ProjectId = "projects/98", ProjectTitle = "Project99" }); reminders.Add(new Reminder { UserId = "users/99", Due = new DateTime(2012, 10, 18), RemindersSent = 0, LastNoteText = "Created 18.10", ProjectId = "projects/99", ProjectTitle = "Project99" }); var target = new ReminderHelper(RavenSession, templateRootPath); var result = target.ComposeMessages(reminders); Assert.AreEqual(result.Count, 2); Assert.AreEqual(result[0].Reminders.Count, 2); Assert.AreEqual(result[1].Reminders.Count, 1); Assert.IsTrue(result[0].HtmlBodyString.Contains("Project99")); }