Skip to content

jeanlrnt/MiniCron.Core

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MiniCron.Core

CI Publish

Release Release

NuGet NuGet downloads dotnet

Open issues Contributors License Last commit

MiniCron.Core is a lightweight and simple library for scheduling background tasks in .NET applications using Cron expressions. It integrates seamlessly with the Microsoft.Extensions.DependencyInjection framework.

Features

  • Simple & Lightweight: Minimal setup required.
  • Dependency Injection: Access your services directly within your scheduled jobs.
  • Async Support: Fully supports asynchronous tasks.
  • Cancellation Support: Handles cancellation tokens gracefully.

Installation

Install the package via NuGet:

dotnet add package MiniCron.Core

Usage

1. Register MiniCron

In your Program.cs (or Startup.cs), register MiniCron services and define your jobs:

using MiniCron.Core.Extensions;

var builder = WebApplication.CreateBuilder(args);

// Register MiniCron
builder.Services.AddMiniCron(options =>
{
    // Run every minute
    options.AddJob("* * * * *", async (serviceProvider, cancellationToken) =>
    {
        Console.WriteLine($"Job executed at {DateTime.Now}");
        await Task.CompletedTask;
    });

    // Run every 5 minutes
    options.AddJob("*/5 * * * *", async (serviceProvider, cancellationToken) =>
    {
        // Resolve services from the DI container
        var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
        logger.LogInformation("5-minute job executed.");
        
        await Task.Delay(100, cancellationToken);
    });
});

var app = builder.Build();
app.Run();

2. Using Scoped Services

Since the background service runs as a Singleton, you should create a scope to resolve Scoped services (like EF Core DbContexts):

builder.Services.AddMiniCron(options =>
{
    options.AddJob("0 12 * * *", async (serviceProvider, cancellationToken) =>
    {
        using var scope = serviceProvider.CreateScope();
        var myScopedService = scope.ServiceProvider.GetRequiredService<IMyScopedService>();
        
        await myScopedService.DoWorkAsync(cancellationToken);
    });
});

Cron Expressions

MiniCron supports standard cron expressions with 5 fields:

* * * * *
| | | | |
| | | | +----- Day of Week (0 - 6) (Sunday=0)
| | | +------- Month (1 - 12)
| | +--------- Day of Month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)

Examples:

  • * * * * *: Every minute
  • */5 * * * *: Every 5 minutes
  • 0 0 * * *: Every day at midnight
  • 0 12 * * 1: Every Monday at 12:00 PM

Documentation

Comprehensive guides and examples are available to help you integrate MiniCron.Core into your projects:

Examples

  • Console Application Example - Learn how to integrate MiniCron.Core into a minimal .NET Console application with step-by-step instructions and working examples.
  • Web Application Example - Complete guide for ASP.NET Core applications including Minimal API, MVC, Web API, and Blazor Server examples with Entity Framework Core integration.

Guides

  • Configuration Guide - Comprehensive information about configuring MiniCron.Core, including configuration sources, environment-specific settings, and a complete cron expression reference.
  • Advanced Scenarios Guide - Advanced usage patterns including error handling strategies, working with third-party services, distributed systems considerations, and performance optimization.

Quick Start

Console Application:

dotnet new console -n MyApp
cd MyApp
dotnet add package MiniCron.Core
dotnet add package Microsoft.Extensions.Hosting

See the Console Application Example for complete code.

Web Application:

dotnet new web -n MyWebApp
cd MyWebApp
dotnet add package MiniCron.Core

See the Web Application Example for complete code.

Contributing

Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.


Made with ❤️ by jeanlrnt

About

A minimalist Cron task scheduler for .NET Core.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •  

Languages