user3755402
user3755402

Reputation: 11

Automatic Mailing based on server datetime

I am working in Asp.Net MVC, i have a scenario as follows.

"Employee will have specific end time to finish a task, once the time is completed, a mail has to be sent automatically to the team lead to notify."

In some way i have to check server datetime and the specified end datetime constantly,my frnd suggested me to check the database datetime with the endtime for every 30 secs by creating a console scheduler to watch the time, do i have any alternative to do this without checking the database frequently?

Upvotes: 1

Views: 299

Answers (2)

László Koller
László Koller

Reputation: 1159

If you know the task's end time (that is, the time when the email is supposed to be sent) then I would recommend that you schedule a task to call your ASP.NET MVC application back at the exact moment you need to send the email message. This would allow you to avoid having to 'poll' the server. To do this, I would recommend that you take a look at the Revalee open source project.

Revalee is a service that allows you to schedule web callbacks to your web application. In your case, you would schedule a callback that would send an email message at a specific time. Revalee works very well with tasks that are discrete transactional actions, like sending an automated email message or updating some database values (read: not long running). By using Revalee, the code to perform your action would all reside within your web application.

To use Revalee, you would:

  1. Install the Revalee Service, a Windows Service, on your server. The Windows Service is available in the source code (which you would compile yourself), in a precompiled version available at the Revalee website, or easily installable via Chocolatey.

  2. Use the Revalee client library in your Visual Studio project. (There is an MVC-specific version too.) The client library is available in the source code (which, again, you would compile yourself) or in a precompiled version available via NuGet.

  3. You would register a future callback when you know the end time for the user's task:

    private void ScheduleTaskEndEmail(int taskId)
    {
        // The DetermineTaskEndTime() method is a private method
        // (of your creation) which returns a task's end time as
        // a DateTimeOffset
        DateTimeOffset callbackTime = DetermineTaskEndTime(taskId);
    
        // The callback should at the task's end time
        Uri callbackUrl = new Uri(
                string.Format(
                        "http://mywebapp.com/Email/SendTaskEndEmail/{0}",
                        taskId
                    )
            );
    
        // Register the callback request with the Revalee service
        RevaleeRegistrar.ScheduleCallback(callbackTime, callbackUrl);
    }
    

4.When Revalee calls your application back, your app would perform whatever action you have coded it to do. In the Email controller, your SendTaskEndEmail action (see the Uri above) might look like:

    [AllowAnonymous]
    [CallbackAction]
    public ActionResult SendTaskEndEmail(int taskId)
    {
        // TODO   1. Validate the taskId,
        //        2. Lookup the task's information, &
        //        3. Send the email message
        // ...

        return new EmptyResult();
    }

The Revalee website has a complete API Reference as well as instructions on how to install and configure the Windows Service. I hope this helps.

Note: The code example above uses a synchronous version of ScheduleCallback(), the Revalee client library also supports asynchronous calls à la:

RevaleeRegistrar.ScheduleCallbackAsync(callbackTime, callbackUrl);

In case it was not clear above, the Revalee Service is not an external 3rd party online scheduler service, but instead a Windows Service that you install and fully control on your own network. It resides and runs on a Windows server of your own choosing where it can receive callback registration requests from your ASP.NET MVC application.

Disclaimer: I was one of the developers involved with the Revalee project. To be clear, however, Revalee is free, open source software (MIT license). The source code is available on GitHub.

Upvotes: 2

Pleun
Pleun

Reputation: 8920

I would suggest a windows service to check the timings.

do i have any alternative to do this without checking the database frequently

You could implement some kind of caching mechanism - keep all tasks in memory and check against server time - however if the tasks are updated frequently this cache would quickly become outdated or you need a way to synch the cache with the database.

So I would usethe approach to check every x seconds (30 seconds seems very little, how accurate do you want your notifications to be?? Perhaps once every 5 minutes will be enough depending on your requirements).

Upvotes: 2

Related Questions