adding
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
using LCU.API.Internal.DocumentLib.Containers;
|
||||
using OwnerNumberMigration.ValueParsing;
|
||||
|
||||
namespace OwnerNumberMigration.Migrations;
|
||||
|
||||
public class AccountNumberMigration : AttributeMigrationTask<string>
|
||||
{
|
||||
public AccountNumberMigration
|
||||
(
|
||||
AttributeMigrationFactory<string> factory,
|
||||
IParsingStrategyFactory strategyFactory,
|
||||
IMigrationManager manager
|
||||
)
|
||||
: base
|
||||
(
|
||||
"AccountNumber",
|
||||
nameof(DocumentInformation.AccountNumber),
|
||||
factory,
|
||||
strategyFactory,
|
||||
manager
|
||||
)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using OwnerNumberMigration.ValueParsing;
|
||||
|
||||
namespace OwnerNumberMigration.Migrations;
|
||||
|
||||
public abstract class AttributeMigrationTask<T> : IHostedService
|
||||
{
|
||||
private readonly IAttributeMigration _migration;
|
||||
private readonly CancellationTokenSource _cancellation = new();
|
||||
private readonly IMigrationManager _manager;
|
||||
|
||||
private Task? _runningMigration;
|
||||
|
||||
protected AttributeMigrationTask
|
||||
(
|
||||
string sourceAttribute,
|
||||
string destinationAttribute,
|
||||
AttributeMigrationFactory<T> factory,
|
||||
IParsingStrategyFactory strategyFactory,
|
||||
IMigrationManager manager
|
||||
)
|
||||
{
|
||||
_manager = manager;
|
||||
_migration = factory
|
||||
(
|
||||
sourceAttribute,
|
||||
strategyFactory.CreateParsingStrategy<T>(sourceAttribute),
|
||||
destinationAttribute
|
||||
);
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested) return Task.CompletedTask;
|
||||
|
||||
_manager.Start(_runningMigration = _migration.Migrate(_cancellation.Token));
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_runningMigration is not null)
|
||||
{
|
||||
if (!_runningMigration.IsCompleted)
|
||||
{
|
||||
_cancellation.Cancel();
|
||||
try
|
||||
{
|
||||
await _runningMigration;
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
_runningMigration.Dispose();
|
||||
}
|
||||
|
||||
_cancellation.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace OwnerNumberMigration.Migrations;
|
||||
|
||||
public interface IMigrationManager
|
||||
{
|
||||
public void Start(Task migrationTask);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace OwnerNumberMigration.Migrations;
|
||||
|
||||
public class MigrationManager : IMigrationManager
|
||||
{
|
||||
private readonly IHostApplicationLifetime _appLifetime;
|
||||
private readonly IList<Task> _migrationTasks = new List<Task>();
|
||||
|
||||
private static readonly IReadOnlySet<TaskStatus> CompletedStatuses = new HashSet<TaskStatus>
|
||||
{
|
||||
TaskStatus.Faulted,
|
||||
TaskStatus.Canceled,
|
||||
TaskStatus.RanToCompletion
|
||||
};
|
||||
|
||||
public MigrationManager(IHostApplicationLifetime appLifetime)
|
||||
{
|
||||
_appLifetime = appLifetime;
|
||||
}
|
||||
|
||||
public void Start(Task migrationTask)
|
||||
{
|
||||
_migrationTasks.Add(migrationTask);
|
||||
|
||||
migrationTask.ContinueWith(_ =>
|
||||
{
|
||||
lock (_migrationTasks)
|
||||
{
|
||||
if (_migrationTasks.All(t => CompletedStatuses.Contains(t.Status)))
|
||||
{
|
||||
_appLifetime.StopApplication();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
using LCU.API.Internal.DocumentLib.Containers;
|
||||
using OwnerNumberMigration.ValueParsing;
|
||||
|
||||
namespace OwnerNumberMigration.Migrations;
|
||||
|
||||
public class OrganizationNumberMigration : AttributeMigrationTask<string[]>
|
||||
{
|
||||
public OrganizationNumberMigration
|
||||
(
|
||||
AttributeMigrationFactory<string[]> factory,
|
||||
IParsingStrategyFactory strategyFactory,
|
||||
IMigrationManager manager
|
||||
)
|
||||
: base
|
||||
(
|
||||
"OrganizationNumber",
|
||||
nameof(DocumentInformation.OrganizationNumbers),
|
||||
factory,
|
||||
strategyFactory,
|
||||
manager
|
||||
)
|
||||
{
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
using LCU.API.Internal.DocumentLib.Containers;
|
||||
using OwnerNumberMigration.ValueParsing;
|
||||
|
||||
namespace OwnerNumberMigration.Migrations;
|
||||
|
||||
public class OrganizationNumbersMigration : AttributeMigrationTask<string[]>
|
||||
{
|
||||
public OrganizationNumbersMigration
|
||||
(
|
||||
AttributeMigrationFactory<string[]> factory,
|
||||
IParsingStrategyFactory strategyFactory,
|
||||
IMigrationManager manager
|
||||
)
|
||||
: base
|
||||
(
|
||||
"OrganizationNumbers",
|
||||
nameof(DocumentInformation.OrganizationNumbers),
|
||||
factory,
|
||||
strategyFactory,
|
||||
manager
|
||||
)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using LCU.API.Internal.DocumentLib.Containers;
|
||||
using OwnerNumberMigration.ValueParsing;
|
||||
|
||||
namespace OwnerNumberMigration.Migrations;
|
||||
|
||||
public class PersonNumberMigration : AttributeMigrationTask<string[]>
|
||||
{
|
||||
public PersonNumberMigration
|
||||
(
|
||||
AttributeMigrationFactory<string[]> factory,
|
||||
IParsingStrategyFactory strategyFactory,
|
||||
IMigrationManager manager
|
||||
)
|
||||
: base
|
||||
(
|
||||
"PersonNumber",
|
||||
nameof(DocumentInformation.PersonNumbers),
|
||||
factory,
|
||||
strategyFactory,
|
||||
manager
|
||||
)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using LCU.API.Internal.DocumentLib.Containers;
|
||||
using OwnerNumberMigration.ValueParsing;
|
||||
|
||||
namespace OwnerNumberMigration.Migrations;
|
||||
|
||||
public class PersonNumbersMigration : AttributeMigrationTask<string[]>
|
||||
{
|
||||
public PersonNumbersMigration
|
||||
(
|
||||
AttributeMigrationFactory<string[]> factory,
|
||||
IParsingStrategyFactory strategyFactory,
|
||||
IMigrationManager manager
|
||||
)
|
||||
: base
|
||||
(
|
||||
"PersonNumbers",
|
||||
nameof(DocumentInformation.PersonNumbers),
|
||||
factory,
|
||||
strategyFactory,
|
||||
manager
|
||||
)
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user