tasks in past and iis application pool recycling

Mar 10, 2012 at 12:10 AM

When scheduling something for a specific time per day (let's say 22,0) - 10PM - I think the logic seems to work out nicely if that event takes place in the future.  But what happens if it happens in the past (let's say it's 10:03 PM).  Does the fluentscheduler logic decide to run it immediately since it happened in the past?

If so, what would happen if the application pool recycles in IIS - say around 10:03 PM.  Assuming that the task ran when it was scheduled (10PM), would it decide to run again?

If that's the case, the workaround I could think of was using a database to track instances when that task ran (count per day).  When the task was run, it could check that count and decide to do nothing if it had already run.

Are my assumptions correct about the behavior of this library?  Is there anyway to keep my Task unaware of it's schedule (without putting this code into the task itself)?  Just looking to craft a more elegant solution for this case - if my assumptions are correct.

 

Thanks in advance, this is a nice API.

Mar 10, 2012 at 12:24 AM

I browsed the source code a bit more, it looks like it will run the next day (in my scenario), rather than try to make-up a missed task.  So I guess that's good, perhaps I would want to figure out how to do the reverse (run a missed task if things were down at the time when the timer would have kicked off).

Coordinator
Mar 10, 2012 at 2:48 PM
Edited Mar 10, 2012 at 2:50 PM

Right, it will not try to make up missed tasks.  To help with that behavior, I have some of my tasks configured with .ToRunNow().AndEvery(1).Days().At(22,0).  That way, they'll run when the application starts and then again at a specified time.  

Alternatively, you can add logic into your task to determine when it should run and execute the task on an interval.  This will ensure that your task is run even if the scheduled time has passed.  For example, run the task every 5-15 minutes and inside the task logic, check a LastRun value stored in a database, xml file, etc.  Something similar to:

 

public class MyTask : TaskBase {

  public void Execute() {

    // This would get a settings object for this task - use your datastore of choice
    var settings = Session.Load<TaskSettings>("MyTask");

    if (settings.NextRun <= DateTime.Now) {
      // Make sure to update the last run, so that the task runs tomorrow at 10pm
      settings.NextRun = DateTime.Today.AddDays(1).AddHours(22);
      Session.Store(settings);


      // Do actual task work

    }
  }
}

Hope that helps