Skip to content

Commit

Permalink
Encapsulate all the stuff related to process managers configuration i…
Browse files Browse the repository at this point in the history
…n a single method (#109)
  • Loading branch information
VCuzmin authored Jan 12, 2021
1 parent 6f75af6 commit 1b3d699
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 55 deletions.

This file was deleted.

6 changes: 1 addition & 5 deletions samples/Orchestration/ProcessManagerSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
using NBB.Messaging.Host.Builder;
using NBB.Messaging.Host.MessagingPipeline;
using NBB.Messaging.InProcessMessaging.Extensions;
using NBB.ProcessManager.Definition;
using NBB.ProcessManager.Runtime;
using NBB.ProcessManager.Runtime.Timeouts;
using NBB.Resiliency;
using ProcessManagerSample.MessageMiddlewares;
using ProcessManagerSample.Queries;
Expand All @@ -32,9 +30,7 @@ public static void ConfigureServicesDelegate(HostBuilderContext context, IServic
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>));
//services.AddScoped<INotificationHandler<TimeoutOccured>, TimeoutOccuredHandler>();

services.AddProcessManagerDefinition(Assembly.GetEntryAssembly());
services.AddProcessManagerRuntime();
services.AddNotificationHandlers(typeof(ProcessManagerNotificationHandler<,,>));
services.AddProcessManager(Assembly.GetEntryAssembly());

services.AddEventStore()
.WithNewtownsoftJsonEventStoreSeserializer()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
using Microsoft.Extensions.DependencyInjection;
using NBB.ProcessManager.Runtime.Persistence;
using NBB.ProcessManager.Runtime.Timeouts;
using System;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using NBB.Application.Effects;
using NBB.Core.Effects;
using NBB.Http.Effects;
using NBB.Messaging.Effects;
using NBB.ProcessManager.Definition;
using NBB.ProcessManager.Definition.SideEffects;
using NBB.ProcessManager.Runtime.Persistence;
using NBB.ProcessManager.Runtime.SideEffectHandlers;
using NBB.ProcessManager.Runtime.Timeouts;
using System;
using System.Reflection;
using Unit = NBB.Core.Effects.Unit;

namespace NBB.ProcessManager.Runtime
{
public static class DependencyInjectionExtensions
{
public static void AddProcessManagerRuntime(this IServiceCollection services)
public static void AddProcessManager(this IServiceCollection services, params Assembly[] assemblies)
{
services.AddProcessManagerDefinition(assemblies);
services.AddSingleton<ProcessExecutionCoordinator>();
services.AddSingleton<IInstanceDataRepository, InstanceDataRepository>();
services.AddEffects();
services.AddTimeoutEffects();
services.AddMessagingEffects();
services.AddHttpEffects();
services.AddMediatorEffects();
services.AddScoped<INotificationHandler<TimeoutOccured>, TimeoutOccuredHandler>();
services.AddNotificationHandlers(typeof(ProcessManagerNotificationHandler<,,>));
}

public static void AddTimeoutEffects(this IServiceCollection services)
private static void AddTimeoutEffects(this IServiceCollection services)
{
services.AddHostedService<TimeoutsService>();
services.AddSingleton<TimeoutsManager>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using MediatR;
using NBB.Core.Abstractions;
using NBB.ProcessManager.Definition;
using System.Threading;
using System.Threading.Tasks;

namespace NBB.ProcessManager.Runtime
{
public class ProcessManagerNotificationHandler<TDefinition, TData, TEvent> : INotificationHandler<TEvent>
where TDefinition : IDefinition<TData>
where TData : struct
where TEvent : INotification, IEvent
{
private readonly ProcessExecutionCoordinator _pec;

public ProcessManagerNotificationHandler(ProcessExecutionCoordinator pec)
{
_pec = pec;
}

public async Task Handle(TEvent notification, CancellationToken cancellationToken)
{
await _pec.Invoke<TDefinition, TData, TEvent>(notification, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using MediatR;
using NBB.Messaging.Abstractions;
using NBB.ProcessManager.Runtime.Timeouts;
using System.Threading;
using System.Threading.Tasks;

namespace NBB.ProcessManager.Runtime
{
public class TimeoutOccuredHandler : INotificationHandler<TimeoutOccured>
{
private readonly IMessageBusPublisher _busPublisher;

public TimeoutOccuredHandler(IMessageBusPublisher busPublisher)
{
_busPublisher = busPublisher;
}

public Task Handle(TimeoutOccured notification, CancellationToken cancellationToken)
=> _busPublisher.PublishAsync(notification.Message, cancellationToken);
}
}

0 comments on commit 1b3d699

Please sign in to comment.