Skip to content

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

Code style refactor (#113)

Leendert de Borst4cbedc7034d6
diff --git a/README.md b/README.md
index eab3d32e7..b69c2ca49 100644
--- a/README.md
+++ b/README.md
@@ -67,6 +67,8 @@ The script will output the URL where the app is available. You can now open the
#### Other useful commands:
- To reset the admin password, run the install.sh script with the `--reset-admin-password` flag.
+- To uninstall AliasVault, make the uninstall script executable with `chmod +x uninstall.sh` first, then run the script: `./uninstall.sh`.
+This will remove all containers, images, and volumes related to AliasVault. It will keep all files and configuration intact however, so you can easily reinstall AliasVault later.
## Tech stack / credits
The following technologies, frameworks and libraries are used in this project:
diff --git a/src/Services/AliasVault.SmtpService/Workers/SmtpServerWorker.cs b/src/Services/AliasVault.SmtpService/Workers/SmtpServerWorker.cs
index bcb0286bd..66bae290b 100644
--- a/src/Services/AliasVault.SmtpService/Workers/SmtpServerWorker.cs
+++ b/src/Services/AliasVault.SmtpService/Workers/SmtpServerWorker.cs
@@ -10,11 +10,11 @@ namespace AliasVault.SmtpService.Workers;
public class SmtpServerWorker(ILogger<SmtpServerWorker> logger, SmtpServer.SmtpServer smtpServer) : BackgroundService
{
/// <inheritdoc />
- protected override async Task ExecuteAsync(CancellationToken cancellationToken)
+ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
logger.LogWarning("AliasVault.SmtpService started at: {Time}", DateTimeOffset.Now);
// Start the SMTP server
- await smtpServer.StartAsync(cancellationToken);
+ await smtpServer.StartAsync(stoppingToken);
}
}
diff --git a/src/Utilities/AliasVault.WorkerStatus/Extensions/StatusExtensions.cs b/src/Utilities/AliasVault.WorkerStatus/Extensions/StatusExtensions.cs
new file mode 100644
index 000000000..d0309a93a
--- /dev/null
+++ b/src/Utilities/AliasVault.WorkerStatus/Extensions/StatusExtensions.cs
@@ -0,0 +1,25 @@
+// -----------------------------------------------------------------------
+// <copyright file="StatusExtensions.cs" company="lanedirt">
+// Copyright (c) lanedirt. All rights reserved.
+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+// -----------------------------------------------------------------------
+
+namespace AliasVault.WorkerStatus.Extensions;
+
+/// <summary>
+/// Extension methods for the Status enum.
+/// </summary>
+internal static class StatusExtensions
+{
+ /// <summary>
+ /// String to Status enum conversion.
+ /// </summary>
+ /// <param name="status">Status enum.</param>
+ /// <returns>String as status enum.</returns>
+ /// <exception cref="ArgumentException">Thrown if string is not a known status enum.</exception>
+ public static Status ToStatusEnum(this string status)
+ {
+ return Enum.TryParse(status, out Status result) ? result : throw new ArgumentException("Invalid status value");
+ }
+}
diff --git a/src/Utilities/AliasVault.WorkerStatus/GlobalServiceStatus.cs b/src/Utilities/AliasVault.WorkerStatus/GlobalServiceStatus.cs
index 8e4cea40c..72b075150 100644
--- a/src/Utilities/AliasVault.WorkerStatus/GlobalServiceStatus.cs
+++ b/src/Utilities/AliasVault.WorkerStatus/GlobalServiceStatus.cs
@@ -38,7 +38,7 @@ public class GlobalServiceStatus
/// <summary>
/// Gets or sets the ServiceName in order to identify the service and its workers in the database.
/// </summary>
- public string ServiceName { get; set; } = null!;
+ public string ServiceName { get; set; }
/// <summary>
/// Register a worker with the service.
diff --git a/src/Utilities/AliasVault.WorkerStatus/ServiceExtensions/ServiceCollectionExtensions.cs b/src/Utilities/AliasVault.WorkerStatus/ServiceExtensions/ServiceCollectionExtensions.cs
index 85515362f..8675a3a8e 100644
--- a/src/Utilities/AliasVault.WorkerStatus/ServiceExtensions/ServiceCollectionExtensions.cs
+++ b/src/Utilities/AliasVault.WorkerStatus/ServiceExtensions/ServiceCollectionExtensions.cs
@@ -5,11 +5,10 @@
// </copyright>
//-----------------------------------------------------------------------
-using AliasVault.WorkerStatus.Database;
-using Microsoft.EntityFrameworkCore;
-
namespace AliasVault.WorkerStatus.ServiceExtensions;
+using AliasVault.WorkerStatus.Database;
+using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
diff --git a/src/Utilities/AliasVault.WorkerStatus/ServiceExtensions/StatusHostedService.cs b/src/Utilities/AliasVault.WorkerStatus/ServiceExtensions/StatusHostedService.cs
index d6639672a..e39caacf8 100644
--- a/src/Utilities/AliasVault.WorkerStatus/ServiceExtensions/StatusHostedService.cs
+++ b/src/Utilities/AliasVault.WorkerStatus/ServiceExtensions/StatusHostedService.cs
@@ -17,6 +17,11 @@ using Microsoft.Extensions.Logging;
public class StatusHostedService<T>(ILogger<StatusHostedService<T>> logger, GlobalServiceStatus globalServiceStatus, T innerService) : BackgroundService
where T : IHostedService
{
+ /// <summary>
+ /// Lock object to prevent multiple tasks from starting the worker at the same time.
+ /// </summary>
+ private readonly object _taskLock = new();
+
/// <summary>
/// A minimum delay that is used to wait before restarting the worker after a fault in the innerService.
/// This delay is increased exponentially with a maximum delay of <see cref="_restartMaxDelayInMs"/>.
@@ -33,36 +38,33 @@ public class StatusHostedService<T>(ILogger<StatusHostedService<T>> logger, Glob
/// </summary>
private int _restartDelayInMs = _restartMinDelayInMs;
- private readonly object _taskLock = new();
- private Task? _workerTask;
-
/// <summary>
/// Default entry point called by the host.
/// </summary>
- /// <param name="serviceCancellationToken">Cancellation token.</param>
+ /// <param name="stoppingToken">Cancellation token.</param>
/// <returns>Task.</returns>
- protected override async Task ExecuteAsync(CancellationToken serviceCancellationToken)
+ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
logger.LogInformation("StatusHostedService<{ServiceType}> ExecuteAsync called.", typeof(T).Name);
// Register the service with the global service status so the StatusWorker will monitor it.
globalServiceStatus.RegisterWorker(typeof(T).Name);
- while (!serviceCancellationToken.IsCancellationRequested)
+ while (!stoppingToken.IsCancellationRequested)
{
// Add a second cancellationToken linked to the parent cancellation token.
// When the parent gets canceled this gets canceled as well. However, this one can also
// be canceled with a signal from the StatusWorker.
var workerCancellationTokenSource =
- CancellationTokenSource.CreateLinkedTokenSource(serviceCancellationToken);
+ CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
// Start the inner while loop with the second cancellationToken.
await ExecuteInnerAsync(workerCancellationTokenSource);
- if (!serviceCancellationToken.IsCancellationRequested)
+ if (!stoppingToken.IsCancellationRequested)
{
- // If the worker was not stopped, wait for a second before attempting to restart it.
- await Task.Delay(1000, serviceCancellationToken);
+ // If the parent service was not stopped, wait for a second before attempting to restart the worker.
+ await Task.Delay(1000, stoppingToken);
}
}
}
@@ -73,7 +75,7 @@ public class StatusHostedService<T>(ILogger<StatusHostedService<T>> logger, Glob
/// <param name="workerCancellationTokenSource">Cancellation token.</param>
private async Task ExecuteInnerAsync(CancellationTokenSource workerCancellationTokenSource)
{
- _workerTask = null;
+ Task? workerTask = null;
while (!workerCancellationTokenSource.IsCancellationRequested)
{
@@ -81,10 +83,10 @@ public class StatusHostedService<T>(ILogger<StatusHostedService<T>> logger, Glob
{
lock (_taskLock)
{
- if (_workerTask == null)
+ if (workerTask == null)
{
globalServiceStatus.SetWorkerStatus(typeof(T).Name, true);
- _workerTask = Task.Run(() => WorkerLogic(workerCancellationTokenSource.Token), workerCancellationTokenSource.Token);
+ workerTask = Task.Run(() => WorkerLogic(workerCancellationTokenSource.Token), workerCancellationTokenSource.Token);
}
}
}
@@ -96,7 +98,6 @@ public class StatusHostedService<T>(ILogger<StatusHostedService<T>> logger, Glob
}
else if (globalServiceStatus.CurrentStatus == "Stopped")
{
- _workerTask = null;
break;
}
}
diff --git a/src/Utilities/AliasVault.WorkerStatus/Status.cs b/src/Utilities/AliasVault.WorkerStatus/Status.cs
new file mode 100644
index 000000000..3870f232e
--- /dev/null
+++ b/src/Utilities/AliasVault.WorkerStatus/Status.cs
@@ -0,0 +1,34 @@
+// -----------------------------------------------------------------------
+// <copyright file="Status.cs" company="lanedirt">
+// Copyright (c) lanedirt. All rights reserved.
+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+// -----------------------------------------------------------------------
+
+namespace AliasVault.WorkerStatus;
+
+/// <summary>
+/// Enumeration of possible statuses for a worker service.
+/// </summary>
+public enum Status
+{
+ /// <summary>
+ /// Indicates that the worker service has started.
+ /// </summary>
+ Started,
+
+ /// <summary>
+ /// Indicates that the worker service is starting.
+ /// </summary>
+ Starting,
+
+ /// <summary>
+ /// Indicates that the worker service is stopping.
+ /// </summary>
+ Stopping,
+
+ /// <summary>
+ /// Indicates that the worker service has stopped.
+ /// </summary>
+ Stopped,
+}
diff --git a/src/Utilities/AliasVault.WorkerStatus/StatusWorker.cs b/src/Utilities/AliasVault.WorkerStatus/StatusWorker.cs
index c7d8e796b..2535ebd2e 100644
--- a/src/Utilities/AliasVault.WorkerStatus/StatusWorker.cs
+++ b/src/Utilities/AliasVault.WorkerStatus/StatusWorker.cs
@@ -8,6 +8,7 @@
namespace AliasVault.WorkerStatus;
using AliasVault.WorkerStatus.Database;
+using AliasVault.WorkerStatus.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
@@ -32,35 +33,40 @@ public class StatusWorker(ILogger<StatusWorker> logger, Func<IWorkerStatusDbCont
try
{
var statusEntry = await GetServiceStatus();
- switch (statusEntry.CurrentStatus)
+ switch (statusEntry.CurrentStatus.ToStatusEnum())
{
- case "Started":
+ case Status.Started:
// Ensure that all workers are running, if not, revert to "Starting" CurrentStatus.
if (!globalServiceStatus.AreAllWorkersRunning())
{
- await SetServiceStatus(statusEntry, "Starting");
- logger.LogInformation("Not all workers are running (yet). Reverting to Starting.");
+ await SetServiceStatus(statusEntry, Status.Starting.ToString());
+ logger.LogInformation(
+ "Status was set to Started but not all workers are running (yet). Reverting to Starting.");
}
break;
- case "Starting":
+ case Status.Starting:
await WaitForAllWorkersToStart(stoppingToken);
- await SetServiceStatus(statusEntry, "Started");
+ await SetServiceStatus(statusEntry, Status.Started.ToString());
logger.LogInformation("All workers started.");
break;
- case "Stopping":
+ case Status.Stopping:
await WaitForAllWorkersToStop(stoppingToken);
- await SetServiceStatus(statusEntry, "Stopped");
+ await SetServiceStatus(statusEntry, Status.Stopped.ToString());
logger.LogInformation("All workers stopped.");
break;
- case "Stopped":
+ case Status.Stopped:
logger.LogInformation("Service is (soft) stopped.");
break;
}
}
+ catch (TaskCanceledException)
+ {
+ // Ignore exception, this is expected when the service is stopped.
+ }
catch (Exception e)
{
- logger.LogError(e, "Global main application exception");
+ logger.LogError(e, "StatusWorker exception");
}
await Task.Delay(5000, stoppingToken);
@@ -82,10 +88,10 @@ public class StatusWorker(ILogger<StatusWorker> logger, Func<IWorkerStatusDbCont
if (!string.IsNullOrEmpty(entry.DesiredStatus) && entry.CurrentStatus != entry.DesiredStatus)
{
- entry.CurrentStatus = entry.DesiredStatus switch
+ entry.CurrentStatus = entry.DesiredStatus.ToStatusEnum() switch
{
- "Started" => "Starting",
- "Stopped" => "Stopping",
+ Status.Started => Status.Starting.ToString(),
+ Status.Stopped => Status.Stopping.ToString(),
_ => entry.CurrentStatus,
};
}
@@ -160,7 +166,7 @@ public class StatusWorker(ILogger<StatusWorker> logger, Func<IWorkerStatusDbCont
entry = new WorkerServiceStatus
{
ServiceName = globalServiceStatus.ServiceName,
- CurrentStatus = "Started",
+ CurrentStatus = Status.Started.ToString(),
DesiredStatus = string.Empty,
};
await _dbContext.WorkerServiceStatuses.AddAsync(entry);