Skip to content

Commit

Permalink
feat(clearinghouse): add auto retrigger for END_CLEARINGHOUSE
Browse files Browse the repository at this point in the history
enable maintenance worker to retrigger the clearinghouse process

Refs: #810
  • Loading branch information
Phil91 authored and ntruchsess committed Sep 16, 2024
1 parent 006bb35 commit 05c4895
Show file tree
Hide file tree
Showing 28 changed files with 679 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class PartnerRegistrationSettings
[Required]
[DistinctValues("x => x.ClientId")]
public IEnumerable<UserRoleConfig> InitialRoles { get; set; } = null!;

public int ApplicationsMaxPageSize { get; set; }
}

public static class PartnerRegistrationSettingsExtensions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Org.Eclipse.TractusX.Portal.Backend.Administration.Service.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;

namespace Org.Eclipse.TractusX.Portal.Backend.Administration.Service.Extensions;

public static class CompanyApplicationStatusFilterExtensions
{
public static IEnumerable<CompanyApplicationStatusId> GetCompanyApplicationStatusIds(this CompanyApplicationStatusFilter? companyApplicationStatusFilter) =>
companyApplicationStatusFilter switch
{
CompanyApplicationStatusFilter.Closed =>
[
CompanyApplicationStatusId.CONFIRMED, CompanyApplicationStatusId.DECLINED
],
CompanyApplicationStatusFilter.InReview => [CompanyApplicationStatusId.SUBMITTED],
_ =>
[
CompanyApplicationStatusId.SUBMITTED, CompanyApplicationStatusId.CONFIRMED,
CompanyApplicationStatusId.DECLINED
]
};
}
3 changes: 2 additions & 1 deletion src/administration/Administration.Service/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@
"Scope": "",
"TokenAddress": "",
"BaseAddress": "",
"UseDimWallet": false
"UseDimWallet": false,
"RetriggerEndClearinghouseIntervalInDays": 30
},
"SdFactory": {
"Username": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Microsoft.Extensions.Options;
using Org.Eclipse.TractusX.Portal.Backend.Clearinghouse.Library.Models;
using Org.Eclipse.TractusX.Portal.Backend.Custodian.Library.BusinessLogic;
using Org.Eclipse.TractusX.Portal.Backend.Framework.DateTimeProvider;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Repositories;
Expand All @@ -33,6 +34,7 @@ public class ClearinghouseBusinessLogic(
IClearinghouseService clearinghouseService,
ICustodianBusinessLogic custodianBusinessLogic,
IApplicationChecklistService checklistService,
IDateTimeProvider dateTimeProvider,
IOptions<ClearinghouseSettings> options)
: IClearinghouseBusinessLogic
{
Expand Down Expand Up @@ -136,4 +138,39 @@ public async Task ProcessEndClearinghouse(Guid applicationId, ClearinghouseRespo
? [ProcessStepTypeId.MANUAL_TRIGGER_OVERRIDE_CLEARING_HOUSE]
: [ProcessStepTypeId.START_SELF_DESCRIPTION_LP]);
}

public async Task CheckEndClearinghouseProcesses(CancellationToken stoppingToken)
{
var applicationIds = await portalRepositories.GetInstance<IApplicationChecklistRepository>()
.GetApplicationsForClearinghouseRetrigger(dateTimeProvider.OffsetNow.AddDays(-_settings.RetriggerEndClearinghouseIntervalInDays))
.ToListAsync(stoppingToken).ConfigureAwait(false);

var hasChanges = false;
foreach (var applicationId in applicationIds)
{
var context = await checklistService
.VerifyChecklistEntryAndProcessSteps(
applicationId,
ApplicationChecklistEntryTypeId.CLEARING_HOUSE,
[ApplicationChecklistEntryStatusId.IN_PROGRESS],
ProcessStepTypeId.END_CLEARING_HOUSE)
.ConfigureAwait(ConfigureAwaitOptions.None);

checklistService.FinalizeChecklistEntryAndProcessSteps(
context,
null,
item =>
{
item.ApplicationChecklistEntryStatusId = ApplicationChecklistEntryStatusId.TO_DO;
item.Comment = "Reset to retrigger clearinghouse";
},
[ProcessStepTypeId.START_OVERRIDE_CLEARING_HOUSE]);
hasChanges = true;
}

if (hasChanges)
{
await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ public interface IClearinghouseBusinessLogic
{
Task<IApplicationChecklistService.WorkerChecklistProcessStepExecutionResult> HandleClearinghouse(IApplicationChecklistService.WorkerChecklistProcessStepData context, CancellationToken cancellationToken);
Task ProcessEndClearinghouse(Guid applicationId, ClearinghouseResponseData data, CancellationToken cancellationToken);
Task CheckEndClearinghouseProcesses(CancellationToken stoppingToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ public class ClearinghouseSettings : KeyVaultAuthSettings
public string CallbackUrl { get; set; } = null!;

public bool UseDimWallet { get; set; }

[Required]
public int RetriggerEndClearinghouseIntervalInDays { get; set; }
}
100 changes: 0 additions & 100 deletions src/maintenance/Maintenance.App/BatchDeleteService.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Org.Eclipse.TractusX.Portal.Backend.Maintenance.App.Services;
using System.ComponentModel.DataAnnotations;

namespace Org.Eclipse.TractusX.Portal.Backend.Maintenance.App.DependencyInjection;

/// <summary>
/// Settings for the <see cref="BatchDeleteService"/>
/// </summary>
public class BatchDeleteServiceSettings
{
/// <summary>
/// Documents older than this configured value will be deleted
/// </summary>
[Required]
public int DeleteIntervalInDays { get; set; }
}

/// <summary>
/// Extensions for the <see cref="BatchDeleteService"/>
/// </summary>
public static class BatchDeleteServiceExtensions
{
/// <summary>
/// Adds the <see cref="BatchDeleteService"/> to the service collection
/// </summary>
/// <param name="services">The service collection used for di</param>
/// <param name="section">The configuration section to get the settings from</param>
/// <returns>The enhanced service collection</returns>
public static IServiceCollection AddBatchDelete(this IServiceCollection services, IConfigurationSection section)
{
services.AddOptions<BatchDeleteServiceSettings>().Bind(section);
services
.AddTransient<IBatchDeleteService, BatchDeleteService>();
return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Org.Eclipse.TractusX.Portal.Backend.Framework.DateTimeProvider;
using Org.Eclipse.TractusX.Portal.Backend.Maintenance.App.Services;

namespace Org.Eclipse.TractusX.Portal.Backend.Maintenance.App.DependencyInjection;

/// <summary>
/// Extension methods to register the necessary services for the maintenance job
/// </summary>
public static class MaintenanceServiceExtensions
{
/// <summary>
/// Adds the dependencies for the maintenance service
/// </summary>
/// <param name="services">The service collection</param>
/// <returns>The enhanced service collection</returns>
public static IServiceCollection AddMaintenanceService(this IServiceCollection services) =>
services
.AddTransient<MaintenanceService>()
.AddTransient<IDateTimeProvider, UtcDateTimeProvider>();
}
1 change: 1 addition & 0 deletions src/maintenance/Maintenance.App/Maintenance.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\externalsystems\Clearinghouse.Library\Clearinghouse.Library.csproj" />
<ProjectReference Include="..\..\framework\Framework.Logging\Framework.Logging.csproj" />
<ProjectReference Include="..\..\portalbackend\PortalBackend.PortalEntities\PortalBackend.PortalEntities.csproj" />
<ProjectReference Include="..\..\processes\Processes.ProcessIdentity\Processes.ProcessIdentity.csproj" />
Expand Down
28 changes: 22 additions & 6 deletions src/maintenance/Maintenance.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

using Laraue.EfCoreTriggers.PostgreSql.Extensions;
using Microsoft.EntityFrameworkCore;
using Org.Eclipse.TractusX.Portal.Backend.Clearinghouse.Library;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Logging;
using Org.Eclipse.TractusX.Portal.Backend.Maintenance.App;
using Org.Eclipse.TractusX.Portal.Backend.Maintenance.App.DependencyInjection;
using Org.Eclipse.TractusX.Portal.Backend.Maintenance.App.Services;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Auditing;
using Org.Eclipse.TractusX.Portal.Backend.Processes.ProcessIdentity.DependencyInjection;
Expand All @@ -31,22 +33,36 @@
Log.Information("Building service");
try
{
var host = Host.CreateDefaultBuilder(args)
.UseSystemd()
var host = Host
.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services
.AddMaintenanceService()
.AddConfigurationProcessIdentityIdDetermination(hostContext.Configuration.GetSection("ProcessIdentity"))
.AddBatchDelete(hostContext.Configuration.GetSection("BatchDelete"))
.AddClearinghouseService(hostContext.Configuration.GetSection("Clearinghouse"))
.AddDbAuditing()
.AddDbContext<PortalDbContext>(o =>
o.UseNpgsql(hostContext.Configuration.GetConnectionString("PortalDb"))
.UsePostgreSqlTriggers());
services.AddHostedService<BatchDeleteService>();
.UsePostgreSqlTriggers());
})
.AddLogging()
.Build();
Log.Information("Building worker completed");

await host.RunAsync().ConfigureAwait(ConfigureAwaitOptions.None);
using var tokenSource = new CancellationTokenSource();
Console.CancelKeyPress += (s, e) =>
{
Log.Information("Canceling...");
tokenSource.Cancel();
e.Cancel = true;
};

Log.Information("Start processing");
var workerInstance = host.Services.GetRequiredService<MaintenanceService>();
await workerInstance.ExecuteAsync(tokenSource.Token).ConfigureAwait(ConfigureAwaitOptions.None);
Log.Information("Execution finished shutting down");
}
catch (Exception ex) when (!ex.GetType().Name.Equals("StopTheHostException", StringComparison.Ordinal))
{
Expand Down
Loading

0 comments on commit 05c4895

Please sign in to comment.