Lunarium.Scheduler 1.3.0

dotnet add package Lunarium.Scheduler --version 1.3.0
                    
NuGet\Install-Package Lunarium.Scheduler -Version 1.3.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="Lunarium.Scheduler" Version="1.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lunarium.Scheduler" Version="1.3.0" />
                    
Directory.Packages.props
<PackageReference Include="Lunarium.Scheduler" />
                    
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 Lunarium.Scheduler --version 1.3.0
                    
#r "nuget: Lunarium.Scheduler, 1.3.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 Lunarium.Scheduler@1.3.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=Lunarium.Scheduler&version=1.3.0
                    
Install as a Cake Addin
#tool nuget:?package=Lunarium.Scheduler&version=1.3.0
                    
Install as a Cake Tool

Lunarium.Scheduler

一个轻量级、高性能的 .NET 定时任务调度器,基于 Cron 表达式实现,原生集成依赖注入 (DI) 容器。

🎯 设计初衷

相比于市面上的其他定时任务库(如 Quartz.NET、Hangfire),Lunarium.Scheduler 专为 DI 容器内使用 而设计,完美适配 任意 DI 架构——包括在 IHostedService 内部自建私有容器的嵌套场景。

核心优势

🪶 轻量级

  • 零配置文件,零外部依赖(除 Cronos)
  • 不需要数据库、不需要持久化
  • 整个调度器只有几个核心类,代码简洁易懂

⚡ 高性能

  • 基于 Task.Delay 的调度循环,开销极小
  • 每个任务有独立的调度循环,互不阻塞
  • 内存占用低,适合长时间运行

🔧 简单易配置

  • API 设计简洁,几行代码即可完成配置
  • 标准 Cron 表达式 + 语义化的便捷扩展方法,学习成本低
  • 无需复杂的配置文件或数据库表

💉 DI 原生集成

  • 可在任意 DI 容器内注册和管理,而不是只能在主机级别使用
  • 任务通过 DI 容器创建,自动解析依赖
  • 完美适配 IHostedService 内部的私有容器场景

典型应用场景

本调度器专为以下场景设计:

// ❌ 其他库通常需要在主机级别配置
builder.Services.AddQuartz(...);  // 全局配置,所有服务共享

// ✅ LunariumScheduler 可以在服务类的私有容器内独立管理
public class MyModuleService : IHostedService
{
    private readonly IServiceProvider _privateServices;
    private readonly LunariumScheduler _scheduler;

    public MyModuleService(ILogger<MyModuleService> logger)
    {
        // 每个服务类都可以有自己的 DI 容器和调度器
        _privateServices = new ServiceCollection()
            .AddSingleton<FullSyncJob>()
            .AddSingleton<AnalysisJob>()
            .AddSingleton<LunariumScheduler>(provider =>
                new LunariumScheduler(provider, logger))
            .BuildServiceProvider();

        _scheduler = _privateServices.GetRequiredService<LunariumScheduler>();
        _scheduler.ScheduleAsync<FullSyncJob>("0 4 * * 2");
        _scheduler.Start();
    }
}

这种设计使得:

  • 每个服务/模块可以有自己的任务调度器,互不干扰
  • 任务的依赖可以限定在服务内部,不会污染全局容器
  • 服务的启动/停止完全独立,符合模块化架构思想

✨ 特性

  • 🎯 Cron 表达式支持:使用标准 Cron 表达式定义任务执行时间
  • 🔧 便捷扩展方法:ScheduleDaily / ScheduleWeekly / ScheduleMonthly / ScheduleInterval / ScheduleHourly,支持链式调用
  • 💉 DI 原生集成:任务通过依赖注入容器创建,自动解析依赖
  • ⚡ 异步/同步双模式:同时支持同步 (IJob) 和异步 (IAsyncJob) 任务
  • 🌍 时区感知:支持为每个任务指定独立的时区
  • 🔒 并发保护:内置任务执行状态管理,防止重复执行
  • 🧱 组内互斥与优先级:同名 Group 的任务不会同时执行,冲突时可按优先级选择跳过 / 等待 / 忽略
  • 📊 任务监控:提供不可变强类型快照(GetJob / GetJobs,返回 JobInfo)与格式化文本输出(含执行历史统计)
  • 🧭 执行上下文:任务在执行期间可通过 JobExecutionContext.Current 读取自身信息与下次执行时间,无需改接口签名
  • 🛡️ 异常隔离:单个任务失败不影响其他任务的调度
  • 📝 完整日志:集成 Microsoft.Extensions.Logging,记录任务执行详情
  • 🕐 异步释放:DisposeAsync 等待所有任务完成后释放资源
  • 🖥️ 执行历史:保留最近 10 次执行记录,含耗时、成功/失败状态
  • 🧩 多目标框架:同时提供 net8.0 / net9.0 / net10.0 资产,按目标框架匹配 Microsoft.Extensions.* 主版本

📦 安装

通过 NuGet 包或项目引用引入:

dotnet add package Lunarium.Scheduler

或使用项目引用:

<ProjectReference Include="../Lunarium.Scheduler/Lunarium.Scheduler.csproj" />

🚀 快速开始

1. 定义任务

创建一个实现 IJob 或 IAsyncJob 接口的任务类:

using Lunarium.Scheduler.Interfaces;

// 同步任务
public class DailyReportJob : IJob
{
    private readonly ILogger<DailyReportJob> _logger;
    private readonly IDataService _dataService;

    public DailyReportJob(ILogger<DailyReportJob> logger, IDataService dataService)
    {
        _logger = logger;
        _dataService = dataService;
    }

    public void Execute()
    {
        _logger.LogInformation("正在生成每日报告...");
        var report = _dataService.GenerateReport();
        // 处理报告...
    }
}

// 异步任务
public class DataSyncJob : IAsyncJob
{
    private readonly IApiClient _apiClient;

    public DataSyncJob(IApiClient apiClient)
    {
        _apiClient = apiClient;
    }

    public async Task ExecuteAsync()
    {
        await _apiClient.SyncDataAsync();
    }
}

2. 创建调度器并注册任务

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Lunarium.Scheduler;

// 创建服务容器并注册任务
var services = new ServiceCollection()
    .AddLogging(builder => builder.AddConsole())
    .AddScoped<DailyReportJob>()
    .AddScoped<DataSyncJob>()
    .BuildServiceProvider();

// 创建调度器
var logger = services.GetRequiredService<ILogger<LunariumScheduler>>();
var scheduler = new LunariumScheduler(services, logger);

// 使用便捷方法注册任务(支持链式调用)
var tokyoTz = TimeZoneInfo.FindSystemTimeZoneById("Asia/Tokyo");
scheduler
    .ScheduleDaily<DailyReportJob>(hour: 4, name: "每日报告")
    .ScheduleWeeklyAsync<DataSyncJob>(DayOfWeek.Tuesday, hour: 9, name: "每周同步", timeZone: tokyoTz)
    .ScheduleInterval<HeartbeatJob>(intervalMinutes: 5, name: "心跳检查");

// 启动调度器
scheduler.Start();

3. 停止与释放

// 停止调度器(取消所有调度循环)
scheduler.Stop();

// 异步释放资源(等待所有任务执行完成)
await scheduler.DisposeAsync();

完整的可运行示例请参阅 Example.md。

📖 详细用法

Cron 表达式格式

调度器使用标准的 5 位 Cron 表达式:

┌───────────── 分钟 (0 - 59)
│ ┌───────────── 小时 (0 - 23)
│ │ ┌───────────── 日期 (1 - 31)
│ │ │ ┌───────────── 月份 (1 - 12)
│ │ │ │ ┌───────────── 星期 (0 - 6) (0 = 星期日)
│ │ │ │ │
* * * * *

常用示例:

Cron 表达式 说明
0 * * * * 每小时整点执行
*/5 * * * * 每 5 分钟执行一次
0 9 * * * 每天 9:00 执行
0 9 * * 1 每周一 9:00 执行
0 0 1 * * 每月 1 号 0:00 执行
30 8 * * 1-5 工作日 8:30 执行
0 */2 * * * 每 2 小时执行一次

便捷扩展方法

无需手写 Cron 表达式,使用语义化方法即可完成常见调度:

方法 说明 Cron 等效
ScheduleDaily<TJob>(hour, minute) 每天执行 minute hour * * *
ScheduleWeekly<TJob>(dayOfWeek, hour, minute) 每周执行 minute hour * * n
ScheduleMonthly<TJob>(day, hour, minute) 每月执行 minute hour day * *
ScheduleInterval<TJob>(intervalMinutes) 按分钟间隔执行(1-59) */n * * * *
ScheduleHourly<TJob>(minute) 每小时执行 minute * * * *

所有方法均有对应的异步版本(ScheduleDailyAsync / ScheduleWeeklyAsync / ScheduleMonthlyAsync / ScheduleIntervalAsync / ScheduleHourlyAsync),且都返回 LunariumScheduler,支持 Fluent 链式调用:

var scheduler = new LunariumScheduler(services, logger)
    .ScheduleDaily<DailyReportJob>(hour: 4)
    .ScheduleWeekly<WeeklyReviewJob>(DayOfWeek.Tuesday, hour: 9)
    .ScheduleInterval<HeartbeatJob>(intervalMinutes: 5);

scheduler.Start();

原始 Cron 表达式

复杂的调度规则仍然可以使用原始方法:

// 同步任务
scheduler.Schedule<ComplexJob>("0 0,12 1 */2 *", name: "复杂任务");

// 异步任务
scheduler.ScheduleAsync<ComplexAsyncJob>("30 8 * * 1-5", name: "工作日任务");

时区支持

每个任务可以指定独立的时区:

// 使用 UTC(默认)
scheduler.Schedule<JobA>("0 12 * * *");

// 使用东京时区(Linux/macOS 使用 IANA 时区 ID)
var tokyoTz = TimeZoneInfo.FindSystemTimeZoneById("Asia/Tokyo");
scheduler.Schedule<JobB>("0 12 * * *", timeZone: tokyoTz);

// 使用纽约时区
var nyTz = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
scheduler.Schedule<JobC>("0 12 * * *", timeZone: nyTz);

注意: Cron 表达式中的时间将按照指定时区解释。

任务管理

// 移除任务(按类型,仅限同步任务 IJob)
scheduler.Remove<DailyReportJob>();

// 移除任务(按名称,同步/异步任务均可)
scheduler.Remove("每日报告");

// 查询单个任务信息(按类型,仅限同步任务 IJob)
string? info = scheduler.GetJobInfo<DailyReportJob>();
Console.WriteLine(info);

// 查询单个任务信息(按名称)
string? infoByName = scheduler.GetJobInfo("每日报告");
Console.WriteLine(infoByName);

// 查询所有任务
string allInfo = scheduler.GetAllJobsInfo();
Console.WriteLine(allInfo);

任务信息输出示例

==================== 调度器信息 ====================
调度器状态: 运行中
已注册任务数: 2

-------------------- 任务 [1] --------------------
任务名称: 每日报告
任务类型: DailyReportJob
Cron 表达式: 0 9 * * *
任务模式: 同步
时区: UTC (Coordinated Universal Time)
下次执行 (UTC): 2025-10-31 09:00:00 +00:00
下次执行 (本地): 2025-10-31 09:00:00 +00:00
上次执行 (UTC): 2025-10-30 09:00:00 +00:00
上次执行 (本地): 2025-10-30 09:00:00 +00:00
运行状态: 已调度
是否正在执行: 否
执行历史记录数: 3
成功次数: 3
失败次数: 0
最近一次执行:
  时间: 2025-10-30 09:00:00 +00:00
  结果: 成功
  耗时: 234.56ms

-------------------- 任务 [2] --------------------
任务名称: 数据同步
任务类型: DataSyncJob
Cron 表达式: 0 * * * *
任务模式: 异步
时区: Asia/Tokyo (Tokyo Standard Time)
下次执行 (UTC): 2025-10-30 15:00:00 +00:00
下次执行 (本地): 2025-10-31 00:00:00 +09:00
上次执行: 从未执行
运行状态: 已调度
是否正在执行: 否
执行历史: 无

====================================================

强类型任务信息(JobInfo)

需要做健康检查、指标上报或管理后台时,不必解析上面的文本,直接用强类型快照:

成员 返回类型 说明
scheduler.IsRunning bool 调度器是否已启动
scheduler.GetJobs() IReadOnlyList<JobInfo> 所有任务快照,按注册顺序;为空时返回空集合
scheduler.GetJob<TJob>() JobInfo? 按类型查询,同步/异步任务均可
scheduler.GetJob(string name) JobInfo? 按名称查询

GetJobInfo<TJob>() 受 where TJob : IJob 限制,只能查同步任务;需要查异步任务时请用 GetJob<TJob>()。

JobInfo 是不可变快照,与调度器内部状态完全隔离(执行历史是深拷贝,修改副本不会影响调度器):

var job = scheduler.GetJob("每日报告");
if (job is null)
{
    return;
}

Console.WriteLine($"类型: {job.JobType.Name}, 模式: {job.ExecutionMode}, Cron: {job.Cron}");
Console.WriteLine($"下次执行: {job.NextRun:O}, 上次执行: {job.LastRun:O}");
Console.WriteLine($"已调度: {job.IsScheduled}, 正在执行: {job.IsExecuting}");
Console.WriteLine($"跳过 {job.SkippedCount} 次, 最近原因: {job.LastSkipReason ?? "-"}");
Console.WriteLine($"成功 {job.SuccessCount} / 失败 {job.FailureCount}(保留窗口内)");

// 互斥组配置(未参与互斥组时为 null)
if (job.Group is { } group)
{
    Console.WriteLine($"组 {group.Name}, 优先级 {group.Priority}, 策略 {group.BusyPolicy}");
}

foreach (var record in job.History)
{
    Console.WriteLine($"{record.ExecutedAt:O} {(record.Success ? "成功" : record.ErrorMessage)} {record.Duration.TotalMilliseconds:0.00}ms");
}

字段语义:

  • NextRun / LastRun / LastSkippedAt 都是 UTC 瞬时;需要本地时间用 TimeZoneInfo.ConvertTime(x, job.TimeZone) 转换
  • SuccessCount / FailureCount / History 只覆盖保留窗口(最多 10 次),不是自注册以来的累计值
  • Cron 保留注册时的原始字符串(Cronos 的 CronExpression.ToString() 会返回规范化后的内部表示,两者可能不同)
  • 快照在调度器锁内一次性采集,GetJobs() 返回的各个任务处于同一时刻的一致状态
  • JobInfo.ToString() 的输出与 GetJobInfo(...) 完全一致,旧代码无需改动

执行历史

调度器保留每个任务最近 10 次执行记录:

// 通过任务信息查询(包含历史统计)
var info = scheduler.GetJobInfo("每日报告");
Console.WriteLine(info);

// 输出会包含:
// 执行历史记录数: 10
// 成功次数: 9
// 失败次数: 1
// 最近一次执行:
//   时间: 2025-11-05 04:00:00 +09:00
//   结果: 成功
//   耗时: 1234.56ms

🔧 高级特性

任务执行上下文

任务在执行期间(包括构造函数内)可以通过 JobExecutionContext.Current 读到本次执行的信息, IJob / IAsyncJob 的签名保持不变:

public class ReportJob : IAsyncJob
{
    public async Task ExecuteAsync()
    {
        var ctx = JobExecutionContext.Current;
        if (ctx is null)
        {
            return; // 不是由调度器调用(例如单元测试里直接 new)
        }

        Console.WriteLine($"任务: {ctx.Name}");
        Console.WriteLine($"本次计划时间: {ctx.ScheduledFor:O}");
        Console.WriteLine($"调度延迟: {(DateTimeOffset.UtcNow - ctx.ScheduledFor).TotalMilliseconds:0}ms");
        Console.WriteLine($"下次执行: {ctx.Job.NextRun:O}");

        await Task.Delay(1000);

        // 上下文随 await 流动,这里依然可读
        Console.WriteLine($"仍在执行的任务: {JobExecutionContext.Current?.Name}");
    }
}

语义:

  • ctx.Job 是执行开始时采集的 JobInfo 快照,IsExecuting 为 true,NextRun 已推进到本次之后的下一次执行时间
  • ctx.ScheduledFor 是 Cron 命中的计划时刻(UTC),和 JobInfo.LastRun 不同 —— 后者是实际完成时间
  • 上下文基于 AsyncLocal,会随 await、Task.Run 流向派生任务;本次执行结束后失效,不会泄漏给下一次执行或其它任务。 需要跨执行保留请自行保存字段
  • 未经过调度器执行(例如测试中直接 new 任务并调用 Execute())时 Current 为 null

并发控制

调度器内置了任务执行状态管理。如果某个任务仍在执行中,调度器会自动跳过下一次调度:

public class LongRunningJob : IAsyncJob
{
    public async Task ExecuteAsync()
    {
        // 这个任务可能需要 10 分钟才能完成
        await SomeLongOperation();
    }
}

// 每 5 分钟调度一次
// 但如果任务还在运行,下一次调度会被跳过
scheduler.ScheduleIntervalAsync<LongRunningJob>(intervalMinutes: 5);

日志输出:

[警告] 任务 LongRunningJob 仍在执行中, 跳过本次调度

优先级与互斥组

默认情况下所有任务完全并发执行,彼此不影响。给任务设置相同的 Group 后,同组任务不会同时执行; Priority 决定冲突时谁说了算,BusyPolicy 决定被挡住时怎么办。

策略 组被同/更高优先级占用时 典型场景
JobBusyPolicy.Ignore(默认) 不占门,直接并发执行 与旧版本行为一致
JobBusyPolicy.Skip 跳过本次调度 报表、对账,错过一次无所谓
JobBusyPolicy.Wait 排队等待,空闲后执行 必须执行,可以延后
scheduler.Schedule<FullSyncJob>("0 * * * *", new JobOptions
{
    Name = "全量同步",
    Group = "sync",                          // 同一组的任务互斥
    Priority = 100,                          // 数值越大优先级越高
    BusyPolicy = JobBusyPolicy.Ignore
});

scheduler.ScheduleAsync<IncrementalSyncJob>("*/5 * * * *", new JobOptions
{
    Name = "增量同步",
    Group = "sync",
    Priority = 0,
    BusyPolicy = JobBusyPolicy.Skip,         // 全量同步在跑时直接跳过
    WaitTimeout = TimeSpan.FromMinutes(1)    // 仅 Wait 策略使用
});

规则:

  • 组空闲 → 直接执行并占用互斥门
  • 组被占用,但自己的优先级严格高于组内所有正在执行的任务 → 依然执行(高优先级不被低优先级挡住)
  • 否则按 BusyPolicy 处理:Skip 跳过、Wait 排队、Ignore 直接并发执行
  • Wait 按优先级从高到低排队,同优先级先到先得;WaitTimeout 为 null 时一直等待
  • 跳过次数与原因可通过 scheduler.GetJob(name)?.SkippedCount / LastSkippedAt / LastSkipReason 查看,也会出现在 GetJobInfo 的输出中

⚠️ 优先级只影响“能否进入互斥组”,不会中断正在执行的任务——.NET 无法强制终止运行中的 Task, IAsyncJob 目前也不接收 CancellationToken。

💡 便捷扩展方法(ScheduleDaily / ScheduleIntervalAsync 等)暂不支持 JobOptions,需要互斥时请使用 Schedule / ScheduleAsync。

动态任务注册

调度器启动后,仍可以动态添加新任务:

// 启动调度器
scheduler.Start();

// 运行中动态添加任务
scheduler.Schedule<NewJob>("0 * * * *");
// 新任务会立即被调度

异常处理

任务执行中的异常会被自动捕获和记录,不会影响其他任务:

public class FaultyJob : IJob
{
    public void Execute()
    {
        throw new Exception("出错了!");
    }
}

日志输出:

[错误] 任务执行失败: FaultyJob,耗时: 0.52ms
System.Exception: 出错了!
   at FaultyJob.Execute()

调度器会继续为该任务安排下一次执行。

📚 完整示例

完整的可运行示例(控制台应用、主机级别集成、私有容器集成)请参阅 Example.md。

以下为在 IHostedService 私有容器中使用的最小示例:

public class ModuleService : IHostedService
{
    private readonly IServiceProvider _privateServices;
    private readonly LunariumScheduler _scheduler;

    public ModuleService(ILoggerFactory loggerFactory)
    {
        // ========== 构建私有 DI 容器 ==========
        _privateServices = new ServiceCollection()
            .AddSingleton<IDatabaseService, DatabaseService>()
            .AddSingleton<IAnalysisService, AnalysisService>()

            // ========== 定时任务 ==========
            .AddSingleton<AnalysisJob>()      // 分析任务
            .AddSingleton<SyncJob>()          // 同步任务

            // ========== 调度器 ==========
            .AddSingleton<LunariumScheduler>(provider =>
                new LunariumScheduler(
                    provider,
                    loggerFactory.CreateLogger<LunariumScheduler>()
                ))

            .BuildServiceProvider();

        // 解析调度器
        _scheduler = _privateServices.GetRequiredService<LunariumScheduler>();
    }

    public Task StartAsync(CancellationToken cancellationToken)
    {
        // 配置时区
        var timeZone = TimeZoneInfo.FindSystemTimeZoneById("Asia/Shanghai");

        // 注册任务
        _scheduler.ScheduleAsync<SyncJob>(
            cronExpression: "0 4 * * 2",    // 每周二 4:00
            name: "数据同步",
            timeZone: timeZone
        );

        _scheduler.ScheduleAsync<AnalysisJob>(
            cronExpression: "0 4 * * *",    // 每天 4:00
            name: "每日分析",
            timeZone: timeZone
        );

        // 启动调度器
        _scheduler.Start();

        Console.WriteLine(_scheduler.GetAllJobsInfo());

        return Task.CompletedTask;
    }

    public async Task StopAsync(CancellationToken cancellationToken)
    {
        // 停止调度器并等待所有任务完成
        _scheduler.Stop();
        await _scheduler.DisposeAsync();

        // 释放私有容器
        if (_privateServices is IDisposable disposable)
        {
            disposable.Dispose();
        }
    }
}

🆚 与其他库的对比

特性 LunariumScheduler Quartz.NET Hangfire
轻量级 ✅ 零配置,无外部依赖 ❌ 需要配置文件 ❌ 需要数据库
私有容器支持 ✅ 完美支持 ⚠️ 需要特殊配置 ❌ 仅支持全局
学习成本 ✅ 极低 ⚠️ 中等 ⚠️ 较高
内存占用 ✅ 极小 ⚠️ 中等 ❌ 较大
集群支持 ❌ 不支持 ✅ 支持 ✅ 支持
持久化 ❌ 不支持 ✅ 支持 ✅ 支持
Web UI ❌ 无 ⚠️ 第三方 ✅ 内置

结论: 如果你需要分布式任务调度、持久化或 Web 管理界面,请选择 Quartz.NET 或 Hangfire。如果你只需要简单、轻量、高性能的进程内任务调度,LunariumScheduler 是最佳选择。

⚠️ 注意事项

  1. 任务类必须注册到 DI 容器:调度器通过 DI 容器获取任务实例,忘记注册会导致运行时错误。

  2. 私有容器的作用域:在私有容器中使用时,任务的依赖也应该注册在同一个容器中,否则会出现依赖解析失败。

    // ✅ 正确:任务和依赖都在私有容器中
    _privateServices = new ServiceCollection()
        .AddSingleton<DatabaseService>()
        .AddSingleton<MyJob>()  // MyJob 依赖 DatabaseService
        .AddSingleton<LunariumScheduler>()
        .BuildServiceProvider();
    
    // ❌ 错误:MyJob 依赖的服务未注册
    _privateServices = new ServiceCollection()
        .AddSingleton<MyJob>()  // 缺少 DatabaseService
        .AddSingleton<LunariumScheduler>()
        .BuildServiceProvider();
    
  3. 任务应该是轻量级的:每次执行都会创建新的作用域 (IServiceScope),避免在任务构造函数中执行耗时操作。

  4. 时区陷阱:注意夏令时 (DST) 的影响,在时区切换时可能会跳过或重复执行任务。Linux/macOS 请使用 IANA 时区 ID(如 Asia/Tokyo),Windows 可使用 Tokyo Standard Time 等 ID。

  5. 异步任务不阻塞调度:异步任务会在后台执行,不会阻塞下一次调度的计算。

  6. 线程安全:调度器的所有公共方法都是线程安全的。

  7. 资源释放:使用完毕后应调用 DisposeAsync() 释放资源,尤其是在服务停止时。同步的 Dispose() 已被移除。

    public async Task StopAsync(CancellationToken cancellationToken)
    {
        _scheduler.Stop();
        await _scheduler.DisposeAsync();  // ✅ 等待任务完成并释放资源
    
        if (_privateServices is IDisposable disposable)
        {
            disposable.Dispose();  // ✅ 释放容器资源
        }
    }
    
  8. 避免在主机和私有容器中重复注册:如果使用私有容器模式,不要将调度器注册到主机容器,这会导致两个独立的调度器实例。

  9. 不允许重复注册:同一任务类型重复注册会抛出 ArgumentException(fail-fast);不同类型的任务使用了相同名称时,后注册的会被忽略并记录警告日志。

  10. 互斥组需要显式设置:只有 Group 相同的任务才会互斥;未设置组时 Priority 与 BusyPolicy 都不会生效(注册时会输出警告日志)。

  11. Wait 策略建议设置超时:WaitTimeout 默认为 null(一直等待),仅靠调度器停止或任务移除时的取消来解除。组内任务可能长时间时,应显式设置超时。

  12. 优先级不会抢占:高优先级任务只能与低优先级任务并发执行,不能中断它;Stop() 也不会取消正在执行的任务。

  13. 升级提示:新增 Schedule(cron, JobOptions) / ScheduleAsync(cron, JobOptions) 重载后,Schedule<TJob>(cron, null) 这种把 null 当第二个参数的写法会产生重载歧义,需改为具名参数或省略该参数(如 Schedule<TJob>(cron) 或 Schedule<TJob>(cron, timeZone: tz))。

🔗 依赖项

  • .NET 8.0 / 9.0 / 10.0(多目标框架,与 Lunarium.Logging 一致)
  • Cronos (v0.11.1):用于解析 Cron 表达式
  • Microsoft.Extensions.Logging.Abstractions:日志抽象,按目标框架匹配主版本(net8.0 → 8.0.x,net9.0 → 9.0.x,net10.0 → 10.0.x)

📄 许可证

本项目采用 Apache License 2.0。


如有问题或建议,欢迎提交 Issue 或 Pull Request。

Product Compatible and additional computed target framework versions.
.NET 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 is compatible.  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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.3.0 105 11/6/2026
1.2.0 110 10/28/2026
1.1.0 117 10/14/2026
1.0.0 135 8/28/2026