RemoteItemMover, Startup/Exit handler refactor

This commit is contained in:
2023-07-26 13:51:17 +02:00
parent 3de71cbdbe
commit 0b36fb939c
23 changed files with 210 additions and 110 deletions

View File

@@ -44,12 +44,12 @@ public class LocalItemDeleter : IItemDeleter<ILocalContentProvider>
throw new FileNotFoundException(nativePath);
}
}
catch (Exception e)
catch (Exception ex)
{
_logger.LogDebug(e, "Failed to delete item with path {Path}", nativePath);
_logger.LogDebug(ex, "Failed to delete item with path {Path}", nativePath);
if (!_adminContentAccessorFactory.IsAdminModeSupported
|| e is not UnauthorizedAccessException and not IOException)
|| ex is not UnauthorizedAccessException and not IOException)
{
_logger.LogTrace(
"Admin mode is disabled or exception is not an access denied one, not trying to create {Path} as admin",

View File

@@ -1,28 +1,66 @@
using FileTime.Core.ContentAccess;
using FileTime.Core.Models;
using FileTime.Providers.LocalAdmin;
using Microsoft.Extensions.Logging;
namespace FileTime.Providers.Local;
public class LocalItemMover : IItemMover<ILocalContentProvider>
{
public Task RenameAsync(ILocalContentProvider contentProvider, FullName fullName, FullName newPath)
private readonly IAdminContentAccessorFactory _adminContentAccessorFactory;
private readonly IAdminContentProvider _adminContentProvider;
private readonly ILogger<LocalItemMover> _logger;
public LocalItemMover(
IAdminContentAccessorFactory adminContentAccessorFactory,
IAdminContentProvider adminContentProvider,
ILogger<LocalItemMover> logger)
{
var source = contentProvider.GetNativePath(fullName);
var destination = contentProvider.GetNativePath(newPath);
if (File.Exists(source.Path))
_adminContentAccessorFactory = adminContentAccessorFactory;
_adminContentProvider = adminContentProvider;
_logger = logger;
}
public async Task RenameAsync(ILocalContentProvider contentProvider, FullName fullName, FullName newPath)
{
_logger.LogTrace("Start renaming item {FullName}", fullName);
try
{
File.Move(source.Path, destination.Path);
var source = contentProvider.GetNativePath(fullName);
var destination = contentProvider.GetNativePath(newPath);
if (File.Exists(source.Path))
{
_logger.LogTrace("File exists with path {Path}", fullName);
File.Move(source.Path, destination.Path);
}
else if (Directory.Exists(source.Path))
{
_logger.LogTrace("Directory exists with path {Path}", fullName);
Directory.Move(source.Path, destination.Path);
}
else
{
_logger.LogTrace("No file or directory exists with path {Path}", fullName);
throw new FileNotFoundException(source.Path);
}
}
else if (Directory.Exists(source.Path))
catch (Exception ex)
{
Directory.Move(source.Path, destination.Path);
_logger.LogDebug(ex, "Failed to rename item {From} to {To}", fullName, newPath);
if (!_adminContentAccessorFactory.IsAdminModeSupported
|| ex is not UnauthorizedAccessException and not IOException)
{
_logger.LogTrace(
"Admin mode is disabled or exception is not an access denied one, not trying to rename {Path} as admin",
fullName
);
throw;
}
var adminItemMover = await _adminContentAccessorFactory.CreateAdminItemMoverAsync();
await adminItemMover.RenameAsync(_adminContentProvider, fullName, newPath);
}
else
{
throw new FileNotFoundException(source.Path);
}
return Task.CompletedTask;
}
}

View File

@@ -7,4 +7,5 @@ public interface IAdminContentAccessorFactory
bool IsAdminModeSupported { get; }
Task<IRemoteItemCreator> CreateAdminItemCreatorAsync();
Task<IRemoteItemDeleter> CreateAdminItemDeleterAsync();
Task<IRemoteItemMover> CreateAdminItemMoverAsync();
}

View File

@@ -1,5 +1,6 @@
using System.Diagnostics;
using FileTime.Providers.Remote;
using FileTime.Server.Common;
using InitableService;
namespace FileTime.Providers.LocalAdmin;
@@ -21,30 +22,26 @@ public class AdminContentAccessorFactory : IAdminContentAccessorFactory
public bool IsAdminModeSupported => _adminElevationManager.IsAdminModeSupported;
public async Task<IRemoteItemCreator> CreateAdminItemCreatorAsync()
{
await _adminElevationManager.CreateAdminInstanceIfNecessaryAsync();
var connection = await _adminElevationManager.CreateConnectionAsync();
Debug.Assert(connection != null);
var adminItemCreator = _serviceProvider.GetInitableResolver(
connection,
_adminElevationManager.ProviderName)
.GetRequiredService<IRemoteItemCreator>();
return adminItemCreator;
}
=> await CreateHelperAsync<IRemoteItemCreator>();
public async Task<IRemoteItemDeleter> CreateAdminItemDeleterAsync()
=> await CreateHelperAsync<IRemoteItemDeleter>();
public async Task<IRemoteItemMover> CreateAdminItemMoverAsync()
=> await CreateHelperAsync<IRemoteItemMover>();
private async Task<T> CreateHelperAsync<T>()
where T : class, IInitable<IRemoteConnection, string>
{
await _adminElevationManager.CreateAdminInstanceIfNecessaryAsync();
var connection = await _adminElevationManager.CreateConnectionAsync();
Debug.Assert(connection != null);
var adminItemDeleter = _serviceProvider.GetInitableResolver(
var helper = _serviceProvider.GetInitableResolver(
connection,
_adminElevationManager.ProviderName)
.GetRequiredService<IRemoteItemDeleter>();
return adminItemDeleter;
.GetRequiredService<T>();
return helper;
}
}

View File

@@ -135,10 +135,7 @@ public class AdminElevationManager : IAdminElevationManager, INotifyPropertyChan
//TODO: use other connections too (if there will be any)
ArgumentNullException.ThrowIfNull(_connectionInfo.SignalRBaseUrl);
var connection = await _serviceProvider
.GetAsyncInitableResolver(_connectionInfo.SignalRBaseUrl)
.GetRequiredServiceAsync<SignalRConnection>();
var connection = await SignalRConnection.GetOrCreateForAsync(_connectionInfo.SignalRBaseUrl);
return connection;
}
catch (Exception ex)
@@ -168,13 +165,14 @@ public class AdminElevationManager : IAdminElevationManager, INotifyPropertyChan
return true;
}
public async Task ExitAsync()
public async Task ExitAsync(CancellationToken token = default)
{
if (!StartProcess)
{
_logger.LogTrace("Not stopping admin process as it was not started by this instance");
return;
}
if (!IsAdminInstanceRunning)
{
_logger.LogTrace("Not stopping admin process as it is not running");
@@ -188,10 +186,21 @@ public class AdminElevationManager : IAdminElevationManager, INotifyPropertyChan
await connection.Exit();
_logger.LogInformation("Admin process stopped successfully");
}
catch(Exception ex)
catch (Exception ex)
{
_logger.LogError(ex, "Error stopping admin process, trying to kill it");
_adminProcess?.Kill();
_logger.LogError(ex, "Error stopping admin process");
if (_adminProcess is null) return;
for (var i = 0; i < 150 && !_adminProcess.HasExited; i++)
{
await Task.Delay(10);
}
/*if (!_adminProcess.HasExited)
{
_logger.LogInformation("Admin process dit not stopped, killing it");
_adminProcess.Kill();
}*/
}
}
}

View File

@@ -0,0 +1,10 @@
using FileTime.Core.ContentAccess;
using FileTime.Server.Common;
using InitableService;
namespace FileTime.Providers.Remote;
public interface IRemoteItemMover : IItemMover<IRemoteContentProvider>, IInitable<IRemoteConnection, string>
{
}

View File

@@ -0,0 +1,19 @@
using FileTime.Core.Models;
using FileTime.Server.Common;
namespace FileTime.Providers.Remote;
public class RemoteItemMover : IRemoteItemMover
{
private IRemoteConnection _remoteConnection = null!;
private string _remoteContentProviderId = null!;
public void Init(IRemoteConnection remoteConnection, string remoteContentProviderId)
{
_remoteConnection = remoteConnection;
_remoteContentProviderId = remoteContentProviderId;
}
public async Task RenameAsync(IRemoteContentProvider contentProvider, FullName fullName, FullName newPath)
=> await _remoteConnection.MoveItemAsync(_remoteContentProviderId, fullName, newPath);
}

View File

@@ -5,13 +5,12 @@ namespace FileTime.Providers.Remote;
public static class Startup
{
public static IServiceCollection AddRemoteProviderServices(this IServiceCollection serviceCollection)
{
serviceCollection.TryAddSingleton<IRemoteContentProvider, RemoteContentProvider>();
serviceCollection.TryAddTransient<IRemoteItemCreator, RemoteItemCreator>();
serviceCollection.TryAddTransient<IRemoteItemDeleter, RemoteItemDeleter>();
serviceCollection.TryAddTransient<IRemoteItemMover, RemoteItemMover>();
return serviceCollection;
}
}