An open API service indexing awesome lists of open source software.

https://github.com/sitholewb/subpub.hangfire

Lets you create applications with event publishers and subscribers. Publishers communicate LOCALLY with subscribers by broadcasting events, Hangfire will process these events in the background via implemented handlers
https://github.com/sitholewb/subpub.hangfire

event-sourcing hangfire hangfire-extension job-queue job-scheduler jobs publisher scheduler sub-pub-event-topics subscriber

Last synced: about 24 hours ago
JSON representation

Lets you create applications with event publishers and subscribers. Publishers communicate LOCALLY with subscribers by broadcasting events, Hangfire will process these events in the background via implemented handlers

Awesome Lists containing this project

README

          

# SubPub.Hangfire

Lets you create applications with event publishers and subscribers.
Publishers communicate LOCALLY with subscribers by broadcasting events, Hangfire will process these events in the background via implemented handlers

```nuget
Install-Package SubPub.Hangfire
```

```C#

public class RegisterEvent
{
public string Email { get; set; }
public DateTimeOffset Date { get; set; }
}
public class DuplicateRegisterEvent
{
public string Email { get; set; }
public DateTimeOffset Date { get; set; }
}

public class RegisterHandler : IHangfireEventHandler
{
public Task RunAsync(RegisterEvent obj)
{
Console.WriteLine("**************START 1*********");
Console.WriteLine($"{obj.Email} from RegisterHandler");
Console.WriteLine("**************END 1*********");
return Task.CompletedTask;
}
}
public class Register2Handler : IHangfireEventHandler
{
public Task RunAsync(RegisterEvent obj)
{
Console.WriteLine("**************START 1*********");
Console.WriteLine($"{obj.Email} from Register2Handler");
Console.WriteLine("**************END 1*********");
return Task.CompletedTask;
}
}

public class DuplicateRegisterHandler : IHangfireEventHandler
{
public Task RunAsync(DuplicateRegisterEvent obj)
{
Console.WriteLine("**************START 1*********");
Console.WriteLine($"{obj.Email} from DuplicateRegisterHandler");
Console.WriteLine("**************END 1*********");
return Task.CompletedTask;
}
}

[ApiController]
[Route("[controller]")]
public class RegistrationController : ControllerBase
{
private readonly IHangfireEventHandlerContainer _hangfireEventHandlerContainer;

public RegistrationController(IHangfireEventHandlerContainer hangfireEventHandlerContainer)
{
_hangfireEventHandlerContainer = hangfireEventHandlerContainer;
}

[HttpPost]
public IActionResult Register(RegisterModel model)
{
var registerEvent = new RegisterEvent
{
Email = model.Email,
Date = DateTimeOffset.Now,
};
_hangfireEventHandlerContainer.Publish(registerEvent);
return Ok();
}

[HttpPost("schedule")]
public IActionResult RegisterOnSchedule(RegisterModel model)
{
var registerEvent = new RegisterEvent
{
Email = model.Email,
Date = DateTimeOffset.Now,
};

var options = new HangfireJobOptions
{
HangfireJobType = HangfireJobType.Schedule,
TimeSpan = TimeSpan.FromSeconds(15)
};
_hangfireEventHandlerContainer.Publish(registerEvent, options);
return Ok();
}
}

//builder.Services.AddHangfire(x => x.UseSQLiteStorage());
//builder.Services.AddHangfireServer();

builder.Services.AddHangfireSubPub()
.Subscribe()
.Subscribe();

builder.Services.AddHangfireSubPub()
.Subscribe();

```
## Credit
I got some of the other code ideas from this repository https://github.com/lamondlu/EventHandlerInSingleApplication