The best way to run an automated task every minute when a site is on multiple servers

I need to set up an automatic task that runs every minute and sends emails to the queue. I am using ASP.NET 4.5 and C #. I am currently using a scheduler class that runs in global.asax and uses caching and cache callbacks. I read that this leads to several problems.

The reason I did this is because this application runs on multiple servers with load balancing, and this allows me to execute execution in one place, and the code will work even if one or more servers are offline.

I'm looking for some direction to make it better. I read about Quartz.NET but never used it. Does Quartz.NET use methods from the application? or from a windows service? or from a web service?

I also read about using the Windows service, but as far as I can tell, they are installed on the server directly. The fact is that I need to complete the task no matter how many servers are connected to the network and do not want to duplicate it. For example, if I have a scheduled task setup on server 1 and server 2, they will both work together to duplicate requests. However, if server 1 was down, I need server 2 to run the task.

Any tips on how to move forward or is this the global.asax method, the best way for a multi-server environment? BTW, web servers run Win Server 2012 with IIS 8.

EDIT

. , -. , . , , . , .

, Windows, ? , .

, Quartz.NET ? , , -, Windows , , . ?

. - , .

+5
2

, .

-, , ASP.NET. global.asax, .

, IIS , . , ( ). . .

Windows . -, .

Quartz scheduler. , , , . , Quartz IJob.

class EmailSender : Quartz.IJob
{
    public void Execute(JobExecutionContext context)
    {
        // send your emails here
    }
}

, Quartz Execute , , .

, , . , , !

, "lock" . , , . , , guid :

UPDATE EmailQueue SET Lock=someGuid WHERE Lock IS NULL LIMIT 1;
SELECT * FROM EmailQueue WHERE Lock=someGuid;

concurrency. UPDATE ( ) . SELECT . (, , ), , .

:

  • .
  • , , .

- , , .


: , .

-, ASP . , , , . , .

, , . , 30 . , 5 . (), , , , , .

-, 5 , . , ( ). , , node ( - , ), . "" . , . , node, , , .

, . Quartz , , .

, . , . , -, , . :

class EmailSender : IJob {
    static int counter = 0;

    public void Execute(JobExecutionContext context) {
        counter++; // BAD!
    }
}

, counter .

Thread A           Thread B
Execute()
                   Execute()
Get counter (0)
                   Get counter (0)
Increment (1)
                   Increment (1)
Store value
                   Store value

            counter = 1 

counter 2, . , , :

Thread A           Thread B
Execute()
                   Execute()
Get counter (0)
Increment (1)
Store value
                   Get counter (1)
                   Increment (2)
                   Store value

            counter = 2

... , .

, Execute - , .

+5

. ; ? , "", , , , true , . concurrency .

+1

All Articles