Ez4.CronJob 6.0.4

dotnet add package Ez4.CronJob --version 6.0.4                
NuGet\Install-Package Ez4.CronJob -Version 6.0.4                
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.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ez4.CronJob --version 6.0.4                
#r "nuget: Ez4.CronJob, 6.0.4"                
#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.4

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

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 Database EFCore SqlProvider

//Jobs
services.AddEz4DbLeaderConfig(options =>
{
    //Config Key
    options.KeyOptions = opt =>
    {
        opt.ServiceName = "data247-wk";
        // Ensure IdentityName is unique name
        opt.IdentityName = Environment.GetEnvironmentVariable("HOST_NAME") ?? $"{opt.ServiceName}-{Guid.NewGuid()}";
    };

    //Config Duration
    options.LockConfigOptions = opt =>
    {
        opt.Duration = TimeSpan.FromSeconds(30);
        opt.Retry = TimeSpan.FromSeconds(10);
    };

    //Store DbContext
    options.StoreOptions = opt =>
    {
        /*
            public class Ez4Lock
            {
                [Key]
                public int Id { get; set; }
                public string HoldIdentity { get; set; }
                public string ServiceName { get; set; }
                public DateTime? AcquiredTime { get; set; }
                public DateTime? RenewTime { get; set; }
                public DateTime? UpdateTime { get; set; }
                public int TakeLockTime { get; set; }
                public int Duration { get; set; }
                public int Retry { get; set; }
            }

            configuration custome your table name and schema
         */        
        opt.DefaultSchema = "dbo";
        opt.LockTable = new Ez4.DbLeader.EFCore.TableConfiguration("YourLockTable");

        opt.ConfigureDbContext =
            builder => builder
                    .UseSqlServer("ConnectionString");
    };
});

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

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 152 8/5/2024
6.0.3 90 7/16/2024
6.0.2 84 7/12/2024
6.0.0 80 7/11/2024