From 0ce3d2710e31357ca701489d766ecea8dd2a67c7 Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Fri, 6 Dec 2024 10:09:51 +0700 Subject: [PATCH] Rename ProjectSyncStatusService so it's clearer It is now called SyncJobStatusService, to better distinguish what it does vs. what the ProjectSyncStatus represents. The job status only checks whether there's an active sync job running, while the project sync status will also check how many commits are on the server waiting to be synced. (If a sync is running right now, then the answer is 0 and we can return without hitting the database). --- backend/FwHeadless/FwHeadlessKernel.cs | 2 +- backend/FwHeadless/Program.cs | 10 +++++----- .../Services/ProjectSyncStatusService.cs | 16 +++++++++++----- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/backend/FwHeadless/FwHeadlessKernel.cs b/backend/FwHeadless/FwHeadlessKernel.cs index 8f4485d74..3b7f64a74 100644 --- a/backend/FwHeadless/FwHeadlessKernel.cs +++ b/backend/FwHeadless/FwHeadlessKernel.cs @@ -17,7 +17,7 @@ public static void AddFwHeadless(this IServiceCollection services) .BindConfiguration("FwHeadlessConfig") .ValidateDataAnnotations() .ValidateOnStart(); - services.AddSingleton(); + services.AddSingleton(); services.AddScoped(); services.AddScoped(); services.AddScoped(); diff --git a/backend/FwHeadless/Program.cs b/backend/FwHeadless/Program.cs index 4b3d03a03..7f737b755 100644 --- a/backend/FwHeadless/Program.cs +++ b/backend/FwHeadless/Program.cs @@ -73,7 +73,7 @@ static async Task, NotFound, ProblemHttpResult>> ExecuteM FwDataFactory fwDataFactory, CrdtProjectsService projectsService, ProjectLookupService projectLookupService, - ProjectSyncStatusService syncStatusService, + SyncJobStatusService syncStatusService, CrdtFwdataProjectSyncService syncService, CrdtHttpSyncService crdtHttpSyncService, IHttpClientFactory httpClientFactory, @@ -132,13 +132,13 @@ static async Task, NotFound, ProblemHttpResult>> ExecuteM static async Task, NotFound>> GetMergeStatus( ProjectContext projectContext, ProjectLookupService projectLookupService, - ProjectSyncStatusService syncStatusService, + SyncJobStatusService syncJobStatusService, IServiceProvider services, LexBoxDbContext lexBoxDb, Guid projectId) { - var status = syncStatusService.SyncStatus(projectId); - if (status is not null) return TypedResults.Ok(new ProjectSyncStatus(status.Value, 0)); + var jobStatus = syncJobStatusService.SyncStatus(projectId); + if (jobStatus == SyncJobStatus.Running) return TypedResults.Ok(ProjectSyncStatus.Syncing); var project = projectContext.Project; if (project is null) { @@ -147,7 +147,7 @@ static async Task, NotFound>> GetMergeStatus( else return TypedResults.NotFound(); } var commitsOnServer = await lexBoxDb.Set().CountAsync(c => c.ProjectId == projectId); - var lcmCrdtDbContext = services.GetRequiredService(); // TODO: This *cannot* be right, can it? + var lcmCrdtDbContext = services.GetRequiredService(); var localCommits = await lcmCrdtDbContext.Set().CountAsync(); return TypedResults.Ok(ProjectSyncStatus.ReadyToSync(commitsOnServer - localCommits)); } diff --git a/backend/FwHeadless/Services/ProjectSyncStatusService.cs b/backend/FwHeadless/Services/ProjectSyncStatusService.cs index 538527972..77d13ca85 100644 --- a/backend/FwHeadless/Services/ProjectSyncStatusService.cs +++ b/backend/FwHeadless/Services/ProjectSyncStatusService.cs @@ -2,13 +2,13 @@ namespace FwHeadless.Services; -public class ProjectSyncStatusService() +public class SyncJobStatusService() { - private ConcurrentDictionary Status { get; init; } = new(); + private ConcurrentDictionary Status { get; init; } = new(); public void StartSyncing(Guid projectId) { - Status.AddOrUpdate(projectId, (_) => ProjectSyncStatusEnum.Syncing, (_, _) => ProjectSyncStatusEnum.Syncing); + Status.AddOrUpdate(projectId, (_) => SyncJobStatus.Running, (_, _) => SyncJobStatus.Running); } public void StopSyncing(Guid projectId) @@ -16,12 +16,18 @@ public void StopSyncing(Guid projectId) Status.Remove(projectId, out var _); } - public ProjectSyncStatusEnum? SyncStatus(Guid projectId) + public SyncJobStatus SyncStatus(Guid projectId) { - return Status.TryGetValue(projectId, out var status) ? status : null; + return Status.TryGetValue(projectId, out var status) ? status : SyncJobStatus.NotRunning; } } +public enum SyncJobStatus +{ + NotRunning, + Running, +} + public enum ProjectSyncStatusEnum { NeverSynced,