<commit_before># Tossit #
[![Build Status](https://travis-ci.org/turgayozgur/tossit.svg?branch=master)](https://travis-ci.org/turgayozgur/tossit)
[![Build status](https://ci.appveyor.com/api/projects/status/whuoamx1tb19jbn6/branch/master?svg=true)](https://ci.appveyor.com/project/turgayozgur/tossit/branch/master)
[![codecov](https://codecov.io/gh/turgayozgur/tossit/branch/master/graph/badge.svg)](https://codecov.io/gh/turgayozgur/tossit)

| Tossit.WorkQueue | Tossit.RabbitMQ |
|:-------------------------------------------------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------------------------------:|
| [![Latest version](https://img.shields.io/nuget/v/Tossit.WorkQueue.svg)](https://www.nuget.org/packages/Tossit.WorkQueue) | [![Latest version](https://img.shields.io/nuget/v/Tossit.RabbitMQ.svg)](https://www.nuget.org/packages/Tossit.RabbitMQ) |

Simple, easy to use library for disturbuted job/worker logic. Disturbuted messages handled by built in [RabbitMQ](https://github.com/rabbitmq/rabbitmq-dotnet-client) implementation.
## Installation ##
You need to install [Tossit.RabbitMQ](https://www.nuget.org/packages/Tossit.RabbitMQ) and [Tossit.WorkQueue](https://www.nuget.org/packages/Tossit.WorkQueue) nuget packages.
```
PM> Install-Package Tossit.RabbitMQ
PM> Install-Package Tossit.WorkQueue
```
Use ConfigureServices method on startup to register services.
```csharp
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.
```csharp
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 Useage ##
Create a new class to sending to worker(s).
```csharp
public class FooData
{
    public int Id { get; set; }
}
```
Create a new job class to dispatch data to worker(s).
```csharp
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.
```csharp
[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();
    }
}
```
You can also dispatch a job as async.
```csharp
Task<bool> task = _jobDispatcher.DispatchAsync(new FooJob { Data = new FooData { Id = 1 } });
```
## Worker Usage ##
Create new class to accept the data sent from jobs.
```csharp
public class FooData
{
    public int Id { get; set; }<commit_msg>Readme edited. - DispatchAsync method no longer active. Removed from readme.md.<commit_after># Tossit #
[![Build Status](https://travis-ci.org/turgayozgur/tossit.svg?branch=master)](https://travis-ci.org/turgayozgur/tossit)
[![Build status](https://ci.appveyor.com/api/projects/status/whuoamx1tb19jbn6/branch/master?svg=true)](https://ci.appveyor.com/project/turgayozgur/tossit/branch/master)
[![codecov](https://codecov.io/gh/turgayozgur/tossit/branch/master/graph/badge.svg)](https://codecov.io/gh/turgayozgur/tossit)

| Tossit.WorkQueue | Tossit.RabbitMQ |
|:-------------------------------------------------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------------------------------:|
| [![Latest version](https://img.shields.io/nuget/v/Tossit.WorkQueue.svg)](https://www.nuget.org/packages/Tossit.WorkQueue) | [![Latest version](https://img.shields.io/nuget/v/Tossit.RabbitMQ.svg)](https://www.nuget.org/packages/Tossit.RabbitMQ) |

Simple, easy to use library for disturbuted job/worker logic. Disturbuted messages handled by built in [RabbitMQ](https://github.com/rabbitmq/rabbitmq-dotnet-client) implementation.
## Installation ##
You need to install [Tossit.RabbitMQ](https://www.nuget.org/packages/Tossit.RabbitMQ) and [Tossit.WorkQueue](https://www.nuget.org/packages/Tossit.WorkQueue) nuget packages.
```
PM> Install-Package Tossit.RabbitMQ
PM> Install-Package Tossit.WorkQueue
```
Use ConfigureServices method on startup to register services.
```csharp
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.
```csharp
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 Useage ##
Create a new class to sending to worker(s).
```csharp
public class FooData
{
    public int Id { get; set; }
}
```
Create a new job class to dispatch data to worker(s).
```csharp
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.
```csharp
[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();
    }
}
```
## Worker Usage ##
Create new class to accept the data sent from jobs.
```csharp
public class FooData
{
    public int Id { get; set; }
{'fraction_non_alphanumeric': 0.1852108768035516, 'fraction_numerical': 0.0015260821309655938, 'mean_word_length': 2.758602711157456, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 12, 'https://': 26, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4164194', 'n_tokens_mistral': 2296, 'n_tokens_neox': 2126, 'n_words': 521}