Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Birthdays Reminder Code for Asp.Net MVC

I want to create a web application in MVC Asp.Net for Hotel Room Booking and Customers Management. I am having trouble with one of the requirement. I want to create a code for Sending SMS to Customers on their Birthdays for Wishing them from Hotel. I am confused, that where should i place the code for checking customers with birthdate same as today's Date, so that Code gets Triggered every day at 12:00 am even if the web application is not launched. Please can you explain where should i insert the code?

like image 907
Pankaj Jhamnani Avatar asked Mar 15 '23 08:03

Pankaj Jhamnani


2 Answers

There is a open-source library called Quarz which will help you with that.

There is a very good blog article by Mike Brind about this library. The library provides a fluent API which allows you to do exactly what you want.

The following code (based on the example of the mentioned blog article) creates a event which is called every day at 12 o´clock:

 IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
 scheduler.Start();

 IJobDetail job = JobBuilder.Create<BirthdayJob>().Build();

 ITrigger trigger = TriggerBuilder.Create()
    .WithDailyTimeIntervalSchedule
       (s =>
        s.WithIntervalInHours(24)
        .OnEveryDay()
        .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(12, 0))
        )
 .Build();

 scheduler.ScheduleJob(job, trigger);

It can run in the context of the Website, a Windows Service or even a WinForms-Application (as long the user doesn't closes it).

Depending on the context you need to schedule the job in different places. For a Website in would be the Application_Start()-Method. For a Service this would be the OnStart()-Method.

Additionally you need a class "BirthdayJob" which will provide the actual code which should be executed periodically:

public class BirthdayJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        // Check for birthdays...
    }
}

There is one point you should be aware of if you call this code in the context of a website:

It's possible that the IIS puts your website to sleep if it isn't requested for a while. In this case it would be possible that the scheduled tasks are not executed.

There is an option to change the timeout for your website: In the IIS manager go to "Application Pools", find the application pool that is used for your website and select "Properties" -> "Performance". There you can set the idle timeout. There are several other approaches to solve this problem.

However, if your website is requested frequently you would never see this problem.

The best way to go would be to call Quarz from a Windows-Service which runs even if your websites sleeps or lays down drunken under a table.

like image 91
Stefan Wanitzek Avatar answered Mar 24 '23 01:03

Stefan Wanitzek


You could ship a service together with your web application which does the periodic checks and runs separately from the site.

Another usage of this service could be general maintenance of the data store which is used by the site, thus, for instance, once a day it would archive information, check birthdays and any other maintenance or periodic tasks, such as the issuing of reminders, etc. which your platform would issue.

like image 44
npinti Avatar answered Mar 24 '23 00:03

npinti