Files
2025-12-15 20:29:25 +01:00

2.8 KiB
Raw Permalink Blame History

EonaCat.FluentScheduler

EonaCat.FluentScheduler is a task scheduling library for C# that combines cron expressions and fluent scheduling.

It supports persistent tasks, retries, parallel execution, logging, and dynamic task management.


Features

  • Fluent scheduling: EveryDayAt, EveryWeekOn, EveryMonthOn, EveryYearOn
  • Cron expressions: full 6-part syntax with seconds, minutes, hours, day, month, weekday
    • Supports ranges (1-5), lists (1,2,3), steps (*/5)
  • Async-safe and parallel task execution
  • Retry mechanism for failed tasks with configurable retry count and delay
  • Persistent tasks with automatic restoration on application restart
  • Dynamic task management: remove tasks at runtime
  • Task status tracking: Pending, Running, Success, Failed
  • Optional logging for monitoring task execution

Installation

Install-Package EonaCat.FluentScheduler

Usage

1. Fluent Scheduling

using EonaCat.FluentScheduler;
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        // Daily task at 08:30
        FluentScheduler.Schedule(async () =>
        {
            Console.WriteLine($"Daily task executed at {DateTime.Now}");
        }).EveryDayAt(8, 30);

        // Weekly task on Monday at 09:00
        FluentScheduler.Schedule(async () =>
        {
            Console.WriteLine($"Weekly task executed at {DateTime.Now}");
        }).EveryWeekOn(DayOfWeek.Monday, 9, 0);

        Scheduler.Start();
        Console.ReadKey();
        Scheduler.Stop();
    }
}

2. Cron Scheduling

FluentScheduler.ScheduleCron("Every5Sec", "*/5 * * * * *", async () =>
{
    Console.WriteLine($"Cron task executed every 5 seconds at {DateTime.Now}");
});

3. Logging and Retry

Scheduler.SetLogger(Console.WriteLine);

4. Dynamic Task Removal

// Remove a task by name
Scheduler.RemoveTask("Every5Sec");

5. Task Status Tracking

foreach (var task in Scheduler.GetAllTasks())
{
    Console.WriteLine($"{task.Name}: Status={task.Status}, NextRun={task.NextRun}, LastRun={task.LastRun}, LastError={task.LastError}");
}

6. Retry Mechanism

  • Default: 3 retries, 5 seconds delay
  • Configurable via RetryCount and RetryDelaySeconds on ScheduledTask objects

Cron Expression Format

<second> <minute> <hour> <day> <month> <weekday>
  • any value

*/n every n units

x-y range

x,y,z list of values

Examples:

"0 30 8 * * 1" → Every Monday at 08:30:00

"*/10 * * * * *" → Every 10 seconds

Persistence

  • Tasks are automatically saved to tasks.json on disk.
  • On restart, tasks are restored automatically.
  • Action mapping is handled internally via registered task names.