GHM.Job 2.2.1

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

// Install GHM.Job as a Cake Tool
#tool nuget:?package=GHM.Job&version=2.2.1

<p align="center"> <img src="logo.png" alt="logo" width="200px"/> </p>

<h1 align="center"> GHM.Job </h1>

GHM.Job is a nuget package aims to run jobs in separates ways(Get Request, Run Request and Update Request).

Install Package

.NET CLI

dotnet add package GHM.Job

Package Manager

NuGet\Install-Package GHM.Job

IServiceCollectionExtensions

To add scoped interface IJobService<> to implementate JobService<> , call extension method to your serviceCollection.

using GHM.Job.Extensions;

var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
service.AddGhmJob();

If you want to set TimeZoneStrategy to set a DateTime.Now for executeAsync with cron string * * * * *, pass one of these strategies at calling AddGhmJob(ITimeZoneStrategy timeZoneStrategy).

  • NowTimeZoneStrategy()
  • UtcTimeZoneStrategy()
  • UtcAddingHoursTimeZoneStrategy(int addhours)
using GHM.Job.Extensions;

var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;

var strategy = new UtcAddingHoursTimeZoneStrategy(-3) // -3Hours UTC
service.AddGhmJob(strategy);
public class NowTimeZoneStrategy : ITimeZoneStrategy
{
    public DateTime Now => DateTime.Now;
}

public class UtcTimeZoneStrategy : ITimeZoneStrategy
{
    public DateTime Now => DateTime.UtcNow;
}

public class UtcAddingHoursTimeZoneStrategy : ITimeZoneStrategy
{
    private readonly int _addhours;

    public UtcAddingHoursTimeZoneStrategy(int addhours)
    {
        _addhours = addhours;
    }

    public DateTime Now => DateTime.UtcNow.AddHours(_addhours);
}

Example

To run a Job

The Job Run the Sequence:

  • Get Requests Unique or List
  • Execute Request
  • After Execute Request
  • On error Execute Request
  • Update Request
  • After Update Request
  • On error Update Request
  • After Work Request
using GHM.Job;

public class MyBackgroundService : BackgroundService
{
    private readonly IJobService<string> _jobService = new JobService<string>();

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        var result = "processing";

        string Requester() => " => data";
        string Executer(string data) => result += data + " => Executer";
        void Updater(string data) => result += " => Updater";
        void AfterWork() => result += " => AfterWork";
        void AfterExecuter(string data) => result += " => AfterExecuter";
        string LoggerId(string data) => data;

        // Act
        var job = Job.Create(
            requesterUnique: Requester,
            executer: Executer,
            updater: Updater,
            afterWork: AfterWork,
            afterExecuter: AfterExecuter,
            loggerId: LoggerId
        );

        // Setting delay: 1 second to the next job running.
        await _jobService.ExecuteAsync(job, TimeSpan.FromSeconds(1), stoppingToken);

        // Running 1 time.
        await _jobService.ExecuteAsync(job, stoppingToken);

        // Setting cron: At minute 5 to job running.
        // More informations about cron. https://crontab.guru
        await _jobService.ExecuteAsync(job, "5 * * * *" ,stoppingToken);

        // result = "processing => data => Executer => AfterExecuter => Updater => AfterWork"
    }
}

You can run your job async.

using GHM.Job;

public class MyBackgroundService : BackgroundService
{
    private readonly IJobService<string> _jobService = new JobService<string>();

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        var result = "processing";

        Task<string> RequesterAsync() => Task.FromResult(" => data");
        Task<string> ExecuterAsync(string data) => Task.FromResult(result += data + " => Executer");
        Task UpdaterAsync(string data) => Task.FromResult(result += " => Updater");
        void AfterWork() => result += " => AfterWork";
        void AfterExecuter(string data) => result += " => AfterExecuter";
        string LoggerId(string data) => data;

        // Act
        var job = JobAsync.Create(
            requesterUnique: RequesterAsync,
            executer: ExecuterAsync,
            updater: UpdaterAsync,
            afterWork: AfterWork,
            afterExecuter: AfterExecuter,
            loggerId: LoggerId
        );

        // Setting delay: 1 second to the next job running.
        await _jobService.ExecuteAsync(job, TimeSpan.FromSeconds(1), stoppingToken);

        // Running 1 time.
        await _jobService.ExecuteAsync(job, stoppingToken);

        // result = "processing => data => Executer => AfterExecuter => Updater => AfterWork"
    }
}

Classes

IJobService

It is a interface implemented by JobService<TRequest>

namespace GHM.Job;

public class JobService<TRequest> : IJobService<TRequest>
{
    public async Task ExecuteAsync<TResponse>(
        Job<TRequest, TResponse> job,
        TimeSpan interval,
        CancellationToken token = default
    )
    {
        while (!token.IsCancellationRequested)
        {
            await Task.Run(job.DoWork, token);
            await Task.Delay(interval, token);
        }
    }

    public async Task ExecuteAsync<TResponse>(Job<TRequest, TResponse> job, CancellationToken token = default)
    {
        await Task.Run(job.DoWork, token);
    }

    public async Task ExecuteAsync<TResponse>(Job<TRequest, TResponse> job, string cron, CancellationToken token = default)
    {
        var crontabSchedule = CrontabSchedule.Parse(cron);
        var nextOccurrence = crontabSchedule.GetNextOccurrence(DateTime.Now);

        while (!token.IsCancellationRequested)
        {
            if (DateTime.Now > nextOccurrence)
            {
                await Task.Run(job.DoWork, token);
                nextOccurrence = crontabSchedule.GetNextOccurrence(DateTime.Now);
            }

            await Task.Delay(1000, token);
        }
    }
}

Star

if you enjoy, don't forget the ⭐ and install the package 😊.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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
3.1.2 111 2/20/2024
3.0.3 99 2/19/2024
2.2.1 112 2/14/2024
2.1.0 85 2/14/2024
2.0.0 87 2/2/2024
1.0.0 110 1/13/2024