Skip to content

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

Update WorkerStatus blazor to auto refresh (#126)

Leendert de Borstcdea2106b3b7
diff --git a/src/AliasVault.Admin/Main/Components/WorkerStatus/Services.razor b/src/AliasVault.Admin/Main/Components/WorkerStatus/Services.razor
index e1d8e4394..5ff2a1447 100644
--- a/src/AliasVault.Admin/Main/Components/WorkerStatus/Services.razor
+++ b/src/AliasVault.Admin/Main/Components/WorkerStatus/Services.razor
@@ -1,10 +1,13 @@
@using AliasVault.WorkerStatus.Database
@inherits MainBase
+@implements IDisposable
<button @onclick="SmtpClick"
- class="@GetSmtpButtonClasses() mx-3 inline-flex items-center justify-center rounded-xl px-8 py-2 text-white">
+ class="@GetSmtpButtonClasses() mx-3 inline-flex items-center justify-center rounded-xl px-8 py-2 text-white"
+ disabled="@(!IsHeartbeatValid())"
+ title="@GetButtonTooltip()">
<span>SmtpService</span>
- @if (smtpPending)
+ @if (SmtpPending)
{
<svg class="animate-spin ml-2 h-5 w-5 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
@@ -14,22 +17,62 @@
</button>
@code {
- private List<WorkerServiceStatus> serviceStatus = [];
- private bool initInProgress;
- private bool firstInitPageDone;
+ private List<WorkerServiceStatus> ServiceStatus = [];
+ private bool InitInProgress;
- private bool smtpStatus;
- private bool smtpPending;
+ private bool SmtpStatus;
+ private bool SmtpPending;
+ private DateTime LastHeartbeat;
+ /// <summary>
+ /// The interval in milliseconds for refreshing the service status.
+ /// </summary>
+ private int AutoRefreshInterval = 5000;
+
+ /// <summary>
+ /// The timer for refreshing the service status.
+ /// </summary>
+ private Timer? Timer;
+
+ /// <inheritdoc />
+ protected override async Task OnAfterRenderAsync(bool firstRender)
+ {
+ if (firstRender)
+ {
+ await InitPage();
+
+ Timer = new Timer(async _ =>
+ {
+ await InitPage();
+ }, null, TimeSpan.Zero, TimeSpan.FromMilliseconds(AutoRefreshInterval));
+ }
+ }
+
+ /// <inheritdoc />
+ protected override void Dispose(bool disposing)
+ {
+ base.Dispose(disposing);
+
+ if (disposing)
+ {
+ // Dispose of the timer if it exists.
+ Timer?.Dispose();
+ }
+ }
+
+ /// <summary>
+ /// Gets the CSS classes for the SMTP button based on its current state.
+ /// </summary>
+ /// <returns>A string containing the CSS classes for the button.</returns>
private string GetSmtpButtonClasses()
{
string buttonClass = "cursor-pointer ";
- if (smtpPending || !firstInitPageDone)
+ if (!IsHeartbeatValid())
{
buttonClass += "bg-gray-600";
}
- else if (smtpStatus)
+ else if (SmtpStatus)
{
buttonClass += "bg-green-600";
}
@@ -41,39 +84,68 @@
return buttonClass;
}
+ /// <summary>
+ /// Gets the tooltip text for the SMTP button.
+ /// </summary>
+ /// <returns>A string containing the tooltip text.</returns>
+ private string GetButtonTooltip()
+ {
+ return IsHeartbeatValid() ? "" : "Heartbeat offline";
+ }
+
+ /// <summary>
+ /// Checks if the heartbeat is valid (within the last 5 minutes).
+ /// </summary>
+ /// <returns>True if the heartbeat is valid, false otherwise.</returns>
+ private bool IsHeartbeatValid()
+ {
+ return DateTime.Now <= LastHeartbeat.AddMinutes(5);
+ }
+
+ /// <summary>
+ /// Handles the click event for the SMTP button.
+ /// </summary>
private async void SmtpClick()
{
- smtpPending = true;
+ if (!IsHeartbeatValid())
+ {
+ return;
+ }
+
+ SmtpPending = true;
StateHasChanged();
- smtpStatus = !smtpStatus;
- await UpdateSmtpStatus(smtpStatus);
+ SmtpStatus = !SmtpStatus;
+ await UpdateSmtpStatus(SmtpStatus);
- smtpPending = false;
+ SmtpPending = false;
StateHasChanged();
}
+ /// <summary>
+ /// Initializes the page by fetching service statuses and updating the SMTP status.
+ /// </summary>
private async Task InitPage()
{
- if (initInProgress)
+ if (InitInProgress)
{
return;
}
- if (!smtpPending)
+ if (!SmtpPending)
{
- initInProgress = true;
- serviceStatus = await GetServiceStatuses();
+ InitInProgress = true;
+ ServiceStatus = await GetServiceStatuses();
// Service status checks if the status is "Started" and was lastAlive
// (so actually reported itself) in the last 5 minutes.
- var smtpEntry = serviceStatus.Find(x => x.ServiceName == "AliasVault.SmtpService");
+ var smtpEntry = ServiceStatus.Find(x => x.ServiceName == "AliasVault.SmtpService");
if (smtpEntry != null)
{
- smtpStatus = (DateTime.Now <= smtpEntry.Heartbeat.AddMinutes(5) && smtpEntry.CurrentStatus == "Started");
+ LastHeartbeat = smtpEntry.Heartbeat;
+ SmtpStatus = IsHeartbeatValid() && smtpEntry.CurrentStatus == "Started";
}
- initInProgress = false;
- firstInitPageDone = true;
+ InitInProgress = false;
await InvokeAsync(() => StateHasChanged());
}
@@ -83,15 +155,6 @@
}
}
- /// <inheritdoc />
- protected override async Task OnAfterRenderAsync(bool firstRender)
- {
- if (firstRender)
- {
- await InitPage();
- }
- }
-
/// <summary>
/// Gets the service statuses.
/// </summary>
@@ -122,7 +185,7 @@
entry.DesiredStatus = translateStatus;
await DbContext.SaveChangesAsync();
- // Wait for service to have stopped with ACK
+ // Wait for service to have updated its status.
var timeout = DateTime.Now.AddSeconds(30);
while (true)
{