Scheduler is a simple job scheduling framework in C# that allows you to easily define and execute scheduled tasks in the background. This project provides a flexible base class for creating scheduled jobs, a background service for managing job execution, a static api to register your background services dynamically, and an extension method for integrating the scheduler into your application.
- Define custom scheduled jobs by extending the
ScheduledJob
base class. - Enqueue jobs for scheduling using the
ScheduleJobAsync
method. - Background service (
SchedulerService
) manages the execution of scheduled jobs. - Integration with Microsoft.Extensions.DependencyInjection for easy setup.
-
Install the Scheduler package from NuGet:
dotnet add package nightmaregaurav.scheduler
-
Create your scheduled job classes by extending the
ScheduledJob
base class. Implement the required methods:public class MyCustomJob : ScheduledJob { public override DateTime GetNextExecutionSchedule() { // Implement your logic to determine the next execution time. } public override Task Execute(IServiceProvider serviceProvider) { // Implement your job's logic here. } }
-
Add the
SchedulerService
as a hosted service in your application using the extension method:public void ConfigureServices(IServiceCollection services) { // Other service registrations... // Add the scheduler service services.StartScheduler(); }
or in your
program.cs
// Other service registrations... // Add the scheduler service builder.Services.StartScheduler();
-
Enqueue jobs for scheduling:
var myJob = new MyCustomJob(); SchedulerService.ScheduleJobAsync(myJob);
-
Run your application, and the scheduler will automatically start executing the scheduled jobs in the background.
- Copy contents of Example folder to a new .NET web application.
- Rename
program-example.cs
toprogram.cs
and uncomment the contents ofprogram.cs
. - Run the application.
- You will see a static scheduled job running every 5 seconds.
- You can send a get request to
/print-time
to schedule a dynamic job that will run every 10 seconds. - You can send a post request to
/send-message
withto
,message
andsecondsDelay
in body to schedule a dynamic job that will run once after the specified delay.
Scheduler is released under the MIT License. You can find the full license details in the LICENSE file.
Made with ❤️ by NightmareGaurav.
We welcome contributions from the community! If you find any issues or have suggestions for improvements, feel free to open a pull request or issue. Your contributions help make this project better for everyone.