{"id":24358459,"url":"https://github.com/thomasgalliker/ncrontab.scheduler","last_synced_at":"2025-04-10T05:40:48.987Z","repository":{"id":40260621,"uuid":"481850077","full_name":"thomasgalliker/NCrontab.Scheduler","owner":"thomasgalliker","description":"NCrontab.Scheduler is a simple, open source task scheduling system that can be used in any .NET application","archived":false,"fork":false,"pushed_at":"2025-03-07T17:30:51.000Z","size":207,"stargazers_count":16,"open_issues_count":2,"forks_count":2,"subscribers_count":5,"default_branch":"develop","last_synced_at":"2025-04-06T22:41:13.421Z","etag":null,"topics":["automation","cron","cronjob","cronjob-scheduler","scheduler","scheduling","tasks"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thomasgalliker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-04-15T05:41:23.000Z","updated_at":"2025-03-29T19:41:56.000Z","dependencies_parsed_at":"2023-12-14T23:10:36.869Z","dependency_job_id":null,"html_url":"https://github.com/thomasgalliker/NCrontab.Scheduler","commit_stats":{"total_commits":68,"total_committers":2,"mean_commits":34.0,"dds":"0.044117647058823484","last_synced_commit":"ba40341705a86e325c7c7cbae432317091dbb7f7"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasgalliker%2FNCrontab.Scheduler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasgalliker%2FNCrontab.Scheduler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasgalliker%2FNCrontab.Scheduler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasgalliker%2FNCrontab.Scheduler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thomasgalliker","download_url":"https://codeload.github.com/thomasgalliker/NCrontab.Scheduler/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248166145,"owners_count":21058474,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["automation","cron","cronjob","cronjob-scheduler","scheduler","scheduling","tasks"],"created_at":"2025-01-18T20:11:13.167Z","updated_at":"2025-04-10T05:40:48.974Z","avatar_url":"https://github.com/thomasgalliker.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NCrontab.Scheduler\n[![Version](https://img.shields.io/nuget/v/NCrontab.Scheduler.svg)](https://www.nuget.org/packages/NCrontab.Scheduler)  [![Downloads](https://img.shields.io/nuget/dt/NCrontab.Scheduler.svg)](https://www.nuget.org/packages/NCrontab.Scheduler)\n\nNCrontab.Scheduler is a simple, open source task scheduling system that can be used in any .NET application.\nThe main component of this project is a thread-safe scheduler which facilitates very basic scheduling operations like adding, remove or changing task schedules.\nNCrontab.Scheduler is built on top of NCrontab.\n\n## Download and Install NCrontab.Scheduler\nThis library is available on NuGet: https://www.nuget.org/packages/NCrontab.Scheduler\nUse the following command to install NCrontab.Scheduler using NuGet package manager console:\n\n    PM\u003e Install-Package NCrontab.Scheduler\n\nYou can use this library in any .NET Standard or .NET Core project.\n\n## ASP.NET Core Integration\nIn ASP.NET Core projects, use following NuGet package: https://www.nuget.org/packages/NCrontab.Scheduler.AspNetCore\nUse the following command to install NCrontab.Scheduler.AspNetCore using NuGet package manager console:\n\n    PM\u003e Install-Package NCrontab.Scheduler.AspNetCore\n\nYou can use this library in any ASP.NET Core project which is compatible to .NET Core 3.1 and higher.\n\n## API Usage\n### Creating a Scheduler\n`Scheduler` implements the main scheduler operations.\n\n#### Create new Scheduler instance\nYou can either create a new instance of Scheduler manually:\n```C#\nIScheduler scheduler = new Scheduler();\n```\n\n#### Access static Scheduler instance\nAlternatively, you can access the provided singleton instance `Scheduler.Current`.\n\n#### Inject Scheduler using dependency injection\nAlternatively, you can register/resolve `IScheduler` in Microsoft's DI framework `Microsoft.Extensions.DependencyInjection`.\n```C#\nserviceCollection.AddScheduler();\n```\n\nThis `AddScheduler` call regist\ners `IScheduler` and `ISchedulerFactory` as singleton services which can now be injected in your code.\nIf you prefer to have multiple instances of `IScheduler` across your code, inject `ISchedulerFactory` instead and use the Create method to create new instances of `IScheduler`.\n\n### Scheduler Options\nThe `Scheduler` class has an extra parameter `ISchedulerOptions` which allows to override the default configuration of the scheduler.\nIf you use dependency injection, you can also configure the scheduler using the `AddScheduler` method:\n```C#\nserviceCollection.AddScheduler(o =\u003e\n{\n    o.DateTimeKind = DateTimeKind.Utc;\n});\n```\n\nScheduler options can also be read from appSettings.json. Create a new section in your appSettings.json.\n```XML\n\"NCrontab.Scheduler\": {\n    \"DateTimeKind\": \"Utc\"\n},\n```\nRefer to the new configuration section when you call `AddScheduler`:\n```C#\nvar configurationSection = builder.Configuration.GetSection(\"NCrontab.Scheduler\");\nbuilder.Services.AddScheduler(configurationSection);\n```\n\nFollowing options are available:\n- **DateTimeKind**: Interprets the given cron expressions as UTC or local time. Default is UTC.\n\n### Add Scheduled Tasks\nUse method `AddTask` with all the provided convenience overloads to add tasks to the scheduler.\nA task is composed of a cron pattern which specifies the recurrance interval and an action (for synchronous callbacks) or a task (for asynchronous callbacks).\n\n```C#\nscheduler.AddTask(\n    cronExpression: CrontabSchedule.Parse(\"* * * * *\"),\n    action: ct =\u003e { Console.WriteLine($\"{DateTime.Now:O} -\u003e Task runs every minutes\"); });\n\nscheduler.AddTask(\n    cronExpression: CrontabSchedule.Parse(\"*/2 * * * *\"),\n    action: ct =\u003e { Console.WriteLine($\"{DateTime.Now:O} -\u003e Task runs every second minute\"); });\n\nscheduler.AddTask(\n    cronExpression: CrontabSchedule.Parse(\"0 * * * *\"),\n    action: ct =\u003e { Console.WriteLine($\"{DateTime.Now:O} -\u003e Task runs every hour\"); });\n            \nscheduler.AddTask(\n    cronExpression: CrontabSchedule.Parse(\"0 0 * * *\"),\n    action: ct =\u003e { Console.WriteLine($\"{DateTime.Now:O} -\u003e Task runs every day at midnight\"); });\n            \nscheduler.AddTask(\n    cronExpression: CrontabSchedule.Parse(\"0 0 1 1 *\"),\n    action: ct =\u003e { Console.WriteLine($\"{DateTime.Now:O} -\u003e Task runs on Januar 1 every year\"); });    \n```\n\nA task is identified by its Id or its Name. The Id is a Guid which can be user-defined. If it's not set, a rand Guid is used. The Name property is optional and will remain unset if it's not set. The Name property is used in log messages, if the appropriate option is enabled. \n\nTasks can be added to the scheduler either before or after the scheduler has been started.\n\nA very helpful resource for creating cron expression is https://crontab.guru.\n\n### Starting and Stopping\nUse method `IScheduler.StartAsync` to start the scheduler operations.\nThis method can be awaited which blocks all further calls until all scheduled tasks have been canceled or removed.\nUse `IScheduler.Start` if you prefer to start the scheduler without blocking the current execution path of your program.\nIf the scheduler is started without having added any tasks, it just waits (and blocks) until tasks are added.\n\nScheduled tasks can be added at runtime. If the scheduler is started without having added tasks beforehand, it logs this info: `Scheduler is waiting for tasks. Use AddTask methods to add tasks.`\n\n`IScheduler.Stop` attempts to cancel all scheduled tasks immediately.\n\n## About Scheduling\nReal-time systems are systems that are required to respond to an external event in some timely manner.\n\"Timely manner\" is often misinterpreted as \"very fast\".\nHowever, for many applications this just means the system needs to react in a specific time. It can be seconds, minutes, hours or even years.\nSchedulers such as NCrontab.Scheduler can help to satisfy such timing constraints. \n\n### Scheduler Algorithm\nNCrontab.Scheduler is a classic implementation of the Earliest Deadline First (EDF) algorithm.\nThe EDF algorithm will always schedule the task(s) whose deadline is soonest.\n\nThe scheduler provides a dynamic prioritization of tasks by evaluating the cron expressions of the tasks.\nEach scheduling iteration attempts to find the task with the earliest possible execution date/time.\n\nIf tasks are added or removed while waiting for the next execution, the earliest possible execution date/time is re-evaluated.\n\n### Task Overrun Condition\nTask overrun conditions can happen if a scheduled task's execution time exceeds the limit of one minute.\nTask overrun can lead to situations where other tasks miss their projected next execution time.\nConsequently, each of other tasks start missing their deadlines one after the other in sequence (domino effect). This is a classic EDF scheduling problem and can lead to dangerous situations!\n\n### Thread-Safety\nAll scheduler operations are kept thread-safe. Adding and removing tasks as well as starting and stopping the scheduler can be done concurrently.\n\n### Task Isolation\nEach task run is isolated from all other scheduled tasks. The success or failure of execution does not have any negative side effect on other scheduled tasks.\n\n## License\nThis project is Copyright \u0026copy; 2023 [Thomas Galliker](https://ch.linkedin.com/in/thomasgalliker). Free for non-commercial use. For commercial use please contact the author.\n\n## Links\n- https://crontab.guru\n- https://github.com/ilromali/reverse-proxy/blob/72bd65b9aa405f19390e1862f68d6631b599eb73/src/ReverseProxy/Service/Management/EntityActionScheduler.cs\n- https://edurev.in/studytube/Real-Time-Task-Scheduling-1/936b294c-d0f9-4e2d-8b05-7e41ac01a8f3_t\n- https://lectures.tik.ee.ethz.ch/es/slides/6_RealTimeScheduling.pdf\n- https://github.com/yuanhx/Monitor/blob/54aaef1b3b96e0e981a3a3312c48d5042479f0d2/visionlib/scheduler/Scheduler.cs\n- https://github.com/ramonsmits/CronTimer/blob/433510fd3a07507f5f1a518e8fbc31d173a9b09f/src/CronTimer.cs\n- https://github.com/maxcherednik/Tempus/tree/master/src/Tempus\n- https://github.com/atifaziz/NCrontab/issues/28\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomasgalliker%2Fncrontab.scheduler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthomasgalliker%2Fncrontab.scheduler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomasgalliker%2Fncrontab.scheduler/lists"}