Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Refactor to reduce complexity (#512)

Leendert de Borst48281f92e652
diff --git a/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs b/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs
index 2f63a0671..6a6f88704 100644
--- a/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs
+++ b/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs
@@ -76,7 +76,7 @@ public class TestHostBuilder : AbstractTestHostBuilder
/// Configures the SMTP services for the test host.
/// </summary>
/// <param name="services">The service collection to configure.</param>
- private void ConfigureSmtpServices(IServiceCollection services)
+ private static void ConfigureSmtpServices(IServiceCollection services)
{
services.AddSingleton(new Config
{
diff --git a/src/Utilities/AliasVault.WorkerStatus/ServiceExtensions/StatusHostedService.cs b/src/Utilities/AliasVault.WorkerStatus/ServiceExtensions/StatusHostedService.cs
index 308c681f1..130c4b335 100644
--- a/src/Utilities/AliasVault.WorkerStatus/ServiceExtensions/StatusHostedService.cs
+++ b/src/Utilities/AliasVault.WorkerStatus/ServiceExtensions/StatusHostedService.cs
@@ -64,6 +64,31 @@ public class StatusHostedService<T>(ILogger<StatusHostedService<T>> logger, Glob
}
}
+ /// <summary>
+ /// Calls the ExecuteAsync method of the inner service.
+ /// </summary>
+ /// <param name="innerService">The inner service.</param>
+ /// <param name="cancellationToken">Cancellation token.</param>
+ private static async Task CallExecuteAsync(T innerService, CancellationToken cancellationToken)
+ {
+ if (innerService is BackgroundService backgroundService)
+ {
+ var executeMethod = backgroundService.GetType().GetMethod("ExecuteAsync", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
+ var executionTask = (Task)executeMethod!.Invoke(backgroundService, new object[] { cancellationToken })!;
+
+ // Wait for the ExecuteAsync method to complete or throw.
+ await executionTask;
+ }
+ else
+ {
+ // For non-BackgroundService implementations, start the service as normal and wait indefinitely
+ await innerService.StartAsync(cancellationToken);
+
+ // For non-BackgroundService implementations, just wait indefinitely
+ await Task.Delay(Timeout.Infinite, cancellationToken);
+ }
+ }
+
/// <summary>
/// Start the inner while loop which adds a second cancellationToken that is controlled by the StatusWorker.
/// </summary>
@@ -102,7 +127,7 @@ public class StatusHostedService<T>(ILogger<StatusHostedService<T>> logger, Glob
}
// Wait for a second before checking the status again.
- await Task.Delay(1000);
+ await Task.Delay(1000, workerCancellationTokenSource.Token);
}
// If we get here, cancel the worker task if it is still running.
@@ -123,22 +148,7 @@ public class StatusHostedService<T>(ILogger<StatusHostedService<T>> logger, Glob
globalServiceStatus.SetWorkerStatus(typeof(T).Name, true);
// If the inner service is a BackgroundService, listen for the results via reflection.
- if (innerService is BackgroundService backgroundService)
- {
- var executeMethod = backgroundService.GetType().GetMethod("ExecuteAsync", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
- var executionTask = (Task)executeMethod!.Invoke(backgroundService, new object[] { cancellationToken })!;
-
- // Wait for the ExecuteAsync method to complete or throw.
- await executionTask;
- }
- else
- {
- // For non-BackgroundService implementations, start the service as normal and wait indefinitely
- await innerService.StartAsync(cancellationToken);
-
- // For non-BackgroundService implementations, just wait indefinitely
- await Task.Delay(Timeout.Infinite, cancellationToken);
- }
+ await CallExecuteAsync(innerService, cancellationToken);
}
catch (OperationCanceledException ex)
{
diff --git a/src/Utilities/AliasVault.WorkerStatus/StatusWorker.cs b/src/Utilities/AliasVault.WorkerStatus/StatusWorker.cs
index f9d5136dc..fc302c19e 100644
--- a/src/Utilities/AliasVault.WorkerStatus/StatusWorker.cs
+++ b/src/Utilities/AliasVault.WorkerStatus/StatusWorker.cs
@@ -13,7 +13,7 @@ using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
/// <summary>
-/// StatusWorker class for monitoring and controlling the status of the worker services.
+/// StatusWorker class for monitoring and controlling the status of individual worker services through a database.
/// </summary>
public class StatusWorker(ILogger<StatusWorker> logger, Func<IWorkerStatusDbContext> createDbContext, GlobalServiceStatus globalServiceStatus) : BackgroundService
{
@@ -33,40 +33,18 @@ public class StatusWorker(ILogger<StatusWorker> logger, Func<IWorkerStatusDbCont
try
{
var statusEntry = await GetServiceStatus();
+
switch (statusEntry.CurrentStatus.ToStatusEnum())
{
case Status.Started:
// Ensure that all workers are running, if not, revert to "Starting" CurrentStatus.
- if (!globalServiceStatus.AreAllWorkersRunning())
- {
- await SetServiceStatus(statusEntry, Status.Starting.ToString());
- logger.LogInformation("Status was set to Started but not all workers are running (yet). Reverting to Starting.");
- }
-
+ await HandleStartedStatus(statusEntry);
break;
case Status.Starting:
- if (globalServiceStatus.AreAllWorkersRunning())
- {
- await SetServiceStatus(statusEntry, Status.Started.ToString());
- logger.LogInformation("All workers started.");
- }
- else
- {
- logger.LogInformation("Waiting for all workers to start.");
- }
-
+ await HandleStartingStatus(statusEntry);
break;
case Status.Stopping:
- if (globalServiceStatus.AreAllWorkersStopped())
- {
- await SetServiceStatus(statusEntry, Status.Stopped.ToString());
- logger.LogInformation("All workers stopped.");
- }
- else
- {
- logger.LogInformation("Waiting for all workers to stop.");
- }
-
+ await HandleStoppingStatus(statusEntry);
break;
case Status.Stopped:
logger.LogInformation("Service is (soft) stopped.");
@@ -91,6 +69,56 @@ public class StatusWorker(ILogger<StatusWorker> logger, Func<IWorkerStatusDbCont
await SetServiceStatus(await GetServiceStatus(), "Stopped");
}
+ /// <summary>
+ /// Handles the Started status.
+ /// </summary>
+ /// <param name="statusEntry">The WorkerServiceStatus entry.</param>
+ /// <returns>Task.</returns>
+ private async Task HandleStartedStatus(WorkerServiceStatus statusEntry)
+ {
+ if (!globalServiceStatus.AreAllWorkersRunning())
+ {
+ await SetServiceStatus(statusEntry, Status.Starting.ToString());
+ logger.LogInformation("Status was set to Started but not all workers are running (yet). Reverting to Starting.");
+ }
+ }
+
+ /// <summary>
+ /// Handles the Starting status.
+ /// </summary>
+ /// <param name="statusEntry">The WorkerServiceStatus entry.</param>
+ /// <returns>Task.</returns>
+ private async Task HandleStartingStatus(WorkerServiceStatus statusEntry)
+ {
+ if (globalServiceStatus.AreAllWorkersRunning())
+ {
+ await SetServiceStatus(statusEntry, Status.Started.ToString());
+ logger.LogInformation("All workers started.");
+ }
+ else
+ {
+ logger.LogInformation("Waiting for all workers to start.");
+ }
+ }
+
+ /// <summary>
+ /// Handles the Stopping status.
+ /// </summary>
+ /// <param name="statusEntry">The WorkerServiceStatus entry.</param>
+ /// <returns>Task.</returns>
+ private async Task HandleStoppingStatus(WorkerServiceStatus statusEntry)
+ {
+ if (globalServiceStatus.AreAllWorkersStopped())
+ {
+ await SetServiceStatus(statusEntry, Status.Stopped.ToString());
+ logger.LogInformation("All workers stopped.");
+ }
+ else
+ {
+ logger.LogInformation("Waiting for all workers to stop.");
+ }
+ }
+
/// <summary>
/// Gets the current status record of the service from database.
/// </summary>