Ez4.CronJob 6.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Ez4.CronJob --version 6.0.2                
NuGet\Install-Package Ez4.CronJob -Version 6.0.2                
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="Ez4.CronJob" Version="6.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ez4.CronJob --version 6.0.2                
#r "nuget: Ez4.CronJob, 6.0.2"                
#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 Ez4.CronJob as a Cake Addin
#addin nuget:?package=Ez4.CronJob&version=6.0.2

// Install Ez4.CronJob as a Cake Tool
#tool nuget:?package=Ez4.CronJob&version=6.0.2                

Getting Started

Cron expression

                                       Allowed values    Allowed special characters   Comment

┌───────────── second (optional)       0-59              * , - /
│ ┌───────────── minute                0-59              * , - /
│ │ ┌───────────── hour                0-23              * , - /
│ │ │ ┌───────────── day of month      1-31              * , - / L W ?
│ │ │ │ ┌───────────── month           1-12 or JAN-DEC   * , - /
│ │ │ │ │ ┌───────────── day of week   0-6  or SUN-SAT   * , - / # L ?                Both 0 and 7 means SUN
│ │ │ │ │ │
* * * * * *

Usage

With Base

MyJob.cs

 public class BasicCronJob : Ez4CronJobBase
 {
    private readonly ILogger<BasicCronJob> logger;

    public BasicCronJob(ILogger<BasicCronJob> logger, ICronConfiguration<BasicCronJob> cronConfiguration)
       : base(logger, cronConfiguration)
    {
       this.logger = logger;
       logger.LogInformation("BasicCronJob created");
    }

    public override Task ExecuteAsync(CancellationToken cancellationToken)
    {
       logger.LogWarning("Working BasicCronJob" + " Start Time : " + DateTime.UtcNow);
       return Task.CompletedTask;
    }
 }

Program.cs Local test with each 5 second

 builder.Services.ApplyEz4CronJob<BasicCronJob>(op =>
 {
    op.CronExpression = "*/5 * * * * *";
    op.CronFormat = CronFormat.IncludeSeconds;
    op.TimeZoneInfo = TimeZoneInfo.Local;
 });

With CronExpressionAttribute

[CronExpression(cronExpression: "*/2 * * * * *", cronFormat: CronFormat.IncludeSeconds)]
public class BasicCronJobAuto : Ez4CronJobBase, ICronJobAutoConfig
{
    private readonly ILogger<BasicCronJobAuto> logger;

    public BasicCronJobAuto(ILogger<BasicCronJobAuto> logger, ICronConfiguration<BasicCronJobAuto> cronConfiguration)
        : base(logger, cronConfiguration)
    {
        this.logger = logger;
        logger.LogInformation("BasicCronJobAuto created {0} {1} {2}", cronConfiguration.CronExpression, cronConfiguration.TimeZoneInfo, cronConfiguration.CronFormat);
    }

    public override Task ExecuteAsync(CancellationToken cancellationToken)
    {
        logger.LogWarning("Working BasicCronJobAuto" + " Start Time : " + DateTime.UtcNow);
        return Task.CompletedTask;
    }
}

Program.cs

builder.Services.AutoApplyEz4CronJob();

With k8s Cluster

[CronExpression(cronExpression: "*/5 * * * * *", cronFormat: CronFormat.IncludeSeconds)]
public class ConsoleBasicCronJob : Ez4CronJobLeader, ICronJobAutoConfig
{
    private ILogger<ConsoleBasicCronJob> _logger;
    public ConsoleBasicCronJob(ILogger<ConsoleBasicCronJob> logger, IEz4LeaderConfiguration Ez4CronJobLeaderOption, ICronConfiguration<ConsoleBasicCronJob> cronConfig) : base(logger, Ez4CronJobLeaderOption, cronConfig)
    {
        _logger = logger;
    }

    public override Task ExecuteAsync(CancellationToken cancellationToken)
    {
        _logger.LogWarning("ok at {0}", DateTime.Now);
        return Task.CompletedTask;
    }
}

Program.cs

builder.Services.AddEz4LeaderConfig(op =>
{
    op.Namespace = "default";
    op.Lock = "my-lock";
});

builder.Services.AutoApplyEz4CronJob();

Example with local k8s & minikube

Prerequisites

Run k8s on Docker Desktop.

Start minikube and run demo test

   minikube start
   helm_deploy.sh

Image description

    kubectl -n ns-demo2024 get pod

Image description

    kubectl -n ns-demo2024 get lease

Image description

    kubectl -n ns-demo2024 logs replace_your_lease_pod

Image description

Image description

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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
6.0.4 155 8/5/2024
6.0.3 93 7/16/2024
6.0.2 87 7/12/2024
6.0.0 83 7/11/2024