MonkeyScheduler 0.0.1.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package MonkeyScheduler --version 0.0.1.5
                    
NuGet\Install-Package MonkeyScheduler -Version 0.0.1.5
                    
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="MonkeyScheduler" Version="0.0.1.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MonkeyScheduler" Version="0.0.1.5" />
                    
Directory.Packages.props
<PackageReference Include="MonkeyScheduler" />
                    
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 MonkeyScheduler --version 0.0.1.5
                    
#r "nuget: MonkeyScheduler, 0.0.1.5"
                    
#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 MonkeyScheduler@0.0.1.5
                    
#: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=MonkeyScheduler&version=0.0.1.5
                    
Install as a Cake Addin
#tool nuget:?package=MonkeyScheduler&version=0.0.1.5
                    
Install as a Cake Tool

MonkeyScheduler

一个简单的分布式任务调度系统,支持基于 CRON 表达式的定时任务调度。

功能特点

  • 基于 CRON 表达式的任务调度
  • 支持秒级和分钟级调度
  • 可扩展的任务执行器
  • 可自定义的任务存储
  • 任务执行日志记录
  • 支持任务启用/禁用
  • 异步日志记录
  • 支持多种日志级别(INFO、WARNING、ERROR)
  • 灵活的日志格式化
  • 自动日志清理策略
  • SQLite 存储后端
  • 高性能设计

系统要求

  • .NET 8.0 或更高版本

项目结构

解决方案包含以下三个项目:

  1. MonkeyScheduler:核心库项目,包含调度系统的所有核心功能实现。
  2. MonkeyScheduler.Tests:单元测试项目,包含所有测试用例。
  3. MonkeyScheduler.Sample:示例项目,展示如何使用MonkeyScheduler库。

安装

dotnet add package MonkeyScheduler
dotnet add package System.Data.SQLite

CRON 表达式格式

MonkeyScheduler 支持两种 CRON 表达式格式:

  1. 标准 5 字段格式(分钟 时 日 月 周):

    */5 * * * *     # 每5分钟执行一次
    0 */2 * * *     # 每2小时执行一次
    0 0 * * *       # 每天午夜执行
    
  2. 扩展 6 字段格式(秒 分 时 日 月 周):

    */5 * * * * *   # 每5秒执行一次
    0 */30 * * * *  # 每30秒执行一次
    

快速开始

using MonkeyScheduler.Core;
using MonkeyScheduler.Core.Models;
using MonkeyScheduler.Core.Services;
using MonkeyScheduler.Storage;
using MonkeyScheduler.Logging;

// 创建日志记录器
var logger = new Logger();

// 创建任务存储
var repo = new InMemoryTaskRepository();

// 创建任务执行器
var executor = new CustomTaskExecutor();

// 创建调度器
var scheduler = new Scheduler(repo, executor);

// 添加任务
repo.AddTask(new ScheduledTask
{
    Name = "示例任务",
    CronExpression = "*/5 * * * * *", // 每5秒执行一次
    NextRunTime = DateTime.UtcNow
});

// 记录系统启动日志
await logger.LogInfoAsync("调度系统启动");

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

// 停止调度器
await logger.LogInfoAsync("调度系统停止");
scheduler.Stop();

日志记录功能

基本使用

// 创建默认日志记录器
var logger = new Logger();

// 记录不同级别的日志
await logger.LogInfoAsync("系统启动");
await logger.LogWarningAsync("内存使用率较高");
await logger.LogErrorAsync("发生错误", new Exception("测试异常"));

自定义配置

// 自定义数据库路径和清理策略
var logger = new Logger(
    dbPath: "C:\\Logs\\monkey_scheduler.db",
    maxLogCount: 5000,          // 最多保留5000条日志
    maxLogAge: TimeSpan.FromDays(7)  // 保留最近7天的日志
);

// 自定义日志格式
var formatter = new DefaultLogFormatter(
    format: "{timestamp} [{level}] {message}",
    includeTimestamp: true,
    includeException: true
);
var customLogger = new Logger(formatter: formatter);

日志清理

// 手动执行清理
await logger.CleanupLogsAsync();

// 监控日志状态
var count = await logger.GetLogCountAsync();
var oldestDate = await logger.GetOldestLogDateAsync();

自定义任务执行器

public class CustomTaskExecutor : ITaskExecutor
{
    private readonly ILogger _logger;

    public CustomTaskExecutor(ILogger logger)
    {
        _logger = logger;
    }

    public async Task ExecuteAsync(ScheduledTask task)
    {
        try
        {
            await _logger.LogInfoAsync($"开始执行任务: {task.Name}");
            // 实现自定义的任务执行逻辑
            await Task.CompletedTask;
            await _logger.LogInfoAsync($"任务执行完成: {task.Name}");
        }
        catch (Exception ex)
        {
            await _logger.LogErrorAsync($"任务执行失败: {task.Name}", ex);
            throw;
        }
    }
}
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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on MonkeyScheduler:

Package Downloads
MonkeyScheduler.WorkerService

工作节点服务项目,负责实际执行任务

MonkeyScheduler.SchedulerService

调度服务项目

MonkeyScheduler.Data.MySQL

MonkeyScheduler MySQL数据存储服务

GitHub repositories

This package is not used by any popular GitHub repositories.

0.0.1.5正式版,目前仅支持单节点,暂时不支持分布式,目前版本适用于小型项目