Location>code7788 >text

NET Lightweight Timed Task Scheduler FreeScheduler

Popularity:867 ℃/2024-08-16 10:54:27

preamble

Timed task scheduling is an important feature in usual project development, widely used in modules such as background jobs, scheduled tasks and automation scripts.

FreeScheduler is a lightweight and powerful timed task scheduling library that supports temporary time-delayed tasks and repetitive recurring tasks (which can be persisted) that can be executed in seconds, at a fixed time per day/week/month, or at customized intervals (CRON expressions).

NET Framework 4.0, .NET Core 2.1+, as well as Xamarin, MAUI and other platforms are also supported by FreeScheduler.

In this article, we will introduce the basic features and usage of FreeScheduler, can be immediately used to help us quickly and easily manage and schedule timed tasks, whether for personal projects or enterprise applications are a good choice, come and try it.

Projects

FreeScheduler is a lightweight timed task scheduling library based on the IdleBus implementation, which supports cluster deployment, temporary deferred tasks, and repetitive loop tasks (which can be persisted).

It can execute tasks by the second, daily/weekly/monthly at fixed times or at customized intervals, and is compatible with the .NET Core 2.1+ and .NET Framework 4.0+ runtimes.

  • Lightweighting Explained
Understand FreeRedis, FreeSql, csredis people know that we released the open source project is known as green , zero dependency after the release of only a DLL, will not cause user project dependency conflicts , support for . NET FX 4.0 projects, these projects can not replace the .NET Core version due to historical reasons or hardware limitations. Therefore, these projects can hardly use the existing open source libraries and can't use reliable open source libraries, so they have to implement them by themselves a lot of the time. While piling up the code, the project may become more and more messy, the code becomes more and more crummy, and the project gradually becomes unstable.
  • One of the featured functions
Persistence, such as creating an order that has not been paid for half an hour and sending a reminder text message to the user, is a dynamic task that would use technical persistence.
  • Featured Functions No.2

FreeScheduler supports [5,5,30,30,60] different timer intervals for a task, any success can end the whole task.

Project use

1、Install the relevant Nuget package

dotnet add package FreeScheduler

or

Install-Package FreeScheduler

2. Correspondence

using FreeScheduler;

Scheduler scheduler = new FreeSchedulerBuilder()
    .OnExecuting(task =>
    {
        ($"[{("HH:mm:")}] {} executed");
        ("log..");
    })
    .Build();
if ((scheduler, null, null, null, null).Total == 0)
{
    ("[System Reserved] Cleaning Task Data", "86400", -1, 3600);
    ("(Monday) Martial Arts Competition", "json", -1, "1:12:00:00");
    ("(Sunday) Parent-child activities", "json", -1, "0:00:00:00");
    ("(Saturday) Socializing", "json", -1, "6:00:00:00");
    ("Last day of the month", "json", -1, "-1:16:00:00");
    ("First day of the month", "json", -1, "1:00:00:00");
    ("Timer 20 seconds", "json", 10, 20);
    ("Test Task 1", "json", new[] { 10, 30, 60, 100, 150, 200 });
}
var builder = (args);

();
();
();

(scheduler);
var app = ();
if (())
{
    ();
    ();
}
("/freescheduler/");

();

();

();

();

methodological explanation

Open your browser and visit http://localhost:5000/freescheduler/ to see the tasks we have set up.

Project Characteristics

  • Supports single-project, multi-site deployments

  • Supports multiple processes without duplication of execution

  • Supports reloading of tasks by other processes after a process exits (after about 30 seconds)

  • Support Process interoperability, any process can be executed (RemoveTask/ExistsTask/PauseTask/RunNowTask/RemoveTempTask/ExistsTempTask)

  • Support for uninstalling tasks in a process and rescheduling them when the process goes offline unexpectedly

1. Temporary assignments (non-persistent)

void Callback()
{
    ("It's about time.");
    ((10), Callback); //Next timer.
}
((10), Callback);

methodological explanation

2. Cyclic tasks/persistence

//Triggered every 5 seconds, executed N times
var id = ("topic1", "body1", round: -1, 5);

//Triggered at different intervals of seconds each time, 6 times.
var id = ("topic1", "body1", new [] { 5, 5, 10, 10, 60, 60 });

//Triggered at 20:00:00 every day, executed N times (note the setting of the time zone UseTimeZone)
var id = ("topic1", "body1", round: -1, "20:00:00");

//Every Monday 20:00:00 Triggered, executed 1 time
var id = ("topic1", "body1", round: 1, "1:20:00:00");

//Triggered at 20:00:00 on the 1st day of the month, executed 12 times
var id = ("topic1", "body1", round: 12, "1:20:00:00");
//Triggered at 20:00:00 on the last day of the month, executed 12 times
var id = ("topic1", "body1", round: 12, "-1:20:00:00");

//Custom Interval cron
var id = ("topic1", "body1", "0/1 * * * * ? ");
new FreeSchedulerBuilder()
    ...
    .UseCustomInterval(task =>
    {
        //Parsing the cron library to get the next execution time
//Subtract the current time to get TimeSpan, if it returns null then the task is complete.
        return (5);
    })
    .Build();

Methodological note

3. Setting aside tasks

[System Reserved] Cleaning Task Data

//Hourly trigger to periodically purge data from 24 hours ago (in seconds)
("[System Reserved] Cleaning Task Data", "86400", round: -1, 3600);

4. Management tasks

// Use FreeSql or SQL to query the TaskInfo and TaskLog tables for paging.
<TaskInfo>().Count(out var total).Page(pageNumber, 30).ToList();
<TaskLog>().Count(out var total).Page(pageNumber, 30).ToList();

//Suspension of the mandate
(id);
//Resumption of suspended tasks
(id);
//Deletion of tasks
(id);
//Run the task immediately (manual trigger)
(id);

Performance Reference

FluentScheduler Single Registry tests fine, but visually single threaded execution (1-10ms intervals), processing speed is not ideal

Try changing the FreeScheduler kernel to HashedWheelTimer Higher memory footprint (600 megs).

Conclusion FreeScheduler functionality takes up more resources

Project Address

Gitee:/hubo/FreeScheduler

Github:/2881099/FreeScheduler

Documentation: /guide/

ultimate

If you found this article helpful, why not support it with a like? Your support is what keeps me motivated to continue sharing my knowledge. If you have any questions or need further help, please feel free to leave a comment. You can also join the WeChat public number[DotNet Technician] community to share ideas and grow with other tech-loving peers!