forked from abpframework/abp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented initial inbox processing logic
- Loading branch information
Showing
19 changed files
with
470 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
framework/src/Volo.Abp.EventBus.Boxes/Volo/Abp/EventBus/Boxes/IInboxProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Volo.Abp.EventBus.Distributed; | ||
|
||
namespace Volo.Abp.EventBus.Boxes | ||
{ | ||
public interface IInboxProcessor | ||
{ | ||
Task StartAsync(InboxConfig inboxConfig, CancellationToken cancellationToken = default); | ||
|
||
Task StopAsync(CancellationToken cancellationToken = default); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
framework/src/Volo.Abp.EventBus.Boxes/Volo/Abp/EventBus/Boxes/InboxProcessManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using Volo.Abp.BackgroundWorkers; | ||
using Volo.Abp.EventBus.Distributed; | ||
|
||
namespace Volo.Abp.EventBus.Boxes | ||
{ | ||
public class InboxProcessManager : IBackgroundWorker | ||
{ | ||
protected AbpDistributedEventBusOptions Options { get; } | ||
protected IServiceProvider ServiceProvider { get; } | ||
protected List<IInboxProcessor> Processors { get; } | ||
|
||
public InboxProcessManager( | ||
IOptions<AbpDistributedEventBusOptions> options, | ||
IServiceProvider serviceProvider) | ||
{ | ||
ServiceProvider = serviceProvider; | ||
Options = options.Value; | ||
Processors = new List<IInboxProcessor>(); | ||
} | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken = default) | ||
{ | ||
foreach (var inboxConfig in Options.Inboxes.Values) | ||
{ | ||
if (inboxConfig.IsProcessingEnabled) | ||
{ | ||
var processor = ServiceProvider.GetRequiredService<IInboxProcessor>(); | ||
await processor.StartAsync(inboxConfig, cancellationToken); | ||
Processors.Add(processor); | ||
} | ||
} | ||
} | ||
|
||
public async Task StopAsync(CancellationToken cancellationToken = default) | ||
{ | ||
foreach (var processor in Processors) | ||
{ | ||
await processor.StopAsync(cancellationToken); | ||
} | ||
} | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
framework/src/Volo.Abp.EventBus.Boxes/Volo/Abp/EventBus/Boxes/InboxProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Medallion.Threading; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Volo.Abp.DependencyInjection; | ||
using Volo.Abp.EventBus.Distributed; | ||
using Volo.Abp.Threading; | ||
using Volo.Abp.Uow; | ||
|
||
namespace Volo.Abp.EventBus.Boxes | ||
{ | ||
public class InboxProcessor : IInboxProcessor, ITransientDependency | ||
{ | ||
protected IServiceProvider ServiceProvider { get; } | ||
protected AbpTimer Timer { get; } | ||
protected IDistributedEventBus DistributedEventBus { get; } | ||
protected IDistributedLockProvider DistributedLockProvider { get; } | ||
protected IUnitOfWorkManager UnitOfWorkManager { get; } | ||
protected IEventInbox Inbox { get; private set; } | ||
protected InboxConfig InboxConfig { get; private set; } | ||
|
||
protected string DistributedLockName => "Inbox_" + InboxConfig.Name; | ||
public ILogger<InboxProcessor> Logger { get; set; } | ||
|
||
public InboxProcessor( | ||
IServiceProvider serviceProvider, | ||
AbpTimer timer, | ||
IDistributedEventBus distributedEventBus, | ||
IDistributedLockProvider distributedLockProvider, | ||
IUnitOfWorkManager unitOfWorkManager) | ||
{ | ||
ServiceProvider = serviceProvider; | ||
Timer = timer; | ||
DistributedEventBus = distributedEventBus; | ||
DistributedLockProvider = distributedLockProvider; | ||
UnitOfWorkManager = unitOfWorkManager; | ||
Timer.Period = 2000; //TODO: Config? | ||
Timer.Elapsed += TimerOnElapsed; | ||
Logger = NullLogger<InboxProcessor>.Instance; | ||
} | ||
|
||
private void TimerOnElapsed(object sender, EventArgs e) | ||
{ | ||
AsyncHelper.RunSync(RunAsync); | ||
} | ||
|
||
public Task StartAsync(InboxConfig inboxConfig, CancellationToken cancellationToken = default) | ||
{ | ||
InboxConfig = inboxConfig; | ||
Inbox = (IEventInbox)ServiceProvider.GetRequiredService(inboxConfig.ImplementationType); | ||
Timer.Start(cancellationToken); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken = default) | ||
{ | ||
Timer.Stop(cancellationToken); | ||
return Task.CompletedTask; | ||
} | ||
|
||
protected virtual async Task RunAsync() | ||
{ | ||
await using (var handle = await DistributedLockProvider.TryAcquireLockAsync(DistributedLockName)) | ||
{ | ||
if (handle != null) | ||
{ | ||
Logger.LogDebug("Obtained the distributed lock: " + DistributedLockName); | ||
|
||
while (true) | ||
{ | ||
var waitingEvents = await Inbox.GetWaitingEventsAsync(1000); //TODO: Config? | ||
if (waitingEvents.Count <= 0) | ||
{ | ||
break; | ||
} | ||
|
||
Logger.LogInformation($"Found {waitingEvents.Count} events in the inbox."); | ||
|
||
foreach (var waitingEvent in waitingEvents) | ||
{ | ||
using (var uow = UnitOfWorkManager.Begin(isTransactional: true, requiresNew: true)) | ||
{ | ||
await DistributedEventBus | ||
.AsRawEventPublisher() | ||
.ProcessRawAsync(waitingEvent.EventName, waitingEvent.EventData); | ||
|
||
/* | ||
await DistributedEventBus | ||
.AsRawEventPublisher() | ||
.PublishRawAsync(waitingEvent.Id, waitingEvent.EventName, waitingEvent.EventData); | ||
*/ | ||
await Inbox.MarkAsProcessedAsync(waitingEvent.Id); | ||
await uow.CompleteAsync(); | ||
} | ||
|
||
Logger.LogInformation($"Processed the incoming event with id = {waitingEvent.Id:N}"); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
Logger.LogDebug("Could not obtain the distributed lock: " + DistributedLockName); | ||
await Task.Delay(7000); //TODO: Can we pass a cancellation token to cancel on shutdown? (Config?) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.