1

I'm trying to create a task scheduler that runs twice a day. I've implemented a task scheduler using CacheItemRemovedCallaback, as suggested in this post and this blog. I have the a feature that enables the admin to modified the scheduled times, and saves them on Application variables:

protected void UpdateSchedule(object sender, EventArgs e)
{
    Button button = sender as Button;
    if (button.ID == "scheduleButton1")
    {

        Application.Lock();
        Application["buildSchedule1"] = GetScheduleTime1; 
        Application.UnLock(); 
    }
    else if (button.ID == "scheduleButton2")
    {
        Application.Lock();
        Application["buildSchedule2"] = GetScheduleTime2; 
        Application.UnLock(); 
    }
    HttpRuntime.Cache.Remove(Global.DummyCachekey); //remove current scheduled task and set new time
 }

And on Global.aspx I have:

protected void Application_Start(object sender, EventArgs e)
{
    Application.Lock();
    Application["buildSchedule1"] = new TimeSpan(10,00, 0); //default time
    Application["buildSchedule2"] = new TimeSpan(16,00, 0); //default time
    Application.UnLock(); 
    SheduleTask(); 
}; 

The problem is that for some reason (probably due to app pool recycling) the schedule times get reset, and even some times the task won't start at the default times.

I found solutions that mention Windows services or Windows task scheduler, but that doesn't work for me since I need to be able to let the admin configure the times through the web application. (I search for this, but couldn't find anything).

Community
  • 1
  • 1

2 Answers2

2

I found solutions that mention Windows services or Windows task scheduler

That's what you should be using. A web application doesn't "always run." It responds to requests, that's all. Unless something is actively making a request to the website, it's not doing anything.

but that doesn't work for me since I need to be able to let the admin configure the times through the web application

Sure it does. More than one application can share a single database. In this case you'd have two applications:

  • A Web Application where users can login and maintain the data related to the scheduled tasks.
  • A Windows Service (or scheduled Console Application) which runs in the background on the server and executes the configured tasks which it reads from the database.
David
  • 208,112
  • 36
  • 198
  • 279
  • Thanks a lot! I hadn't thought of using the database and sharing it with a Windows Service. – AnthonyGiraldo Aug 14 '13 at 23:36
  • Why not use IIS? It's constantly running – Fandango68 Mar 03 '17 at 03:36
  • @Fernando68: The IIS service is "always running", the web application is not. IIS, like any web server, is optimized for the expected nature and architecture of web applications. Stateless request/response systems whose resources can be allocated and de-allocated based on the shared needs of the web server. Using it for any purpose for which it wasn't designed is simply using the wrong tool for the job. Use a web server to run a web application. Use a task scheduler to schedule tasks. – David Mar 03 '17 at 03:41
  • 1
    @David I disagree as per the blog mentioned by the OP in codeproject.com. The blog states - "First, we need something in ASP.NET that is continuously running and gives us a callback. The IIS web server is continuously running. So, we somehow need to get a frequent callback from it so that we can lookup a job queue and see if there's something that needs to be executed.". Is that now not correct? Of course if Admin stop IIS or server crashes, then that's where no one can help you other than a hit from a search engine or some other website service. A windows service is not the way to go. – Fandango68 Mar 03 '17 at 05:48
  • @Fernando68: You're contradicting yourself. You claim that IIS is the way to go because it's a continuously running background service, and then claim that a continuously running background service is not the way to go. IIS certainly *can* be used for this task, but that doesn't mean it *should*. Web servers do things which are unrelated to task scheduling, and are optimized for those things. In performance of its duties as a web server it *might* conflict with the surrogate duties you've imposed on it. Why take that chance? Use a tool designed for the job at hand. – David Mar 03 '17 at 10:54
0

Using hacks like Windows Scheduled Tasks and Control Panel abilities are not nice solutions. They sucks most of the time, they are a headache.

You can use ATrigger scheduling service. A .Net library is also available to create scheduled tasks without overhead.

//Tags: Tags are required to identify tasks. 
//read more at: http://atrigger.com/docs/wiki/9/rest-api-v10-parameter-tag_
Dictionary<string, string> tags = new Dictionary<string, string>();
tags.Add("type", "test");

//Create
ATrigger.Client.doCreate(TimeQuantity.Hour(), "12", "http://www.example.com/myTask?something", tags);

Disclaimer: I was among the ATrigger team. It's a freeware and I have not any commercial purpose.