2

Folks,

We have just launched a survey application recently, its MVC using LINQ to entity.

We have a SurveyStatus table.

Every monday we want to run an automatic query that will check if a survey is complete. If the survey is complete then send an email to inform the user.

Previously we had done this using a scheduled task that ran a VB script to linked to the database. However now we would like to create this EmailService within the project to take advnatge of the Model and services already there.

Can anybody give me some advice on the best way to achieve this?

Thanks in advance

László Koller
  • 1,139
  • 6
  • 15
Dez79
  • 527
  • 2
  • 9
  • 25

4 Answers4

1

Please take a look at the Revalee open source project.

Revalee is a service that allows you to schedule web callbacks to your ASP.NET MVC application. Revalee manages task persistence and scheduling using a Windows Service, but leverages your ASP.NET MVC application to handle the processing effort (i.e., "the work").

The following is the workflow of an MVC application that uses Revalee:

Revalee Workflow
(source: sageanalytic.com)

In your case, you would schedule a future callback (when the user submits the survey) to trigger an action in your MVC application that would inspect your survey results. The outcome of that inspection would then trigger (or not) a subsequent email message to be sent to the user.

<edit>

Something else to consider...

You may want to change the way you approach your problem domain. Instead of scheduling a fixed task "every Monday" (that is, execute a process that tests all surveys all at the same time), flip the processing around: process each survey individually. Understandably this is problematic without a callback solution, like Revalee. But with Revalee, you can schedule each user's survey to be processed +48 hours (for example) after they submit it. This does two things:

  1. It alters the survey inspection "work" from testing all surveys to testing a survey.

  2. It spreads out the inspection processing throughout all days of the week (and hours of the day). That is, it will no longer be limited to processing every Monday at exactly, say, 8 o'clock in the morning.

To register a more individualized callback using Revalee, you might register a future callback like this:

private void ScheduleSurveyInspection(int surveyId)
{
    // The callback will be 48 hours from now
    DateTimeOffset callbackTime = DateTimeOffset.Now.AddHours(48.0);

    // The callback URL will include the ID of the survey to be reviewed
    Uri callbackUrl = new Uri(
            string.Format(
                    "http://mywebapp.com/Survey/Inspect/{0}",
                    surveyId
                )
        );
    
    // Register the callback request with the Revalee service
    RevaleeRegistrar.ScheduleCallback(callbackTime, callbackUrl);
}

When your MVC application is called back 48 hours later, your SurveyController would process the callback using the following Inspect action:

[AllowAnonymous]
[CallbackAction]
public ActionResult Inspect(int surveyId)
{
    // TODO Lookup the survey's information and perform the necessary review
    // ...
    return new EmptyResult();
}

Naturally, if your current processing workflow cannot be changed from occurring once a week every Monday to occurring +48 hours after survey submission, then Revalee can still be used to schedule that task. Your callbackUrl would simply be:

Uri callbackUrl = new Uri("http://mywebapp.com/Survey/InspectAll");

</edit>

I hope this helps. Good luck!

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

Community
  • 1
  • 1
László Koller
  • 1,139
  • 6
  • 15
0

You have quite a few options here, but since this is an MVC project, you should definitely check out the MVC Mailer.

What's nice about this, is that you can use a master page as a layout for your email, and a standard MVC view as the content for it. It's pretty easy to wire up, and there is some great documentation and examples to help you get started.

Also, if you have to make changes to your emails depending on which user you're sending it to, you can just add those parameters to the controller methods that are serving up the email views - now you can format content based on who you're sending to, on the fly.

Aside from that, all you'll need is an SMTP server to actually send the message, which it sounds like you already have.

X3074861X
  • 3,709
  • 5
  • 32
  • 45
0

I suggest you to use FluentScheduler.

A task scheduler that uses fluent interface to configure schedules. Useful for running cron jobs/automated tasks from your application.

This way you can use your already implemented methods.

using FluentScheduler;

public class MyRegistry : Registry
{
    public MyRegistry()
    {
        // Schedule an ITask to run at an interval
        Schedule<MyTask>().ToRunNow().AndEvery(2).Seconds();

        // Schedule an ITask to run once, delayed by a specific time interval. 
        Schedule<MyTask>().ToRunOnceIn(5).Seconds();

        // Schedule a simple task to run at a specific time
        Schedule(() => Console.WriteLine("Timed Task - Will run every day at 9:15pm: " + DateTime.Now)).ToRunEvery(1).Days().At(21, 15);

        // Schedule a more complex action to run immediately and on an monthly interval
        Schedule(() =>
        {
            Console.WriteLine("Complex Action Task Starts: " + DateTime.Now);
            Thread.Sleep(1000);
            Console.WriteLine("Complex Action Task Ends: " + DateTime.Now);
        }).ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0);

        //Schedule multiple tasks to be run in a single schedule
        Schedule<MyTask>().AndThen<MyOtherTask>().ToRunNow().AndEvery(5).Minutes();
    }
} 

FluentScheduler Github page

Ozan Yurtseven
  • 810
  • 8
  • 17
0

What about a simple console application to trigger your automated emails? My suggestion would be to use either a console application or a windows service scheduled to run every Monday by the host system's scheduling software (Windows Task Scheduler, cron, etc.).

Here and here is an interesting discussion on the similar requirement like yours.

Community
  • 1
  • 1
Dennis R
  • 3,195
  • 1
  • 19
  • 24