FluentScheduler
The job configuration is handled in a Registry class. A job is either an Action or a class that inherits IJob:
using FluentScheduler;
public class MyRegistry : Registry
{
public MyRegistry()
{
// Schedule an IJob to run at an interval
Schedule<MyJob>().ToRunNow().AndEvery(2).Seconds();
// Schedule an IJob to run once, delayed by a specific time interval
Schedule<MyJob>().ToRunOnceIn(5).Seconds();
// Schedule a simple job to run at a specific time
Schedule(() => Console.WriteLine("It's 9:15 PM now.")).ToRunEvery(1).Days().At(21, 15);
// Schedule a more complex action to run immediately and on an monthly interval
Schedule<MyComplexJob>().ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0);
// Schedule a job using a factory method and pass parameters to the constructor.
Schedule(() => new MyComplexJob("Foo", DateTime.Now)).ToRunNow().AndEvery(2).Seconds();
// Schedule multiple jobs to be run in a single schedule
Schedule<MyJob>().AndThen<MyOtherJob>().ToRunNow().AndEvery(5).Minutes();
}
}
You can also use the Registry class directly (instead of inheriting it):
var registry = new Registry();
registry.Schedule<MyJob>().ToRunNow().AndEvery(2).Seconds();
registry.Schedule<MyJob>().ToRunOnceIn(5).Seconds();
registry.Schedule(() => Console.WriteLine("It's 9:15 PM now.")).ToRunEvery(1).Days().At(21, 15);
registry.Schedule<MyComplexJob>().ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0);
registry.Schedule<MyJob>().AndThen<MyOtherJob>().ToRunNow().AndEvery(5).Minutes();
With the registry ready you then need to initialize the JobManager. This is usually done as soon as your application is loaded (in the Application_Start method of a web application for example):
protected void Application_Start()
{
JobManager.Initialize(new MyRegistry());
}
It's also possible to schedule jobs after initialization:
JobManager.AddJob(() => Console.WriteLine("Late job!"), (s) => s.ToRunEvery(5).Seconds());
Using it with ASP.NET
When using it with ASP.NET consider implementing IRegisteredObject and registering it on HostingEnvironment (here's why), like:
public class SampleJob : IJob, IRegisteredObject
{
private readonly object _lock = new object();
private bool _shuttingDown;
public SampleJob()
{
// Register this job with the hosting environment.
// Allows for a more graceful stop of the job, in the case of IIS shutting down.
HostingEnvironment.RegisterObject(this);
}
public void Execute()
{
try
{
lock (_lock)
{
if (_shuttingDown)
return;
// Do work, son!
}
}
finally
{
// Always unregister the job when done.
HostingEnvironment.UnregisterObject(this);
}
}
public void Stop(bool immediate)
{
// Locking here will wait for the lock in Execute to be released until this code can continue.
lock (_lock)
{
_shuttingDown = true;
}
HostingEnvironment.UnregisterObject(this);
}
}
Using it with .NET Core
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);
Dependency Injection
Currently, the library supports dependency injection of jobs (via IJobFactory). However, you shouldn't use it, it's on its way to be deprecated (here's why).
Contributing
Feel free to open an issue or to submit a pull request.
To make sure your pull request doesn't get rejected, discuss it in an issue beforehand. Also, remember to Run All Tests (Ctrl + R, A), Run Code Analysis on Solution (Alt + F11) and to be consistent with the existing code.
You can also help others in need for support, check the "help wanted" label.
// 立即执行计划任务,并根据指定时间间隔执行一次计划任务
//(指定一个时间间隔运行,根据自己需求,可以是秒、分、时、天、月、年等。)
Schedule().ToRunNow().AndEvery(10).Seconds();//每十秒运行一次
// 延迟一个指定时间间隔执行一次计划任务。
//(当然,这个间隔依然可以是秒、分、时、天、月、年等。)
Schedule().ToRunOnceIn(10).Seconds();
//在一个指定时间执行计划任务 每个小时的第10分钟执行
Schedule().ToRunEvery(1).Hours().At(46);
// 在一个指定时间执行计划任务(最常用。这里是在每天的下午 1:10 分执行) Schedule().ToRunEvery(1).Days().At(13,10);
//每n年的第几天
Schedule().ToRunEvery(1).Years().On(5).At(12, 00);
//每n年的最后一天
Schedule().ToRunEvery(1).Years().OnTheLastDay();
//每n月的第几天
Schedule().ToRunEvery(1).Months().On(1).At(12,0);
//每n月的第一个星期的星期5 的15:0执行Schedule().ToRunEvery(1).Months().OnTheFirst(DayOfWeek.Friday).At(15, 0);
//每n月的第一个星期的星期5 的15:0执行 CleanJob和TestJob
Schedule().AndThen().ToRunEvery(1).Months().OnTheFirst(DayOfWeek.Friday).At(15, 0);
//在每天3点清理数据
Schedule().ToRunNow().AndEvery(10).Days().At(3,
00);
最后还要在项目的Global.asax那注册定时任务
双击进出,在里面注册定时任务
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
2016-04-20 通过autofac教你彻底明白依赖解耦(二)理论结合实践 - 大侠.Net
2016-04-20 Autofac 组件、服务、自动装配 《第二篇》
2016-04-20 依赖注入框架Autofac的简单使用
2016-04-20 WebAPI2使用Autofac实现IOC属性注入完美解决方案
2016-04-20 .NET领域最为流行的IOC框架之一Autofac
2016-04-20 .NET基于Redis缓存实现单点登录SSO的解决方案
2015-04-20 http://www.cnblogs.com/eye-like/p/4121219.html