FreeScheduler 2.0.30

dotnet add package FreeScheduler --version 2.0.30
NuGet\Install-Package FreeScheduler -Version 2.0.30
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FreeScheduler" Version="2.0.30" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FreeScheduler --version 2.0.30
#r "nuget: FreeScheduler, 2.0.30"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install FreeScheduler as a Cake Addin
#addin nuget:?package=FreeScheduler&version=2.0.30

// Install FreeScheduler as a Cake Tool
#tool nuget:?package=FreeScheduler&version=2.0.30

FreeScheduler 是利用 IdleBus 实现的轻量化定时任务调度,支持集群、临时的延时任务和重复循环任务(可持久化),可按秒,每天/每周/每月固定时间,自定义间隔执行,支持 .NET Core 2.1+、.NET Framework 4.0+ 运行环境。

IdleScheduler 已正式改名为 FreeScheduler

如果对本项目感兴趣,欢迎加入 FreeSql QQ讨论群:8578575

Quick start

dotnet add package FreeScheduler

Install-Package FreeScheduler

static Scheduler scheduler = new FreeSchedulerBuilder()
    .OnExecuting(task =>
    {
        Console.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss.fff")}] {task.Topic} 被执行");
        switch (task.Topic)
        {
            case "武林大会": Wulin(task.Body); break;
            case "攻城活动": AttackCity(task.Body); break;
        }
    })
    .Build();
Method 说明
OnExecuting(Action<TaskInfo> executing) 任务触发
UseTimeZone() 设置时区
UseStorage() 基于 数据库或者 Redis 持久化
UseCluster() 开启集群(依赖 Redis),支持跨进程互通
UseCustomInterval() 自定义间隔(可实现 cron)
UseScanInterval() 扫描间隔(默认200ms),值越小触发精准
Build() 创建 Scheduler 对象

使用 ASP.NET Core 项目,一行代码解决如下:

app.UseFreeSchedulerUI("/freescheduler/");

image

集群特性

  • 支持 单项目,多站点部署
  • 支持 多进程,不重复执行
  • 支持 进程退出后,由其他进程重新加载任务(约30秒后)
  • 支持 进程互通,任意进程都可以执行(RemoveTask/ExistsTask/PauseTask/RunNowTask/RemoveTempTask/ExistsTempTask)
  • 支持 进程意外离线后,卸载进程内的任务,重新安排上线

1、临时任务(不可持久化)

void Callback()
{
    Console.WriteLine("时间到了");
    scheduler.AddTempTask(TimeSpan.FromSeconds(10), Callback); //下一次定时
}
scheduler.AddTempTask(TimeSpan.FromSeconds(10), Callback);
Method 说明
string AddTempTask(TimeSpan, Action) 创建临时的延时任务,返回 id
bool RemoveTempTask(string id) 删除任务(临时任务)
bool ExistsTempTask(string id) 判断任务是否存在(临时任务)
int QuantityTempTask 任务数量(临时任务)

2、循环任务/可持久化

//每5秒触发,执行N次
var id = scheduler.AddTask("topic1", "body1", round: -1, 5);

//每次 不同的间隔秒数触发,执行6次
var id = scheduler.AddTask("topic1", "body1", new [] { 5, 5, 10, 10, 60, 60 });

//每天 20:00:00 触发,执行N次(注意设置时区 UseTimeZone)
var id = scheduler.AddTaskRunOnDay("topic1", "body1", round: -1, "20:00:00");

//每周一 20:00:00 触发,执行1次
var id = scheduler.AddTaskRunOnWeek("topic1", "body1", round: 1, "1:20:00:00");

//每月1日 20:00:00 触发,执行12次
var id = scheduler.AddTaskRunOnMonth("topic1", "body1", round: 12, "1:20:00:00");
//每月最后一日 20:00:00 触发,执行12次
var id = scheduler.AddTaskRunOnMonth("topic1", "body1", round: 12, "-1:20:00:00");

//自定义间隔 cron
var id = scheduler.AddTaskCustom("topic1", "body1", "0/1 * * * * ? ");
new FreeSchedulerBuilder()
    ...
    .UseCustomInterval(task =>
    {
        //利用 cron 功能库解析 task.IntervalArgument 得到下一次执行时间
        //与当前时间相减,得到 TimeSpan,若返回 null 则任务完成
        return TimeSpan.FromSeconds(5);
    })
    .Build();
Method 说明
void ctor(ITaskHandler) 指定任务调度器(单例)
string AddTask(string topic, string body, int round, int seconds) 创建循环定时任务,返回 id
string AddTask(string topic, string body, int[] seconds) 创建每轮间隔不同的定时任务,返回 id
string AddTaskRunOnDay(..) 创建每日循环任务,返回 id
string AddTaskRunOnWeek(..) 创建每周循环任务,返回 id
string AddTaskRunOnMonth(..) 创建每月循环任务,返回 id
string AddTaskCustom(string topic, string body, string expression) 创建自定义任务,返回 id
bool RemoveTask(string id) 删除任务
bool ExistsTask(string id) 判断任务是否存在
bool ResumeTask(string id) 恢复已暂停的任务
bool PauseTask(string id) 暂停正在运行的任务
bool RunNowTask(string id) 立刻运行任务(人工触发)
TaskInfo[] FindTask(lambda) 查询正在运行中的任务
int QuantityTask 任务数量

3、预留任务

[系统预留]清理任务数据

//每小时触发,定期清理24小时之前的数据(单位:秒)
scheduler.AddTask("[系统预留]清理任务数据", "86400", round: -1, 3600);

4、管理任务

// 使用 FreeSql 或者 SQL 查询 TaskInfo、TaskLog 两个表进行分页显示
fsql.Select<TaskInfo>().Count(out var total).Page(pageNumber, 30).ToList();
fsql.Select<TaskLog>().Count(out var total).Page(pageNumber, 30).ToList();

//暂停任务
scheduler.PauseTask(id);
//恢复暂停的任务
scheduler.ResumeTask(id);
//删除任务
scheduler.RemoveTask(id);
//立刻运行任务(人工触发)
scheduler.RunNowTask(id);

如果正在使用 ASP.NET Core 项目,一行代码解决如下:

app.UseFreeSchedulerUI("/freescheduler/");

https://github.com/2881099/FreeScheduler/tree/master/Examples/Examples_FreeScheduler_Net60 image

Performance

FreeScheduler Quartz.net FluentScheduler HashedWheelTimer
(500,000 Tasks + 10s) (500,000 Tasks + 10s) (500,000 Tasks + 10s) (500,000 Tasks + 10s)
<img src="https://github.com/2881099/FreeScheduler/blob/master/Examples/Examples_FreeScheduler_VsQuartz/performance_self.png?raw=true"/> <img src="https://github.com/2881099/FreeScheduler/blob/master/Examples/Examples_FreeScheduler_VsQuartz/performance_quartz.png?raw=true"/> <img src="https://github.com/2881099/FreeScheduler/blob/master/Examples/Examples_FreeScheduler_VsQuartz/performance_fluentscheduler.png?raw=true"/> <img src="https://github.com/2881099/FreeScheduler/blob/master/Examples/Examples_FreeScheduler_VsQuartz/performance_hashedwheeltimer.png?raw=true"/>
383M 1700+M StackOverflow 213M
70563.6066ms 50692.5365ms 未知 33697.8758ms

FluentScheduler 单个 Registry 测试正常,但目测单线程执行(间隔1-10ms),处理速度不理想 View Code

我尝试把 FreeScheduler 内核改成 HashedWheelTimer 内存占用更高(600兆),结论:FreeScheduler 功能需要占用更多资源

💕 Donation (捐赠)

感谢你的打赏

🗄 License (许可证)

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net40 is compatible.  net403 was computed.  net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (9)

Showing the top 5 NuGet packages that depend on FreeScheduler:

Package Downloads
FreeSql.Cloud

提供跨数据库访问,分布式事务TCC、SAGA解决方案,支持 .NET Core 2.1+, .NET Framework 4.0+.

ZhonTai.Admin

中台Admin权限管理接口库

FreeScheduler.TaskHandlers.FreeRedis

轻量化定时任务调度,支持临时的延时任务和重复循环任务,可按秒,每天/每周/每月固定时间,自定义间隔执行,支持 .NET Core 2.1+、.NET Framework 4.0+ 运行环境。

AdminBlazor

AdminBlazor 是一款 Blazor Server SaaS 后台管理项目,支持 RABC 权限菜单/按钮,支持一对一、一对多、多对多代码生成 .razor 界面,集成菜单、角色、用户、定时任务、数据字典、租户等功能。

FreeScheduler.Dash

Package Description

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on FreeScheduler:

Repository Stars
leooneone/aibpm.plus
AIBPM是一个开源的工作流引擎。本项目是后端服务,前端请移步aibpm.ui.plus。
Version Downloads Last updated
2.0.30 1,720 3/26/2024
2.0.28 1,244 1/18/2024
2.0.27 562 1/8/2024
2.0.25 171 1/2/2024
2.0.18 2,626 12/15/2023
1.2.3 803 9/28/2023
1.2.2 259 9/5/2023
1.2.1 124 9/5/2023
1.1.0 19,347 3/14/2023
1.0.9 11,567 9/20/2022
1.0.8 1,489 9/5/2022
1.0.7 16,503 8/31/2022
1.0.3 1,960 8/1/2022
1.0.2 1,696 7/27/2022
1.0.0 391 7/20/2022