VassasCo.Utility.ScheduleHelper 1.1.0

dotnet add package VassasCo.Utility.ScheduleHelper --version 1.1.0
                    
NuGet\Install-Package VassasCo.Utility.ScheduleHelper -Version 1.1.0
                    
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="VassasCo.Utility.ScheduleHelper" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="VassasCo.Utility.ScheduleHelper" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="VassasCo.Utility.ScheduleHelper" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add VassasCo.Utility.ScheduleHelper --version 1.1.0
                    
#r "nuget: VassasCo.Utility.ScheduleHelper, 1.1.0"
                    
#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.
#:package VassasCo.Utility.ScheduleHelper@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=VassasCo.Utility.ScheduleHelper&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=VassasCo.Utility.ScheduleHelper&version=1.1.0
                    
Install as a Cake Tool

ScheduleHelper

全能定时任务调度器。支持 CRON/FixedRate/FixedDelay 三种模式,内置重试、超时、日历过滤、线程池管理。

文件说明

文件 内容
Models.cs 枚举、数据结构(JobStats、JobContext、JobResult、JobEventArgs)、委托
CronExpression.cs CRON 表达式解析器(5 段格式,支持 *,-/?L 特殊字符)
CronBuilder.cs CRON 表达式生成器(20+ 预设 + 流式 API)
ScheduleCore.cs JobBuilder、ScheduleBuilder、ScheduleHelper 主类、ScheduleLogWriter
HolidayCalendar.cs 节假日日历(JSON 加载、调休冲突检测、安全加载)

快速开始

// 创建调度器
var scheduler = ScheduleHelper.Build()
    .SetMaxConcurrency(4)
    .SetLogPath("D:/SchedulerLogs")
    .AddJob(j => j
        .SetName("OrderSync")
        .SetCron("0 */5 * * *")                    // 每 5 分钟
        .SetJob(ctx => SyncOrdersAsync(ctx))        // 任务体
        .SetTimeout(TimeSpan.FromMinutes(2))         // 超时
        .SetRetry(3, TimeSpan.FromSeconds(5)))       // 重试 3 次,间隔 5s
    .AddJob(j => j
        .SetName("CacheRefresh")
        .SetFixedDelay(TimeSpan.FromSeconds(30))     // 每次完成后等 30s
        .SetJob(ctx => RefreshCache(ctx))
        .SetPolicy(JobPolicy.MultiInstance))         // 允许多例并发
    .Build();

// 优雅关闭
await scheduler.ShutdownAsync();

调度模式

// CRON: 在指定的时间点触发
.SetCron("0 9 * * 1-5")     // 工作日早 9 点

// FixedRate: 按固定速率触发(不管上次是否完成)
.SetFixedRate(TimeSpan.FromMinutes(1))

// FixedDelay: 上次完成后等 N 秒再触发
.SetFixedDelay(TimeSpan.FromSeconds(30))

事件监听

scheduler.OnStarting += (s, e) => Console.WriteLine($"任务 {e.JobName} 开始");
scheduler.OnCompleted += (s, e) => Console.WriteLine($"任务 {e.JobName} 完成");
scheduler.OnError += (s, e) => Console.WriteLine($"任务 {e.JobName} 出错");
scheduler.OnSkipped += (s, e) => Console.WriteLine($"任务 {e.JobName} 被跳过");

执行统计

var stats = scheduler.GetAllStats();
foreach (var s in stats)
{
    Console.WriteLine($"{s.JobName}: 成功率 {s.SuccessRate:P}");
    Console.WriteLine($"  平均耗时 {s.AverageDuration.TotalMilliseconds:F1}ms");
}

动态修改

// 运行时修改 CRON 表达式
scheduler.UpdateCron("OrderSync", "0 */10 * * *");

// 运行时修改间隔
scheduler.UpdateInterval("CacheRefresh", TimeSpan.FromMinutes(1));

重试配置

.SetRetry(5, TimeSpan.FromSeconds(2), exponentialBackoff: true)
.AddRetryableException<TimeoutException>(shouldRetry: true)
.AddRetryableException<ArgumentException>(shouldRetry: false) // 参数错误不重试

日历过滤

// 使用 HolidayCalendar 跳过节假日
var calendar = HolidayCalendar.Load("holidays.json");
.SetCalendarFilter(calendar.ToWorkdayFilter())

// 自定义过滤
.SetCalendarFilter(dt => dt.DayOfWeek != DayOfWeek.Sunday)

CRON 表达式快捷生成

CronBuilder.EveryDayAt(9, 0)        // "0 9 * * *"
CronBuilder.EveryWeekdayAt(10, 0)   // "0 10 * * 1-5"
CronBuilder.Every5Minutes()          // "*/5 * * * *"
CronBuilder.LastDayOfMonth(23, 59)   // "59 23 L * ?"

// 流式构建
CronBuilder.Create()
    .AtMinute(0).AtHour(8, 12, 16)
    .OnWeekdays()
    .Build()                         // "0 8,12,16 * * 1-5"

HolidayCalendar

{
  "holidays": [
    { "date": "2025-01-01", "name": "元旦" },
    { "date": "2025-10-01", "name": "国庆节" }
  ],
  "workdays": ["2025-01-26", "2025-02-08"]
}
// 加载
var calendar = HolidayCalendar.Load("holidays.json");

// 或代码添加
var calendar = new HolidayCalendar()
    .AddHoliday(DateTime.Today, "测试假日")
    .AddWorkday(new DateTime(2025, 6, 28));

// 查询
bool isHoliday = calendar.IsHoliday(DateTime.Today);
bool isWorkday = calendar.IsWorkday(DateTime.Today);  // 调休日优先

// 安全加载(不抛异常)
if (HolidayCalendar.TryLoad("bad.json", out var cal, out var error))
    Console.WriteLine("加载成功");
else
    Console.WriteLine($"加载失败: {error}");

// 冲突检测(holidays 和 workdays 都有同一日期)
var conflicts = calendar.GetConflicts(); // 調休规则下视为工作日

日志

调度器内置独立日志系统,不依赖 LogHelper。格式:

[2025-06-17 14:30:00.123] [Schedule] [INFO] 任务 [OrderSync] 开始执行 (第 5 次)

目录结构:LogPath/yyyy-MM/MM-dd/Schedule.log

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  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 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework 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 (1)

Showing the top 1 NuGet packages that depend on VassasCo.Utility.ScheduleHelper:

Package Downloads
VassasCo.Utility

C# 桌面开发工具库 (WinForm / WPF / Avalonia): - ConfigHelper:零代码实体类⇋JSON/XML配置双向映射(带注释、热重载、原子保存、列表展开) - LogHelper:异步高性能日志系统(建造者模式、异步队列、自动清理、14种日志类型) - CrashDumpHelper:崩溃捕获+FirstChance异常追踪+MiniDump生成 - SnowflakeIdHelper:分布式雪花ID生成器(单调时钟杜绝回拨、集群WorkerId分配、ID反解) - ScheduleHelper:全能定时任务调度器(CRON/固定速率/固定延迟、重试退避、超时取消、日历过滤、线程池、优雅关闭) - RetryHelper:智能重试器(指数退避+抖动+断路器三态+降级+超时,同步/异步) - EventBus:进程内事件总线(类型发布/订阅、特性自动注册、优先级、条件过滤、粘性事件、多通道),支持 .NET Standard 2.0 / .NET 6 / .NET 8 / .NET 9 / .NET 10 - ExcelMapper:原生对象→Excel映射(嵌套类子表头合并、数组独立Sheet、Dictionary自适应、全可配置样式)

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 284 6/18/2026