Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add merge status API to fw-headless #1294

Merged
merged 12 commits into from
Dec 6, 2024
1 change: 1 addition & 0 deletions backend/FwHeadless/FwHeadlessKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static void AddFwHeadless(this IServiceCollection services)
.BindConfiguration("FwHeadlessConfig")
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddSingleton<ProjectSyncStatusService>();
services.AddScoped<SendReceiveService>();
services.AddScoped<ProjectLookupService>();
services.AddScoped<LogSanitizerService>();
Expand Down
18 changes: 17 additions & 1 deletion backend/FwHeadless/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FwHeadless;
using FwHeadless.Services;
using FwDataMiniLcmBridge;
using FwDataMiniLcmBridge.Api;
using FwLiteProjectSync;
Expand Down Expand Up @@ -49,6 +50,7 @@
app.MapHealthChecks("/api/healthz");

app.MapPost("/api/crdt-sync", ExecuteMergeRequest);
app.MapGet("/api/crdt-sync-status", GetMergeStatus);

app.Run();

Expand All @@ -60,6 +62,7 @@ static async Task<Results<Ok<SyncResult>, NotFound, ProblemHttpResult>> ExecuteM
FwDataFactory fwDataFactory,
CrdtProjectsService projectsService,
ProjectLookupService projectLookupService,
ProjectSyncStatusService syncStatusService,
CrdtFwdataProjectSyncService syncService,
CrdtHttpSyncService crdtHttpSyncService,
IHttpClientFactory httpClientFactory,
Expand All @@ -73,6 +76,8 @@ static async Task<Results<Ok<SyncResult>, NotFound, ProblemHttpResult>> ExecuteM
return TypedResults.Ok(new SyncResult(0, 0));
}

using var syncStatusUpdater = new ProjectSyncStatusUpdater(syncStatusService, projectId);

var projectCode = await projectLookupService.GetProjectCode(projectId);
if (projectCode is null)
{
Expand Down Expand Up @@ -103,7 +108,6 @@ static async Task<Results<Ok<SyncResult>, NotFound, ProblemHttpResult>> ExecuteM
var crdtSyncService = services.GetRequiredService<CrdtSyncService>();
await crdtSyncService.Sync();


var result = await syncService.Sync(miniLcmApi, fwdataApi, dryRun);
logger.LogInformation("Sync result, CrdtChanges: {CrdtChanges}, FwdataChanges: {FwdataChanges}", result.CrdtChanges, result.FwdataChanges);

Expand All @@ -113,6 +117,18 @@ static async Task<Results<Ok<SyncResult>, NotFound, ProblemHttpResult>> ExecuteM
return TypedResults.Ok(result);
}

static async Task<Results<Ok<ProjectSyncStatus>, NotFound>> GetMergeStatus(
ProjectLookupService projectLookupService,
ProjectSyncStatusService syncStatusService,
Guid projectId)
{
var status = syncStatusService.SyncStatus(projectId);
hahn-kev marked this conversation as resolved.
Show resolved Hide resolved
if (status is not null) return TypedResults.Ok(status.Value);
// 404 only means "project doesn't exist"; if we don't know the status, then it hasn't synced before and is therefore ready to sync
if (await projectLookupService.ProjectExists(projectId)) return TypedResults.Ok(ProjectSyncStatus.ReadyToSync);
else return TypedResults.NotFound();
}

static async Task<FwDataMiniLcmApi> SetupFwData(FwDataProject fwDataProject,
SendReceiveService srService,
string projectCode,
Expand Down
2 changes: 1 addition & 1 deletion backend/FwHeadless/Services/AppVersionService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Reflection;

namespace FwHeadless;
namespace FwHeadless.Services;

public static class AppVersionService
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using LcmCrdt.RemoteSync;
using SIL.Harmony;

namespace FwHeadless;
namespace FwHeadless.Services;

public class CrdtSyncService(
CrdtHttpSyncService httpSyncService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.EntityFrameworkCore;
using SIL.Harmony.Core;

namespace FwHeadless;
namespace FwHeadless.Services;

public class ProjectLookupService(LexBoxDbContext dbContext)
{
Expand All @@ -15,6 +15,11 @@ public class ProjectLookupService(LexBoxDbContext dbContext)
return projectCode;
}

public async Task<bool> ProjectExists(Guid projectId)
{
return await dbContext.Projects.AnyAsync(p => p.Id == projectId);
}

public async Task<bool> IsCrdtProject(Guid projectId)
{
return await dbContext.Set<ServerCommit>().AnyAsync(c => c.ProjectId == projectId);
Expand Down
47 changes: 47 additions & 0 deletions backend/FwHeadless/Services/ProjectSyncStatusService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections.Concurrent;

namespace FwHeadless.Services;

public class ProjectSyncStatusService()
{
private ConcurrentDictionary<Guid, ProjectSyncStatus> Status { get; init; } = new();

public void StartSyncing(Guid projectId)
{
Status.AddOrUpdate(projectId, (_) => ProjectSyncStatus.Syncing, (_, _) => ProjectSyncStatus.Syncing);
}

public void StopSyncing(Guid projectId)
{
Status.AddOrUpdate(projectId, (_) => ProjectSyncStatus.ReadyToSync, (_, _) => ProjectSyncStatus.ReadyToSync);
hahn-kev marked this conversation as resolved.
Show resolved Hide resolved
}

public ProjectSyncStatus? SyncStatus(Guid projectId)
{
return Status.TryGetValue(projectId, out var status) ? status : null;
}
}

public class ProjectSyncStatusUpdater : IDisposable
hahn-kev marked this conversation as resolved.
Show resolved Hide resolved
{
private readonly Guid _projectId;
private readonly ProjectSyncStatusService _statusService;

public ProjectSyncStatusUpdater(ProjectSyncStatusService statusService, Guid projectId)
{
_projectId = projectId;
_statusService = statusService;
_statusService.StartSyncing(_projectId);
}

public void Dispose()
{
_statusService.StopSyncing(_projectId);
}
}

public enum ProjectSyncStatus
{
ReadyToSync,
Syncing,
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using FwDataMiniLcmBridge;
using SIL.Progress;

namespace FwHeadless;
namespace FwHeadless.Services;

public static class SendReceiveHelpers
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using FwDataMiniLcmBridge;
using FwHeadless.Services;
using Microsoft.Extensions.Options;

namespace FwHeadless;
namespace FwHeadless.Services;

public class SendReceiveService(IOptions<FwHeadlessConfig> config, SafeLoggingProgress progress)
{
Expand Down
Loading