Home

Awesome

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

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

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.