Skip to content

Commit

Permalink
Feature/worker template updates (#95)
Browse files Browse the repository at this point in the history
* Worker template updates
TenantMiddleware when TenancyType == None

* Template updates

* Removed PackegaVersionOverrides

* Health checks UI client

* reorder namespace imports
  • Loading branch information
fraliv13 authored Oct 9, 2020
1 parent bcebfa7 commit 076a194
Show file tree
Hide file tree
Showing 21 changed files with 119 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@ namespace NBB.Contracts.Worker.MultiTenancy
{
public class TenantRepositoryMock : ITenantRepository
{
private readonly IOptions<TenancyHostingOptions> _tenancyHostingOptions;

public TenantRepositoryMock(IOptions<TenancyHostingOptions> tenancyHostingOptions)
public TenantRepositoryMock()
{
_tenancyHostingOptions = tenancyHostingOptions;
}

public Task<Tenant> Get(Guid id, CancellationToken token = default)
{
var isMonoTenant = _tenancyHostingOptions.Value.TenancyType == TenancyType.MonoTenant &&
id == _tenancyHostingOptions.Value.TenantId;

var tenant = new Tenant(id, id.ToString(), !isMonoTenant);
var tenant = new Tenant(id, id.ToString(), true);
return Task.FromResult(tenant);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static async Task MainAsync(string[] args)
.UsePipeline(pipelineBuilder => pipelineBuilder
.UseCorrelationMiddleware()
.UseExceptionHandlingMiddleware()
//.UseTenantMiddleware()
.UseTenantMiddleware()
.UseDefaultResiliencyMiddleware()
.UseMediatRMiddleware()
);
Expand Down
10 changes: 8 additions & 2 deletions src/Messaging/NBB.Messaging.MultiTenancy/TenantMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public TenantMiddleware(ITenantContextAccessor tenantContextAccessor, ITenantIde

public async Task Invoke(MessagingEnvelope message, CancellationToken cancellationToken, Func<Task> next)
{
if (_tenancyOptions.Value.TenancyType == TenancyType.None)
{
await next();
return;
}

var tenantId = await _tenantIdentificationService.GetTenantIdAsync();

if (!message.Headers.TryGetValue(MessagingHeaders.TenantId, out var messageTenantIdHeader))
Expand Down Expand Up @@ -71,14 +77,14 @@ public async Task Invoke(MessagingEnvelope message, CancellationToken cancellati
throw new ApplicationException(
$"Received a message for premium tenant {messageTenantIdHeader} in a MultiTenant (shared) context");
}

if (_tenantContextAccessor.TenantContext != null)
{
throw new ApplicationException("Tenant context is already set");
}

_tenantContextAccessor.TenantContext = new TenantContext(tenant);

await next();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,14 @@
"SqlLoggingConnectionString": {
"longName": "sql-logging-connection-string"
},
"FluentValidation": {
"longName": "fluent-validation"
},
"AutoMapper": {
"longName": "auto-mapper"
},
"MediatR": {
"longName": "mediat-r"
},
"AddSamples": {
"longName": "add-samples"
},
"MessagingTransport": {
"longName": "messaging-transport"
},
"SkipRestore": {
"longName": "no-restore"
},
"OpenTracing": {
"longName": "open-tracing"
}
}
}
24 changes: 3 additions & 21 deletions template/Worker/content/NBB.Worker/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"sources": [
{
"exclude": ["PlaceholderTypes/*", ".template.config/*"],
"exclude": [".template.config/*"],
"modifiers": [
{
"condition": "(!HealthCheck)",
Expand Down Expand Up @@ -107,29 +107,11 @@
"replaces": "SQL_LOG_CONNECTION_STRING",
"description": "The connection string to the sql server database."
},
"FluentValidation": {
"OpenTracing": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "true",
"description": "Register fluent validators in IoC container."
},
"AutoMapper": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "true",
"description": "Register AutoMapper profilers in IoC container."
},
"MediatR": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "true",
"description": "Add MediatR support."
},
"AddSamples": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "true",
"description": "Add sample files like a subscriber pipeline middleware or a publisher decorator."
"description": "Add Jaeger / OpenTracing support"
},
"MessagingTransport": {
"type": "parameter",
Expand Down
24 changes: 24 additions & 0 deletions template/Worker/content/NBB.Worker/Application/HelloWorld.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using MediatR;
using NBB.Core.Abstractions;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace NBB.Worker.Application
{
public class HelloWorld
{
public class Command : ICommand, IRequest
{

}

public class Handler : IRequestHandler<Command>
{
public Task Handle(Command request, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
}
}
69 changes: 12 additions & 57 deletions template/Worker/content/NBB.Worker/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
#if AutoMapper
using AutoMapper;
#endif
#if FluentValidation
using FluentValidation;
#endif
#if MediatR
using MediatR;
using MediatR;
using MediatR.Pipeline;
#endif
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NBB.Application.DataContracts;
using NBB.Worker.Messaging;
using NBB.Worker.Application;
using NBB.Messaging.Host;
using NBB.Messaging.Host.Builder;
using NBB.Messaging.Host.MessagingPipeline;
using NBB.Messaging.InProcessMessaging.Extensions;
#if AddSamples
using NBB.Worker.Messaging;
#endif
using NBB.Messaging.Abstractions;
#if NatsMessagingTransport
using NBB.Messaging.Nats;
Expand All @@ -32,7 +21,7 @@
using Jaeger.Samplers;
using Jaeger.Reporters;
using Jaeger.Senders;
using OpenTracing.Util;
using OpenTracing.Util;
#endif
#elif KafkaMessagingTransport
using NBB.Messaging.Kafka;
Expand All @@ -45,55 +34,39 @@ namespace NBB.Worker
{
public static class DependencyInjectionExtensions
{
public static void AddWorkerServices(this IServiceCollection services, IConfiguration configuration,
bool useTestDoubles = false)
public static void AddWorkerServices(this IServiceCollection services, IConfiguration configuration)
{
#if MediatR
// MediatR
services.AddMediatR(typeof(__AHandler__).Assembly);
services.AddMediatR(typeof(HelloWorld.Command).Assembly);
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>));
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPostProcessorBehavior<,>));
services.Scan(scan => scan.FromAssemblyOf<__AHandler__>()
services.Scan(scan => scan.FromAssemblyOf<HelloWorld.Handler>()
.AddClasses(classes => classes.AssignableTo(typeof(IPipelineBehavior<,>)))
.AsImplementedInterfaces()
.WithScopedLifetime());
#endif

// Messaging
if (useTestDoubles)
{
services.AddInProcessMessaging();
}
else
{
#if NatsMessagingTransport
services.AddNatsMessaging();
services.AddNatsMessaging();
#elif KafkaMessagingTransport
services.AddKafkaMessaging();
services.AddKafkaMessaging();
#endif
}


#if (Resiliency)
services.AddResiliency();
#endif
#if AddSamples
services.Decorate<IMessageBusPublisher, SamplePublisherDecorator>();
#endif

#if OpenTracing
services.Decorate<IMessageBusPublisher, OpenTracingPublisherDecorator>();
#endif
services.AddMessagingHost()
.AddSubscriberServices(selector =>
{
#if MediatR
selector
.FromMediatRHandledCommands()
.AddClassesAssignableTo<Command>();
#else
selector
.FromAssemblyOf<__ACommand__>()
.AddClassesAssignableTo<Command>();
#endif
.AddAllClasses();
})
.WithDefaultOptions()
.UsePipeline(builder => builder
Expand All @@ -105,28 +78,10 @@ public static void AddWorkerServices(this IServiceCollection services, IConfigur
#if Resiliency
.UseDefaultResiliencyMiddleware()
#endif
#if MediatR
.UseMediatRMiddleware()
#endif
#if AddSamples
.UseMiddleware<SampleSubscriberPipelineMiddleware>()
#endif
);


#if FluentValidation
// Validation
services.Scan(scan => scan.FromAssemblyOf<__AValidator__>()
.AddClasses(classes => classes.AssignableTo<IValidator>())
.AsImplementedInterfaces()
.WithScopedLifetime());
#endif

#if AutoMapper
// AutoMapper
services.AddAutoMapper(typeof(__AMappingProfile__).Assembly);
#endif

#if OpenTracing
// OpenTracing
services.AddOpenTracing();
Expand Down
4 changes: 2 additions & 2 deletions template/Worker/content/NBB.Worker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
FROM microsoft/dotnet:3.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.2-sdk AS build
FROM microsoft/dotnet:3.1-sdk AS build
WORKDIR /src
COPY NuGet.config .
COPY dependencies.props .
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if AddSamples
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;
using NBB.Messaging.Abstractions;
using NBB.Messaging.DataContracts;
using System;
Expand Down Expand Up @@ -32,5 +31,4 @@ void NewCustomizer(MessagingEnvelope outgoingEnvelope)
return _inner.PublishAsync(message, cancellationToken, NewCustomizer);
}
}
}
#endif
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if AddSamples
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;
using NBB.Core.Pipeline;
using NBB.Messaging.DataContracts;
using System;
Expand All @@ -23,5 +22,4 @@ public async Task Invoke(MessagingEnvelope message, CancellationToken cancellati
await next();
}
}
}
#endif
}
Loading

0 comments on commit 076a194

Please sign in to comment.