0

Brief Idea: I am developing a small social networking kinda site.

Now there's a user "A" who has 100 followers...now what i want to do is whenever user "A" submits an article in the website all his followers should get an email with the article link...that is okay i can do it.

Problem: Now, there's a submit button on the page which stores the article in the DB and sends email to the followers...as there are many followers it takes a lot of time sending the emails...so the page keeps showing loading msg till all the emails are sent..how can i send all the emails asynchronously ??

i mean after the article has been submitted ...the emails should go automatically to the followers without putting the email sending function in the click event of the button....hope am not confusing you folks.

can i do something like store the article in the DB , redirect to the article page , start sending emails in a batch of 10 per 10 mins automatically...this process should start as soon as an article has been submitted by an user.

user1150440
  • 439
  • 2
  • 10
  • 23

4 Answers4

3

I had a similar issue with batch emails, and various other long-running tasks.

I developed a window service which contained a job manager. When a job needs to run from the main MVC application, the web application communicates with the service over HTTP (actually, using JSON), and the service performs the meat of actually sending emails, or performing other long-running tasks.

This means the web application request returns immediately.

The web application can also poll the service to determine the status of any particular job that is running (each job is given a unique identifier).

Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
0

I would create a database table containing information about all pending email notifications.

When hitting submit, you can quickly add rows to this table.
Then, a background thread can check the table and send the mails (and of course remove the successfully sent ones from the table).

Matthias
  • 12,053
  • 4
  • 49
  • 91
  • BEWARE that "background threads" in IIS/ASP.NET applications might cause trouble with the IIS threadpool... – Yahia Mar 14 '12 at 09:49
  • @Yahia: See http://stackoverflow.com/questions/1325718/using-threadpool-queueuserworkitem-in-asp-net-in-a-high-traffic-scenario and http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx - it shouldn't cause any problems when done right. – Matthias Mar 14 '12 at 09:52
  • @user1150440 see the answer of Moo-Juice which seems like a good idea and combine that with the part that says "table containing pending email notifications" of this answer - at least that is how I would solve this (Windows Service for doing batch emailing etc. combined with DB table for pending emails and stuff)... – Yahia Mar 14 '12 at 09:53
  • @winSharp93 actually reality is quite different... your first link talks about making it "more robust" - this does NOT mean that it is a good idea, it only says that it is possible (which I don't dispute)... – Yahia Mar 14 '12 at 09:55
  • @Yahia: I guess you need to differentiate based on the size of the site - I definitely wouldn't do this under heavy load scenarios but for a "small" site, it's ok IMHO. Besides, it should be quite easy to move the code into a service project at a later time. – Matthias Mar 14 '12 at 09:57
  • 1
    @winSharp93 IMO this is more of a "quick and dirty" option than a robust solution... it works for a while and after some time (the site grows/has more traffic) it starts behaving badly... then the big hunt for the problem starts (because usually one tends to forget this type of "technical debt")... I tend to avoid such "solution" if possible... – Yahia Mar 14 '12 at 10:00
0

Have you thought to implement it using AJAX ?

When the user press on the submit button, instead of posting back to the server, create 2 ajax calls: The first one is to save the article to the repository (database ?).

After receiving succesfull answer from the server (which can include the article id), invoke 2nd ajax call to send the mails. The server can start a thread to send the mails so the answer to the client will be immediate.

My preferred way of invoking ajax calls is using JQuery:

$.ajax({
    type: "POST",
    url: "services.aspx/SubmitArticle",
    data: "{articlecontent: '[put here the content you want to send]'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    error: function(response) {
      // Handle Error here. The response object contains the error details 
    },
    success: function(response) {
      // Check here if the article has been saved:
      // response.d property contains the server answer. It can be boolean,
      // integer, string or any other complex object

      // If article saved, invoke here the send mail ajax call. assuming the response.d contains
      // the article id:
      sendMails(response.d);

     // sendMails invokes another ajax similiar to this code snippest
    }
});

In the server side the async email send method can looks like:

 [WebMethod]
 public static bool SendMails(int articleId)
 {
     // Add the actual method that send mail to the thread pool
     ThreadPool.QueueUserWorkItem(new WaitCallback(DoSendMail), articleId);

     return true;
 }

 private void DoSendMail(object a)
 {
   int articleId = (int)a;
   // Your code that sends mails goes here
 }
Koby Mizrahy
  • 1,361
  • 2
  • 12
  • 23
-1

You could use a queueing system like MassTransit, ZMQ or MSMQ.

Or... If you really wanted to create a cool app, you could pass the emailing task to a node.js app!?

rogchap
  • 933
  • 6
  • 8
  • -1 for `node.js` when the question clearly states he is a C# developer. Why overcomplicate it with yet another language barrier? – Moo-Juice Mar 14 '12 at 10:10
  • Language Barrier?? What? A C# developer does not know JavaScript??? I would say that is making things simpler. – rogchap Mar 14 '12 at 10:17