Flaminco.TickCronos 1.0.1

dotnet add package Flaminco.TickCronos --version 1.0.1                
NuGet\Install-Package Flaminco.TickCronos -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Flaminco.TickCronos" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Flaminco.TickCronos --version 1.0.1                
#r "nuget: Flaminco.TickCronos, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Flaminco.TickCronos as a Cake Addin
#addin nuget:?package=Flaminco.TickCronos&version=1.0.1

// Install Flaminco.TickCronos as a Cake Tool
#tool nuget:?package=Flaminco.TickCronos&version=1.0.1                

Flaminco.TickCronos

Flaminco.TickCronos is a flexible and lightweight .NET package designed for cron-based background job scheduling. Utilizing the high-performance .NET 8 TimeProvider, TickCronons ensures precise and efficient timing, while allowing you to set unique TimeProvider configurations for each scheduled task.

Features

  • Cron-based scheduling with second-level precision.
  • High-performance timing with .NET 8 TimeProvider.
  • Flexible time zone support for individual jobs.
  • Scoping support for dependency injection within job execution.

Installation

Install Flaminco.TickCronos via NuGet:

dotnet add package Flaminco.TickCronos

Getting Started

Step 1: Create a Cron Job

Inherit from TickCrononsJobService to create your job class, implementing the ExecuteAsync method where the job logic is defined.


public class MyTickCronosExample1 : TickCrononsJobService
{
    public MyTickCronosExample1(ITickCrononsConfig<MyTickCronosExample1> config, IServiceProvider serviceProvider, ILogger<MyTickCronosExample1> logger)
        : base(config.CronExpression, config.TimeProvider, serviceProvider, logger) { }

    public override string CronJobName => nameof(MyTickCronosExample1);

    public override async Task ExecuteAsync(IServiceScope scope, CancellationToken cancellationToken)
    {
        // Define your scheduled job logic
        Console.WriteLine("Running scheduled job for MyTickCronosExample1...");
        await Task.Delay(1000, cancellationToken);  // Simulated work
    }
}


public class MyTickCronosExample2(ITickCronosConfig<MyTickCronosExample2> config, IServiceProvider serviceProvider, ILogger<MyTickCronosExample2> logger)
    : TickCronosJobService(config.CronExpression, config.TimeProvider, serviceProvider, logger)
{
    public override string CronJobName => nameof(MyTickCronosExample2);

    public override async Task ExecuteAsync(IServiceScope scope, CancellationToken cancellationToken)
    {
        // Your job logic here
        Console.WriteLine("Running scheduled job...");
        await Task.Delay(1000, cancellationToken);  // Simulated work
    }
}

Step 2: Configure and Register Jobs

In your application setup (e.g., Program.cs), use AddTickCronosJob to configure and add your job with a cron expression and time provider.

builder.Services.AddTickCronosJob<MyTickCronosExample1>(a =>
{
    a.CronExpression = "*/5 * * * * *";  // Every 5 seconds
    a.TimeProvider = TimeProvider.System; // default if not provided
});

builder.Services.AddTickCronosJob<MyTickCronosExample2>(a =>
{
    a.CronExpression = "*/5 * * * * *";  // Every 5 seconds
    a.TimeProvider = new EgyptTimeProvider();
});

Custom Time Provider Example

TickCronons allows for custom time providers. Here�s an example of a TimeZoneTimeProvider to run jobs in a specific time zone.

public class EgyptTimeProvider : TimeProvider
{
    private readonly TimeZoneInfo _timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Egypt Standard Time");
    public override DateTimeOffset GetUtcNow()
    {
        return TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, _timeZoneInfo);
    }

    public override TimeZoneInfo LocalTimeZone => _timeZoneInfo;
}

Configuration Options

  • CronExpression: Defines the cron schedule.
  • TimeProvider: Customize time zones or use the default system time.

Logging

Flaminco.TickCronos logs job status, execution times, and errors using the .NET ILogger. Ensure logging is set up in your application to capture job activities.

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 95 11/9/2024