diff --git a/src/Service.Encoder/src/Application.Components/Encoding/Definitions/EncodeConsumerDefinition.cs b/src/Service.Encoder/src/Application.Components/Encoding/Definitions/EncodeFileDefinition.cs similarity index 64% rename from src/Service.Encoder/src/Application.Components/Encoding/Definitions/EncodeConsumerDefinition.cs rename to src/Service.Encoder/src/Application.Components/Encoding/Definitions/EncodeFileDefinition.cs index 486a7bf7..afcfe7eb 100644 --- a/src/Service.Encoder/src/Application.Components/Encoding/Definitions/EncodeConsumerDefinition.cs +++ b/src/Service.Encoder/src/Application.Components/Encoding/Definitions/EncodeFileDefinition.cs @@ -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 +public class EncodeFileDefinition : ConsumerDefinition { + private readonly IOptions _limits; + + public EncodeFileDefinition(IOptions limits) + { + _limits = limits; + } + protected override void ConfigureConsumer( IReceiveEndpointConfigurator endpointConfigurator, IConsumerConfigurator consumerConfigurator, @@ -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) ); } } \ No newline at end of file diff --git a/src/Service.Encoder/src/Application.Components/Encoding/Definitions/EncodeJobStateMachineDefinition.cs b/src/Service.Encoder/src/Application.Components/Encoding/Definitions/EncodeOperationDefinition.cs similarity index 80% rename from src/Service.Encoder/src/Application.Components/Encoding/Definitions/EncodeJobStateMachineDefinition.cs rename to src/Service.Encoder/src/Application.Components/Encoding/Definitions/EncodeOperationDefinition.cs index 6462c112..cf6f5fb0 100644 --- a/src/Service.Encoder/src/Application.Components/Encoding/Definitions/EncodeJobStateMachineDefinition.cs +++ b/src/Service.Encoder/src/Application.Components/Encoding/Definitions/EncodeOperationDefinition.cs @@ -4,15 +4,13 @@ namespace Giantnodes.Service.Encoder.Application.Components.Encoding.Definitions; -public class EncodeJobStateMachineDefinition : SagaDefinition +public class EncodeOperationDefinition : SagaDefinition { protected override void ConfigureSaga( IReceiveEndpointConfigurator endpointConfigurator, ISagaConfigurator sagaConfigurator, IRegistrationContext context) { - endpointConfigurator.ConcurrentMessageLimit = 3; - endpointConfigurator.UseMessageRetry(r => r.Interval(3, TimeSpan.FromSeconds(3))); endpointConfigurator.UseEntityFrameworkOutbox(context); } diff --git a/src/Service.Encoder/src/Application.Components/Settings/LimitSettings.cs b/src/Service.Encoder/src/Application.Components/Settings/LimitSettings.cs new file mode 100644 index 00000000..a0975455 --- /dev/null +++ b/src/Service.Encoder/src/Application.Components/Settings/LimitSettings.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace Giantnodes.Service.Encoder.Application.Components.Settings; + +public sealed class LimitSettings +{ + public const string ConfigurationSection = "Limit"; + + /// + /// 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. + /// + [Required] + public int MaxConcurrentEncodes { get; init; } +} \ No newline at end of file diff --git a/src/Service.Encoder/src/Console/ConsoleServiceRegistration.cs b/src/Service.Encoder/src/Console/ConsoleServiceRegistration.cs index 92fad810..2f498b6b 100644 --- a/src/Service.Encoder/src/Console/ConsoleServiceRegistration.cs +++ b/src/Service.Encoder/src/Console/ConsoleServiceRegistration.cs @@ -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; @@ -10,6 +11,12 @@ public static class ConsoleServiceRegistration { public static void AddConsoleServices(this IServiceCollection services) { + services + .AddOptions() + .BindConfiguration(LimitSettings.ConfigurationSection) + .ValidateDataAnnotations() + .ValidateOnStart(); + services.AddMassTransitServices(); } diff --git a/src/Service.Encoder/src/Console/appsettings.Development.json b/src/Service.Encoder/src/Console/appsettings.Development.json index ac7a3177..a246fc89 100644 --- a/src/Service.Encoder/src/Console/appsettings.Development.json +++ b/src/Service.Encoder/src/Console/appsettings.Development.json @@ -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": { diff --git a/src/Service.Encoder/src/Console/appsettings.json b/src/Service.Encoder/src/Console/appsettings.json index cae2be8f..111e9768 100644 --- a/src/Service.Encoder/src/Console/appsettings.json +++ b/src/Service.Encoder/src/Console/appsettings.json @@ -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": {