Skip to content

Commit

Permalink
Rename ProjectSyncStatusService so it's clearer
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
rmunn committed Dec 6, 2024
1 parent e81d560 commit 0ce3d27
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion backend/FwHeadless/FwHeadlessKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void AddFwHeadless(this IServiceCollection services)
.BindConfiguration("FwHeadlessConfig")
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddSingleton<ProjectSyncStatusService>();
services.AddSingleton<SyncJobStatusService>();
services.AddScoped<SendReceiveService>();
services.AddScoped<ProjectLookupService>();
services.AddScoped<LogSanitizerService>();
Expand Down
10 changes: 5 additions & 5 deletions backend/FwHeadless/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static async Task<Results<Ok<SyncResult>, NotFound, ProblemHttpResult>> ExecuteM
FwDataFactory fwDataFactory,
CrdtProjectsService projectsService,
ProjectLookupService projectLookupService,
ProjectSyncStatusService syncStatusService,
SyncJobStatusService syncStatusService,
CrdtFwdataProjectSyncService syncService,
CrdtHttpSyncService crdtHttpSyncService,
IHttpClientFactory httpClientFactory,
Expand Down Expand Up @@ -132,13 +132,13 @@ static async Task<Results<Ok<SyncResult>, NotFound, ProblemHttpResult>> ExecuteM
static async Task<Results<Ok<ProjectSyncStatus>, 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)
{
Expand All @@ -147,7 +147,7 @@ static async Task<Results<Ok<ProjectSyncStatus>, NotFound>> GetMergeStatus(
else return TypedResults.NotFound();
}
var commitsOnServer = await lexBoxDb.Set<ServerCommit>().CountAsync(c => c.ProjectId == projectId);
var lcmCrdtDbContext = services.GetRequiredService<LcmCrdtDbContext>(); // TODO: This *cannot* be right, can it?
var lcmCrdtDbContext = services.GetRequiredService<LcmCrdtDbContext>();
var localCommits = await lcmCrdtDbContext.Set<Commit>().CountAsync();
return TypedResults.Ok(ProjectSyncStatus.ReadyToSync(commitsOnServer - localCommits));
}
Expand Down
16 changes: 11 additions & 5 deletions backend/FwHeadless/Services/ProjectSyncStatusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,32 @@

namespace FwHeadless.Services;

public class ProjectSyncStatusService()
public class SyncJobStatusService()
{
private ConcurrentDictionary<Guid, ProjectSyncStatusEnum> Status { get; init; } = new();
private ConcurrentDictionary<Guid, SyncJobStatus> 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)
{
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,
Expand Down

0 comments on commit 0ce3d27

Please sign in to comment.