Skip to content

Repository files navigation

CronTimer

CronTimer is a simple and lightweight library for creating cron jobs in .NET. It provides functionality similar to System.Timer but uses cron expressions to schedule tasks.

Features

  • Schedule tasks using cron expressions.
  • Supports both synchronous and asynchronous operations.
  • Integrates with Microsoft.Extensions.Logging for logging.
  • Supports dependency injection.

Installation

You can install the CronTimer library via NuGet:

dotnet add package Late4dTrain.CronTimer

Usage

Basic Usage

Here's a basic example of how to use the CronTimer:

usingSystem;usingSystem.Threading;usingSystem.Threading.Tasks;usingLate4dTrain.CronTimer;usingLate4dTrain.CronTimer.Events;classProgram{staticasyncTaskMain(string[]args){vartimer=newCronTimer("*/1 * * * * *",CronFormats.IncludeSeconds);timer.TriggeredEventHandler+=async(s,e)=>awaitHandleCronTimer(e);timer.Start();// Let the timer run for 7 secondsawaitTask.Delay(TimeSpan.FromSeconds(7));awaittimer.StopAsync(CancellationToken.None);}privatestaticTaskHandleCronTimer(CronEventArgse){if(e.CancellationToken.IsCancellationRequested)returnTask.CompletedTask;returnTask.Run(()=>{Console.WriteLine("Task has completed at {0}.",DateTime.UtcNow);});}}

Using Dependency Injection and Logging

To use CronTimer with dependency injection and logging, follow these steps:

  1. Add the necessary NuGet packages:

    dotnet add package Microsoft.Extensions.Logging
    dotnet add package Microsoft.Extensions.DependencyInjection
  2. Configure the services in your Startup class:

    usingMicrosoft.Extensions.DependencyInjection;usingMicrosoft.Extensions.Logging;publicclassStartup{publicvoidConfigureServices(IServiceCollectionservices){services.AddLogging(configure =>configure.AddConsole());services.AddSingleton<ITimeProvider,SystemTimeProvider>();services.AddSingleton<IDelayProvider,SystemDelayProvider>();services.AddTransient<CronTimer>(provider =>{varlogger=provider.GetRequiredService<ILogger<CronTimer>>();vartimeProvider=provider.GetService<ITimeProvider>();vardelayProvider=provider.GetService<IDelayProvider>();returnnewCronTimer(options =>{options.AddCronTabs(newCronTab("*/5 * * * * *",CronFormats.IncludeSeconds));},timeProvider,delayProvider,logger);});}}
  3. Use the CronTimer in your application:

    usingSystem;usingSystem.Threading;usingSystem.Threading.Tasks;usingMicrosoft.Extensions.DependencyInjection;usingMicrosoft.Extensions.Logging;classProgram{staticasyncTaskMain(string[]args){varserviceProvider=newServiceCollection().AddLogging(configure =>configure.AddConsole()).AddSingleton<ITimeProvider,SystemTimeProvider>().AddSingleton<IDelayProvider,SystemDelayProvider>().AddTransient<CronTimer>(provider =>{varlogger=provider.GetRequiredService<ILogger<CronTimer>>();vartimeProvider=provider.GetService<ITimeProvider>();vardelayProvider=provider.GetService<IDelayProvider>();returnnewCronTimer(options =>{options.AddCronTabs(newCronTab("*/5 * * * * *",CronFormats.IncludeSeconds));},timeProvider,delayProvider,logger);}).BuildServiceProvider();vartimer=serviceProvider.GetRequiredService<CronTimer>();timer.TriggeredEventHandler+=async(s,e)=>awaitHandleCronTimer(e);timer.Start();// Let the timer run for 7 secondsawaitTask.Delay(TimeSpan.FromSeconds(7));awaittimer.StopAsync(CancellationToken.None);}privatestaticTaskHandleCronTimer(CronEventArgse){if(e.CancellationToken.IsCancellationRequested)returnTask.CompletedTask;returnTask.Run(()=>{Console.WriteLine("Task has completed at {0}.",DateTime.UtcNow);});}}

License

This project is licensed under the MIT License. See the LICENSE file for more details.

This README.md provides an overview of the CronTimer library, installation instructions, basic usage examples, and instructions for using dependency injection and logging.

About

Simple wrapper around Cronos to have functionality similar to System.Timer

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages