GHM.Job 3.0.3

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

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

<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();

TimeZoneStrategy

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(options => options.TimeZoneStrategy = 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);
}

Handlers

To Add handlers at Requester, Executer and Updater executions, implement the IJobHandler at your handler class.

public class MyJobHandler<TRequest> : IJobHandler<TRequest>
{
    public async Task<ExecuterResponse<TRequest, TResponse>> HandleExecuter<TResponse>(
        Func<Task<ExecuterResponse<TRequest, TResponse>>> executer)
    {
        // after executer
        var result = await executer();
        // before executer
        return result;
    }

    public async Task<RequesterResponse<TRequest>> HandleRequester(
        Func<Task<RequesterResponse<TRequest>>> requester)
    {
        // after requester
        var result = await requester();
        // before requester
        return result;
    }

    public async Task<UpdaterResponse<TRequest, TResponse>> HandleUpdater<TResponse>(
        Func<Task<UpdaterResponse<TRequest, TResponse>>> updater)
    {
        // after updater
        var result = await updater();
        // before updater
        return result;
    }
}

public class MyJobServiceHandler<TRequest> : IJobServiceHandler<TRequest>
{
    public async Task<JobServiceResponse<TRequest>> HandleWork(
        Func<Task<JobServiceResponse<TRequest>>> runWork)
    {
        // after runWork
        var result = await runWork();
        // before runWork
        return result;
    }
}

Add Handlers to serviceCollection

using GHM.Job.Extensions;

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

service.AddGhmJob(
    options =>
    {
        options.JobHandler = type(MyJobHandler)
        options.JobServiceHandler = type(MyJobServiceHandler)
    }
);

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 = JobFactory.Create(
            requester: Requester,
            executer: Executer,
            updater: Updater,
            jobOptions:
                new JobOptions<TRequest>(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 = JobAsyncFactory.Create(
            requester: RequesterAsync,
            executer: ExecuterAsync,
            updater: UpdaterAsync,
            jobOptions:
                new JobOptions<TRequest>(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"
    }
}

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 144 2/20/2024
3.0.3 122 2/19/2024
2.2.1 133 2/14/2024
2.1.0 111 2/14/2024
2.0.0 122 2/2/2024
1.0.0 141 1/13/2024