GHM.Job 2.1.0

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

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

<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

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

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