Skip to content

Commit

Permalink
feat: add settings to set concurrent encode limit (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILLIPS71 authored Jun 2, 2024
1 parent f1a010e commit fd6c246
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
using Giantnodes.Service.Encoder.Application.Components.Encoding.Jobs;
using Giantnodes.Service.Encoder.Application.Components.Settings;
using Giantnodes.Service.Encoder.Application.Contracts.Encoding.Jobs;
using MassTransit;
using Microsoft.Extensions.Options;

namespace Giantnodes.Service.Encoder.Application.Components.Encoding.Definitions;

public class EncodeConsumerDefinition : ConsumerDefinition<EncodeFileConsumer>
public class EncodeFileDefinition : ConsumerDefinition<EncodeFileConsumer>
{
private readonly IOptions<LimitSettings> _limits;

public EncodeFileDefinition(IOptions<LimitSettings> limits)
{
_limits = limits;
}

protected override void ConfigureConsumer(
IReceiveEndpointConfigurator endpointConfigurator,
IConsumerConfigurator<EncodeFileConsumer> consumerConfigurator,
Expand All @@ -16,7 +25,7 @@ protected override void ConfigureConsumer(
options
.SetRetry(r => r.Interval(3, TimeSpan.FromSeconds(30)))
.SetJobTimeout(TimeSpan.FromDays(7))
.SetConcurrentJobLimit(3)
.SetConcurrentJobLimit(_limits.Value.MaxConcurrentEncodes)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

namespace Giantnodes.Service.Encoder.Application.Components.Encoding.Definitions;

public class EncodeJobStateMachineDefinition : SagaDefinition<EncodeOperationSagaState>
public class EncodeOperationDefinition : SagaDefinition<EncodeOperationSagaState>
{
protected override void ConfigureSaga(
IReceiveEndpointConfigurator endpointConfigurator,
ISagaConfigurator<EncodeOperationSagaState> sagaConfigurator,
IRegistrationContext context)
{
endpointConfigurator.ConcurrentMessageLimit = 3;

endpointConfigurator.UseMessageRetry(r => r.Interval(3, TimeSpan.FromSeconds(3)));
endpointConfigurator.UseEntityFrameworkOutbox<ApplicationDbContext>(context);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;

namespace Giantnodes.Service.Encoder.Application.Components.Settings;

public sealed class LimitSettings
{
public const string ConfigurationSection = "Limit";

/// <summary>
/// Specifies the maximum number of file encoding jobs that can be processed concurrently.
///
/// It should be carefully chosen based on the available system resources, setting a higher value may improve
/// overall throughput, but it could also lead to resource contention and degraded performance or stability.
/// </summary>
[Required]
public int MaxConcurrentEncodes { get; init; }
}
7 changes: 7 additions & 0 deletions src/Service.Encoder/src/Console/ConsoleServiceRegistration.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;
using Giantnodes.Infrastructure.MassTransit.Filters;
using Giantnodes.Service.Encoder.Application.Components.Settings;
using Giantnodes.Service.Encoder.Persistence.DbContexts;
using MassTransit;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -10,6 +11,12 @@ public static class ConsoleServiceRegistration
{
public static void AddConsoleServices(this IServiceCollection services)
{
services
.AddOptions<LimitSettings>()
.BindConfiguration(LimitSettings.ConfigurationSection)
.ValidateDataAnnotations()
.ValidateOnStart();

services.AddMassTransitServices();
}

Expand Down
3 changes: 3 additions & 0 deletions src/Service.Encoder/src/Console/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"ConnectionStrings": {
"DatabaseConnection": "Host=localhost;Database=Giantnodes.Service.Orchestrator;Username=postgres;Password=password;Include Error Detail=true"
},
"Limit": {
"MaxConcurrentEncodes": 2
},
"Serilog": {
"Using": ["Serilog.Sinks.Console"],
"MinimumLevel": {
Expand Down
3 changes: 3 additions & 0 deletions src/Service.Encoder/src/Console/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"ConnectionStrings": {
"DatabaseConnection": "Host=localhost;Database=Giantnodes.Service.Orchestrator;Username=postgres;Password=password;Include Error Detail=true"
},
"Limit": {
"MaxConcurrentEncodes": 2
},
"Serilog": {
"Using": ["Serilog.Sinks.Console"],
"MinimumLevel": {
Expand Down

0 comments on commit fd6c246

Please sign in to comment.