受欢迎的博客标签

How to Integrate background Task FluentScheduler With ASP.NET Core

Published

 

FluentScheduler supports .NET Core, just add the dependency to project.json and run dotnet restore:

  "dependencies": {
    "FluentScheduler": "<desired version>"
  }
 

Stopping the Scheduler

To stop the scheduler:

JobManager.Stop();
 

To both stop and wait for the running jobs to finish:

JobManager.StopAndBlock();
 

Unexpected exceptions

To observe unhandled exceptions from your scheduled jobs listen for the JobException event on JobManager:

JobManager.JobException += info => Log.Fatal("An error just happened with a scheduled job: " + info.Exception);
 

Concurrent jobs

By default, the library allows a schedule to run in parallel with a previously triggered execution of the same schedule.

If you don't want such behaviour you can set a specific schedule as non-reentrant:

public class MyRegistry : Registry
{
    Schedule<MyJob>().NonReentrant().ToRunEvery(2).Seconds();
}
 

Or you can set it to all schedules of the registry at once:

public class MyRegistry : Registry
{
    NonReentrantAsDefault();
    Schedule<MyJob>().ToRunEvery(2).Seconds();
}
 

Daylight Saving Time

Unfortunately, not unlike many schedulers, there is no daylight saving time support yet.

If you are worried about your jobs not running or running twice due to that, the suggestion is to either avoid troublesome time ranges or to force the use of UTC:

JobManager.UseUtcTime();
 

Milliseconds Accuracy

The aim of the library is ease of use and flexibility, and not millisecond precision. While the library has a millisecond unit, you should avoid relying on really small intervals (less than 100ms).

Weekly jobs

Let's suppose it's 10:00 of a Monday morning and you want to start a job that runs every Monday at 14:00. Should the first run of your job be today or only on the next week Monday?

If you want the former (not waiting for a whole week):

// Every "zero" weeks
Schedule<MyJob>().ToRunEvery(0).Weeks().On(DayOfWeek.Monday).At(14, 0);
 

Or if you want the latter (making sure that at least one week has passed):

// Every "one" weeks
Schedule<MyJob>().ToRunEvery(1).Weeks().On(DayOfWeek.Monday).At(14, 0);