turgayozgur / tossit

Library for distributed job/worker logic.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tossit

Latest version Build Status Build status codecov Gitter

Simple, easy to use library for distributed job/worker logic. Distributed messages handled by built in RabbitMQ implementation.

Highlights

  • Super easy way to use RabbitMQ .net client for job/worker logic.
  • Connection and channel management.
  • Failure management.
  • Send and receive data that auto converted to your object types.
  • Recovery functionality. Do not worry about connection loses.

Installation

You need to install Tossit.RabbitMQ nuget package.

PM> Install-Package Tossit.RabbitMQ

Use ConfigureServices method on startup to register services.

public void ConfigureServices(IServiceCollection services)
{
    // Add RabbitMQ implementation dependencies.
    services.AddRabbitMQ();

    // Warning!
    // Call only AddTossitJob method or only AddTossitWorker method which one you need. 
    // Call both of them, if current application contains worker and job.

    // Add Tossit Job dependencies.
    services.AddTossitJob();

    // Add Tossit Worker dependencies.
    services.AddTossitWorker(); 
}

Then, use configure method to configuring RabbitMQ server and prepare workers.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    // Use RabbitMQ server.
    app.UseRabbitMQServer("amqp://guest:guest@localhost");

    // Warning!
    // Call UseTossitWorker method, if current application contains worker.

    // If this application has worker(s), register it.
    app.UseTossitWorker();
}

Job Usage

Create a new class to sending to worker(s).

public class FooData
{
    public int Id { get; set; }
}

Create a new job class to dispatch data to worker(s).

public class FooJob : IJob<FooData>
{
    public FooData Data { get; set; }

    public string Name => "foo.job.name"; // Job name should be unique for each job.
}

Dispatch job using IJobDispatcher.

[Route("api/[controller]")]
public class AnyController : Controller
{
    private readonly IJobDispatcher _jobDispatcher;

    public AnyController(IJobDispatcher jobDispatcher)
    {
        _jobDispatcher = jobDispatcher;
    }

    [HttpGet]
    public IActionResult Create()
    {
        // Dispatch job.
        var isDispatched = _jobDispatcher.Dispatch(new FooJob
        {
            Data = new FooData
            {
                Id = 1
            }
        });

        return Ok();
    }
}

Send Options

  • WaitToRetrySeconds: Time as second for wait to retry when job rejects or throws an error. Should be greater then zero. Default 30 seconds.

  • ConfirmReceiptIsActive: Set true if u want to wait to see the data received successfully from a worker until timeout. Otherwise can be false. It is highly recommended to be true. Default: true.

  • ConfirmReceiptTimeoutSeconds: Wait until a dispatched data have been confirmed. Default 10 seconds.

public void ConfigureServices(IServiceCollection services)
{
    ...

    // Add Tossit Job dependencies with options.
    services.AddTossitJob(sendOptions => {
        sendOptions.WaitToRetrySeconds = 30;
        sendOptions.ConfirmReceiptIsActive = true;
        sendOptions.ConfirmReceiptTimeoutSeconds = 10;
    });

    ...
}

Worker Usage

Create new class to accept the data sent from jobs.

public class FooData
{
    public int Id { get; set; }
}

Create a new worker class to process given data.

public class FooWorker : IWorker<FooData>
{
    public string JobName => "foo.job.name"; // Should be same as dispatched job name.

    public bool Work(FooData data)
    {
        // Lets, do whatever you want by data.

        // Return true, if working completed successfully, otherwise return false.
        return true;
    }
}

License

The Tossit is open-sourced software licensed under the MIT license.

About

Library for distributed job/worker logic.

License:MIT License


Languages

Language:C# 98.8%Language:Shell 1.2%