Skip to content

Commit

Permalink
Prioritize backend processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Guy Fankam committed Jul 2, 2024
1 parent d93572a commit 0e10dad
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
9 changes: 6 additions & 3 deletions tools/code/publisher/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace publisher;

file sealed class RunPublisherHandler(ProcessNamedValuesToPut processNamedValuesToPut,
ProcessDeletedNamedValues processDeletedNamedValues,
ProcessBackendsToPut processBackendsToPut,
ProcessDeletedBackends processDeletedBackends,
GetPublisherFiles getPublisherFiles,
PublishFile publishFile,
ILoggerFactory loggerFactory)
Expand All @@ -24,10 +26,12 @@ file sealed class RunPublisherHandler(ProcessNamedValuesToPut processNamedValues
public async ValueTask Handle(CancellationToken cancellationToken)
{
await processNamedValuesToPut(cancellationToken);
await processBackendsToPut(cancellationToken);

await ProcessPublisherFiles(cancellationToken);

await processDeletedNamedValues(cancellationToken);
await processDeletedBackends(cancellationToken);

logger.LogInformation("Publisher completed.");
}
Expand All @@ -47,7 +51,6 @@ private async ValueTask ProcessPublisherFiles(CancellationToken cancellationToke
file sealed class PublishFileHandler(FindTagAction findTagAction,
FindGatewayAction findGatewayAction,
FindVersionSetAction findVersionSetAction,
FindBackendAction findBackendAction,
FindLoggerAction findLoggerAction,
FindDiagnosticAction findDiagnosticAction,
FindPolicyFragmentAction findPolicyFragmentAction,
Expand Down Expand Up @@ -75,7 +78,6 @@ private Option<PublisherAction> FindPublisherAction(FileInfo file) =>
findTagAction(file)
| findGatewayAction(file)
| findVersionSetAction(file)
| findBackendAction(file)
| findLoggerAction(file)
| findDiagnosticAction(file)
| findPolicyFragmentAction(file)
Expand All @@ -100,6 +102,8 @@ public static void ConfigureRunPublisher(IServiceCollection services)
{
NamedValueServices.ConfigureProcessNamedValuesToPut(services);
NamedValueServices.ConfigureProcessDeletedNamedValues(services);
BackendServices.ConfigureProcessBackendsToPut(services);
BackendServices.ConfigureProcessDeletedBackends(services);
ConfigurePublishFile(services);

services.TryAddSingleton<RunPublisherHandler>();
Expand All @@ -111,7 +115,6 @@ private static void ConfigurePublishFile(IServiceCollection services)
TagServices.ConfigureFindTagAction(services);
GatewayServices.ConfigureFindGatewayAction(services);
VersionSetServices.ConfigureFindVersionSetAction(services);
BackendServices.ConfigureFindBackendAction(services);
LoggerServices.ConfigureFindLoggerAction(services);
DiagnosticServices.ConfigureFindDiagnosticAction(services);
PolicyFragmentServices.ConfigureFindPolicyFragmentAction(services);
Expand Down
47 changes: 42 additions & 5 deletions tools/code/publisher/Backend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace publisher;

internal delegate Option<PublisherAction> FindBackendAction(FileInfo file);
internal delegate ValueTask ProcessBackendsToPut(CancellationToken cancellationToken);
internal delegate ValueTask ProcessDeletedBackends(CancellationToken cancellationToken);

file delegate Option<BackendName> TryParseBackendName(FileInfo file);

Expand All @@ -32,6 +34,30 @@ namespace publisher;

file delegate ValueTask DeleteBackendFromApim(BackendName name, CancellationToken cancellationToken);

file sealed class ProcessBackendsToPutHandler(GetPublisherFiles getPublisherFiles,
TryParseBackendName tryParseBackendName,
IsBackendNameInSourceControl isNameInSourceControl,
PutBackend putBackend)
{
public async ValueTask Handle(CancellationToken cancellationToken) =>
await getPublisherFiles()
.Choose(tryParseBackendName.Invoke)
.Where(isNameInSourceControl.Invoke)
.IterParallel(putBackend.Invoke, cancellationToken);
}

file sealed class ProcessDeletedBackendsHandler(GetPublisherFiles getPublisherFiles,
TryParseBackendName tryParseBackendName,
IsBackendNameInSourceControl isNameInSourceControl,
DeleteBackend deleteBackend)
{
public async ValueTask Handle(CancellationToken cancellationToken) =>
await getPublisherFiles()
.Choose(tryParseBackendName.Invoke)
.Where(name => isNameInSourceControl(name) is false)
.IterParallel(deleteBackend.Invoke, cancellationToken);
}

file sealed class FindBackendActionHandler(TryParseBackendName tryParseName, ProcessBackend processBackend)
{
public Option<PublisherAction> Handle(FileInfo file) =>
Expand Down Expand Up @@ -189,13 +215,14 @@ public async ValueTask Handle(BackendName name, CancellationToken cancellationTo

internal static class BackendServices
{
public static void ConfigureFindBackendAction(IServiceCollection services)
public static void ConfigureProcessBackendsToPut(IServiceCollection services)
{
ConfigureTryParseBackendName(services);
ConfigureProcessBackend(services);
ConfigureIsBackendNameInSourceControl(services);
ConfigurePutBackend(services);

services.TryAddSingleton<FindBackendActionHandler>();
services.TryAddSingleton<FindBackendAction>(provider => provider.GetRequiredService<FindBackendActionHandler>().Handle);
services.TryAddSingleton<ProcessBackendsToPutHandler>();
services.TryAddSingleton<ProcessBackendsToPut>(provider => provider.GetRequiredService<ProcessBackendsToPutHandler>().Handle);
}

private static void ConfigureTryParseBackendName(IServiceCollection services)
Expand Down Expand Up @@ -241,6 +268,16 @@ private static void ConfigurePutBackendInApim(IServiceCollection services)
services.TryAddSingleton<PutBackendInApim>(provider => provider.GetRequiredService<PutBackendInApimHandler>().Handle);
}

public static void ConfigureProcessDeletedBackends(IServiceCollection services)
{
ConfigureTryParseBackendName(services);
ConfigureIsBackendNameInSourceControl(services);
ConfigureDeleteBackend(services);

services.TryAddSingleton<ProcessDeletedBackendsHandler>();
services.TryAddSingleton<ProcessDeletedBackends>(provider => provider.GetRequiredService<ProcessDeletedBackendsHandler>().Handle);
}

private static void ConfigureDeleteBackend(IServiceCollection services)
{
ConfigureDeleteBackendFromApim(services);
Expand Down

0 comments on commit 0e10dad

Please sign in to comment.