I have used fluentSchedular in my MVC application. In that my requirement is email should go before 30 days, 15 days, and 3 days ago. Also email reminder should go before 3 hours and 30 minutes when client date is expires.
I put following code..
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
ModelBinders.Binders.Add(typeof(DataTableRequestModel), new DataTableRequestModelBinder());
TaskManager.Initialize(new MyRegistry());
}
then I create a new class file called MyRegistry. In that class file I put following code.
======================================================.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using FluentScheduler;
using NexusWeb.Data;
public class MyRegistry : Registry
{
public MyRegistry()
{
Schedule<ThreeHoursReminder>().ToRunNow().AndEvery(1).Minutes();
Schedule<ThirtyMinutesReminder>().ToRunNow().AndEvery(1).Minutes().DelayFor(20).Seconds();
Schedule<ThirtyDaysReminder>().ToRunNow().AndEvery(0).Days().At(10, 05);
Schedule<FifteenDaysReminder>().ToRunNow().AndEvery(0).Days().At(10, 06);
Schedule<ThreeDaysReminder>().ToRunNow().AndEvery(0).Days().At(10, 07);
Schedule<TaskReminder>().ToRunNow().AndEvery(0).Days().At(10, 08);
}
}
public class ThreeHoursReminder : ITask
{
private ICallListRepository _ThreeHoursRepo;
private ICallListRepository ThreeHoursRepo
{
get
{
if (_ThreeHoursRepo == null)
{
IDependencyResolver resolver = DependencyResolver.Current;
if (resolver != null) _ThreeHoursRepo = resolver.GetService<ICallListRepository>();
}
return _ThreeHoursRepo;
}
}
public void Execute()
{
StringBuilder sb = new StringBuilder();
List<CallList> lst3HoursUsers = ThreeHoursRepo.GetCallListforReminder(180);
for (int i = 0; i < lst3HoursUsers.Count; i++)
{
sb.Clear();
sb.Append("Hello, <b>" + lst3HoursUsers[i].CallBy + "</b> <br/><br/>");
sb.Append("Please check the following details for Client :- <br/>");
sb.Append("Client Name : " + lst3HoursUsers[i].ClientName + "<br/>");
sb.Append("Client Phone number : " + lst3HoursUsers[i].ClientPhoneNo + "<br/>");
sb.Append("Contact Person : " + lst3HoursUsers[i].ContactPerson + "<br/><br/>");
sb.Append("Please check below description when you call to client <br/>");
sb.Append(lst3HoursUsers[i].Title + "<br/><br/>");
sb.Append("--<br/>");
sb.Append("Thanks & Regards<br/>");
sb.Append("Nexusweb Team<br/>");
NexusUtility.SendEmail(lst3HoursUsers[i].CallByUserEmail, "Call to client after 3 hours", sb.ToString());
}
}
}
public class ThirtyMinutesReminder : ITask
{
private ICallListRepository _ThirtyMinutesRepo;
private ICallListRepository ThirtyMinutesRepo
{
get
{
if (_ThirtyMinutesRepo == null)
{
IDependencyResolver resolver = DependencyResolver.Current;
if (resolver != null) _ThirtyMinutesRepo = resolver.GetService<ICallListRepository>();
}
return _ThirtyMinutesRepo;
}
}
public void Execute()
{
StringBuilder sb = new StringBuilder();
List<CallList> lst30MinutesUser = ThirtyMinutesRepo.GetCallListforReminder(30);
for (int j = 0; j < lst30MinutesUser.Count; j++)
{
sb.Clear();
sb.Append("Hello, <b>" + lst30MinutesUser[j].CallBy + "</b> <br/><br/>");
sb.Append("Please check the following details for Client :- <br/>");
sb.Append("Client Name : " + lst30MinutesUser[j].ClientName + "<br/>");
sb.Append("Client Phone number : " + lst30MinutesUser[j].ClientPhoneNo + "<br/>");
sb.Append("Contact Person : " + lst30MinutesUser[j].ContactPerson + "<br/><br/>");
sb.Append("Please check below description when you call to client <br/>");
sb.Append(lst30MinutesUser[j].Title + "<br/><br/>");
sb.Append("--<br/>");
sb.Append("Thanks & Regards<br/>");
sb.Append("Nexusweb Team<br/>");
NexusUtility.SendEmail(lst30MinutesUser[j].CallByUserEmail, "Call to client after 30 Minutes", sb.ToString());
}
}
}
public class ThirtyDaysReminder : ITask
{
private IClientRepository _ThirtyDaysRepo;
private IClientRepository ThirtyDaysRepo
{
get
{
if (_ThirtyDaysRepo == null)
{
IDependencyResolver resolver = DependencyResolver.Current;
if (resolver != null) _ThirtyDaysRepo = resolver.GetService<IClientRepository>();
}
return _ThirtyDaysRepo;
}
}
public void Execute()
{
StringBuilder sb = new StringBuilder();
List<NexusClient> lst30DaysClient = ThirtyDaysRepo.GetClientListforReminder(30);
for (int j = 0; j < lst30DaysClient.Count; j++)
{
sb.Clear();
sb.Append("Hello, <b>Admin</b> <br/><br/>");
sb.Append("Please check the following details for Client :- <br/>");
sb.Append("Client Name : " + lst30DaysClient[j].Name + "<br/>");
sb.Append("Client Phone number : " + lst30DaysClient[j].PhoneNo + "<br/>");
sb.Append("Contact Person : " + lst30DaysClient[j].ContactPerson + "<br/><br/>");
sb.Append("Below is the details of client has purchased service, service due date is : <b>" + lst30DaysClient[j].DueDate.ToString() + "</b><br/>");
sb.Append(lst30DaysClient[j].ServicePurchaseDetail + "<br/><br/>");
sb.Append("--<br/>");
sb.Append("Thanks & Regards<br/>");
sb.Append("Nexusweb Team<br/>");
NexusUtility.SendEmail("[email protected]", "Client service expired in next 30 days", sb.ToString());
}
}
}
public class FifteenDaysReminder : ITask
{
private IClientRepository _FifteenDaysRepo;
private IClientRepository FifteenDaysRepo
{
get
{
if (_FifteenDaysRepo == null)
{
IDependencyResolver resolver = DependencyResolver.Current;
if (resolver != null) _FifteenDaysRepo = resolver.GetService<IClientRepository>();
}
return _FifteenDaysRepo;
}
}
public void Execute()
{
StringBuilder sb = new StringBuilder();
List<NexusClient> lst15DaysClient = FifteenDaysRepo.GetClientListforReminder(15);
for (int j = 0; j < lst15DaysClient.Count; j++)
{
sb.Clear();
sb.Append("Hello, <b>Admin</b> <br/><br/>");
sb.Append("Please check the following details for Client :- <br/>");
sb.Append("Client Name : " + lst15DaysClient[j].Name + "<br/>");
sb.Append("Client Phone number : " + lst15DaysClient[j].PhoneNo + "<br/>");
sb.Append("Contact Person : " + lst15DaysClient[j].ContactPerson + "<br/><br/>");
sb.Append("Below is the details of client has purchased service, service due date is : <b>" + lst15DaysClient[j].DueDate.ToString() + "</b><br/>");
sb.Append(lst15DaysClient[j].ServicePurchaseDetail + "<br/><br/>");
sb.Append("--<br/>");
sb.Append("Thanks & Regards<br/>");
sb.Append("Nexusweb Team<br/>");
NexusUtility.SendEmail("[email protected]", "Client service expired in next 15 days", sb.ToString());
}
}
}
public class ThreeDaysReminder : ITask
{
private IClientRepository _ThreeDaysRepo;
private IClientRepository ThreeDaysRepo
{
get
{
if (_ThreeDaysRepo == null)
{
IDependencyResolver resolver = DependencyResolver.Current;
if (resolver != null) _ThreeDaysRepo = resolver.GetService<IClientRepository>();
}
return _ThreeDaysRepo;
}
}
public void Execute()
{
StringBuilder sb = new StringBuilder();
List<NexusClient> lst3DaysClient = ThreeDaysRepo.GetClientListforReminder(3);
for (int j = 0; j < lst3DaysClient.Count; j++)
{
sb.Clear();
sb.Append("Hello, <b>Admin</b> <br/><br/>");
sb.Append("Please check the following details for Client :- <br/>");
sb.Append("Client Name : " + lst3DaysClient[j].Name + "<br/>");
sb.Append("Client Phone number : " + lst3DaysClient[j].PhoneNo + "<br/>");
sb.Append("Contact Person : " + lst3DaysClient[j].ContactPerson + "<br/><br/>");
sb.Append("Below is the details of client has purchased service, service due date is : <b>" + lst3DaysClient[j].DueDate.ToString() + "</b><br/>");
sb.Append(lst3DaysClient[j].ServicePurchaseDetail + "<br/><br/>");
sb.Append("--<br/>");
sb.Append("Thanks & Regards<br/>");
sb.Append("Nexusweb Team<br/>");
NexusUtility.SendEmail("[email protected]", "Client service expired in next 15 days", sb.ToString());
}
}
}
public class TaskReminder : ITask
{
private ITaskRepository _taskRepo;
private ITaskRepository TaskRepo
{
get
{
if (_taskRepo == null)
{
IDependencyResolver resolver = DependencyResolver.Current;
if (resolver != null) _taskRepo = resolver.GetService<ITaskRepository>();
}
return _taskRepo;
}
}
public void Execute()
{
StringBuilder sb = new StringBuilder();
List<Task> lstUsersTask = TaskRepo.GetUserTaskListForReminder();
for (int j = 0; j < lstUsersTask.Count; j++)
{
sb.Clear();
sb.Append("Hello, <b>" + lstUsersTask[j].TaskAssignTo + "</b> <br/><br/>");
sb.Append("Please check the following task has been assigned to you :- <br/>");
sb.Append("Task Name : " + lstUsersTask[j].Title + "<br/>");
sb.Append("Task duration : " + lstUsersTask[j].TimeDuration + " Hrs.<br/>");
sb.Append("Task EndDate : " + lstUsersTask[j].EndDate.Value.ToString("dd/MM/yyyy") + " Hrs.<br/>");
sb.Append("Priority : " + lstUsersTask[j].Priority + "<br/><br/>");
sb.Append("Task should be completed within : " + lstUsersTask[j].EstimatedDays + " days. <br/><br/>");
sb.Append("<u>Below is the task description</u><br/>");
sb.Append(lstUsersTask[j].Description + "<br/><br/>");
sb.Append("--<br/>");
sb.Append("Thanks & Regards<br/>");
sb.Append("Nexusweb Team<br/>");
NexusUtility.SendEmail(lstUsersTask[j].TaskAssignToUserEmail, "Task Reminder", sb.ToString());
var task = TaskRepo.SingleOrDefault(lstUsersTask[j].Id);
task.LastEmailSentDate = DateTime.Today.Date;
TaskRepo.Update(task);
}
}
}
Also Scheduler for each minutes is some time not working. When I am checking this code in local machine it some time works and some time not working.
I have attached a image for code structure I pasted above. please refer that too.
Please let me know where is things going wrong. I am trying to run schedule since last 3 days from above code. But I still wondering whats wrong in above code.
Thanks in advance.