Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to run scheduled tasks in ASP.NET CORE [duplicate]

Today we have built a console application for running the scheduled tasks for our ASP.NET website. But I think this approach is a bit error prone and difficult to maintain. How do you execute your scheduled task (in an windows/IIS/ASP.NET environment)

Update:

Examples of tasks:

  • Sending email from an email-queue in the database
  • Removing outdated objects from the database
  • Retrieving stats from Google AdWords and fill a table in the database.
like image 808
Niels Bosma Avatar asked Feb 12 '09 19:02

Niels Bosma


5 Answers

This technique by Jeff Atwood for Stackoverflow is the simplest method I've come across. It relies on the "cache item removed" callback mechanism build into ASP.NET's cache system

Update: Stackoverflow has outgrown this method. It only works while the website is running but it's a very simple technique that is useful for many people.

Also check out Quartz.NET

like image 54
BC. Avatar answered Sep 23 '22 17:09

BC.


All of my tasks (which need to be scheduled) for a website are kept within the website and called from a special page. I then wrote a simple Windows service which calls this page every so often. Once the page runs it returns a value. If I know there is more work to be done, I run the page again, right away, otherwise I run it in a little while. This has worked really well for me and keeps all my task logic with the web code. Before writing the simple Windows service, I used Windows scheduler to call the page every x minutes.

Another convenient way to run this is to use a monitoring service like Pingdom. Point their http check to the page which runs your service code. Have the page return results which then can be used to trigger Pingdom to send alert messages when something isn't right.

like image 37
Brettski Avatar answered Sep 21 '22 17:09

Brettski


Create a custom Windows Service.

I had some mission-critical tasks set up as scheduled console apps and found them difficult to maintain. I created a Windows Service with a 'heartbeat' that would check a schedule in my DB every couple of minutes. It's worked out really well.

Having said that, I still use scheduled console apps for most of my non-critical maintenance tasks. If it ain't broke, don't fix it.

like image 29
Chris Van Opstal Avatar answered Sep 21 '22 17:09

Chris Van Opstal


I've found this to be easy for all involved:

  • Create a webservice method such as DoSuchAndSuchProcess
  • Create a console app that calls this webmethod.
  • Schedule the console app in the task scheduler.

Using this methodology all of the business logic is contained in your web app, but you have the reliability of the windows task manager, or any other commercial task manager to kick it off and record any return information such as an execution report. Using a web service instead of posting to a page has a bit of an advantage because it's easier to get return data from a webservice.

like image 24
Daniel Auger Avatar answered Sep 24 '22 17:09

Daniel Auger


Why reinvent the wheel, use the Threading and the Timer class.

    protected void Application_Start()
    {
        Thread thread = new Thread(new ThreadStart(ThreadFunc));
        thread.IsBackground = true;
        thread.Name = "ThreadFunc";
        thread.Start();
    }

    protected void ThreadFunc()
    {
        System.Timers.Timer t = new System.Timers.Timer();
        t.Elapsed += new System.Timers.ElapsedEventHandler(TimerWorker);
        t.Interval = 10000;
        t.Enabled = true;
        t.AutoReset = true;
        t.Start();
    }

    protected void TimerWorker(object sender, System.Timers.ElapsedEventArgs e)
    {
        //work args
    }
like image 45
Moises Goncalves Avatar answered Sep 21 '22 17:09

Moises Goncalves