Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions backend/FwHeadless/FwHeadless.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<PackageReference Include="SIL.ChorusPlugin.LfMergeBridge" />
<PackageReference Include="SIL.Chorus.Mercurial" />
<PackageReference Include="SIL.Chorus.ChorusMerge" GeneratePathProperty="true" />
<!-- GeneratePathProperty so we can copy the NewLangProj template out of the package (below).
SIL.LCModel is otherwise a transitive dependency via FwDataMiniLcmBridge. -->
<PackageReference Include="SIL.LCModel" GeneratePathProperty="true" />
</ItemGroup>
<Import Project="$(MSBuildThisFileDirectory)..\Harmony.App.References.props" />
<ItemGroup>
Expand All @@ -40,6 +43,11 @@
<Content Remove="Mercurial\contrib\vs2022-settings.json" />
<Content Include="Mercurial\**" CopyToOutputDirectory="Always" Watch="false" />
<Content Include="MercurialExtensions\**" CopyToOutputDirectory="Always" Watch="false" />
<!-- Ship the LCM project templates (NewLangProj.fwdata etc.) so ProjectCreationService can build a
new project from them. TemplatesFolder is pointed at this output folder in FwHeadlessKernel. -->
<Content Include="$(PkgSIL_LCModel)/contentFiles/Templates/*.*"
Link="Templates/%(Filename)%(Extension)"
CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
<None Include="chorusmerge">
Expand Down
16 changes: 16 additions & 0 deletions backend/FwHeadless/FwHeadlessConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ public class FwHeadlessConfig
public long MaxUploadFileSizeBytes => MaxUploadFileSizeKb * 1024;
public string FdoDataModelVersion { get; init; } = "7000072";

/// <summary>
/// The FLExBridge data version. FieldWorks/LfMergeBridge form the Mercurial branch a project's
/// data lives on by joining this and the FDO model version with a dot (see <see cref="SendReceiveBranchName"/>).
/// Mirrors FlexBridgeConstants.FlexBridgeDataVersion in FLExBridge, which is internal so we can't
/// reference it directly; bump this if FLExBridge bumps its data version.
/// </summary>
public string FlexBridgeDataVersion { get; init; } = "7500002";

/// <summary>
/// The Mercurial branch FieldWorks/LfMergeBridge keep a project's data on: FlexBridgeDataVersion,
/// a dot, then the FDO model version (e.g. "7500002.7000072"). This is exactly the branch name
/// LfMergeBridge derives from <see cref="FdoDataModelVersion"/>, so a new project's genesis commit
/// must land on this branch for send/receive to find the data.
/// </summary>
public string SendReceiveBranchName => $"{FlexBridgeDataVersion}.{FdoDataModelVersion}";

/// <summary>
/// Project directory structure in FwHeadless: (Note that FwDataProject.ProjectsPath is the root of a SINGLE project)
/// {ProjectStorageRoot}/
Expand Down
9 changes: 9 additions & 0 deletions backend/FwHeadless/FwHeadlessKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static IServiceCollection AddFwHeadless(this IServiceCollection services)
services.AddScoped<ISendReceiveService, SendReceiveService>();
services.AddScoped<IProjectLookupService, ProjectLookupService>();
services.AddScoped<ProjectDeletionService>();
services.AddScoped<ProjectCreationService>();
services.AddScoped<LogSanitizerService>();
services.AddScoped<SafeLoggingProgress>();
// Singleton so its file lock is shared across requests and the sync worker
Expand All @@ -37,6 +38,14 @@ public static IServiceCollection AddFwHeadless(this IServiceCollection services)
.AddLcmCrdtClientCore()
.AddFwDataBridge(ServiceLifetime.Scoped)
.AddFwLiteProjectSync();
// The LCM project templates (NewLangProj.fwdata etc.) ship in our output (see FwHeadless.csproj).
// FwDataBridgeConfig's default TemplatesFolder points at the FieldWorks user-data dir, which
// doesn't exist in the container; point it at the shipped templates unless overridden in config.
services.Configure<FwDataBridgeConfig>(config =>
{
if (config.TemplatesFolder == new FwDataBridgeConfig().TemplatesFolder)
config.TemplatesFolder = Path.Combine(AppContext.BaseDirectory, "Templates");
});
services.RemoveAll(typeof(IMediaAdapter));
services.AddScoped<IMediaAdapter, LexboxFwDataMediaAdapter>();
services.AddScoped<MediaFileService>();
Expand Down
1 change: 1 addition & 0 deletions backend/FwHeadless/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
app.MapMediaFileRoutes();
app.MapMergeRoutes();
app.MapLexiconRoutes();
app.MapProjectRoutes();

// DELETE endpoint to delete the FieldWorks repo/project (and nothing else)
app.MapDelete("/api/manage/repo/{projectId}", async (Guid projectId, ProjectDeletionService deletionService) =>
Expand Down
4 changes: 4 additions & 0 deletions backend/FwHeadless/Routes/MergeRoutes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ static async Task<Results<Ok, ProblemHttpResult>> ExecuteMergeRequest(

switch (syncHostedService.QueueJob(projectId))
{
case QueueJobResult.ProjectBeingCreated:
activity?.SetStatus(ActivityStatusCode.Ok, "Project is still being created");
return TypedResults.Problem("Project is still being created, try again once creation has finished",
statusCode: StatusCodes.Status409Conflict);
case QueueJobResult.SyncAlreadyRunning:
activity?.SetStatus(ActivityStatusCode.Ok, "Sync already running");
return TypedResults.Problem("A sync is already running for this project, try again once it has finished",
Expand Down
137 changes: 137 additions & 0 deletions backend/FwHeadless/Routes/ProjectRoutes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
using System.Diagnostics;
using FwHeadless.Services;
using LexCore.Entities;
using LexCore.Sync;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.Extensions.Options;

namespace FwHeadless.Routes;

public static class ProjectRoutes
{
public static IEndpointConventionBuilder MapProjectRoutes(this WebApplication app)
{
var group = app.MapGroup("/api/project");
group.MapPost("/initFwDataProject", InitFwDataProject);
group.MapGet("/creation-status", GetCreationStatus);
group.MapGet("/await-creation-finished", AwaitCreationFinished);
return group;
}

// Internal endpoint: LexBoxApi has already created the project row + empty repo and performed the
// admin-auth check. FwHeadless populates the empty repo with a template .fwdata configured for the
// requested writing systems. Not idempotent — on failure LexBoxApi deletes and recreates.
private static async Task<Results<Ok, ProblemHttpResult>> InitFwDataProject(
Guid projectId,
InitFwDataProjectInput input,
IProjectLookupService projectLookupService,
ProjectCreationService projectCreationService,
ILogger<Program> logger)
{
// Null-safe against a malformed body (System.Text.Json leaves an absent list null).
var wsError = InitFwDataProjectInput.ValidateWritingSystemList("vernacular", input.WsVernacular)
?? InitFwDataProjectInput.ValidateWritingSystemList("analysis", input.WsAnalysis)
?? InitFwDataProjectInput.ValidateUiWritingSystem(input.WsUi);
if (wsError is not null)
return TypedResults.Problem(wsError, statusCode: StatusCodes.Status400BadRequest);

var projectCode = await projectLookupService.GetProjectCode(projectId);
if (projectCode is null)
{
logger.LogError("initFwDataProject request for non-existent project {ProjectId}", projectId);
return TypedResults.Problem("Project not found", statusCode: StatusCodes.Status404NotFound);
}

await projectCreationService.InitFwDataProject(
projectId, projectCode, input.WsVernacular, input.WsAnalysis, input.WsUi);
return TypedResults.Ok();
}

// Instantaneous creation status, for a caller that wants to poll. Mirrors /api/merge/status.
private static async Task<Results<Ok<ProjectCreationStatus>, NotFound>> GetCreationStatus(
Guid projectId,
SyncHostedService syncHostedService,
IProjectLookupService projectLookupService,
IOptions<FwHeadlessConfig> config)
{
using var activity = FwHeadlessActivitySource.Value.StartActivity();
activity?.SetTag("app.project_id", projectId);

var status = await DetermineCreationStatus(projectId, syncHostedService, projectLookupService, config);
if (status is null)
{
activity?.SetStatus(ActivityStatusCode.Error, "Project not found");
return TypedResults.NotFound();
}
activity?.SetStatus(ActivityStatusCode.Ok, status.Status.ToString());
return TypedResults.Ok(status);
}

// Long-poll until an in-flight creation finishes. Mirrors /api/merge/await-finished: giving up on the
// wait is reported as a status, not an error, because the creation itself keeps running either way.
private static async Task<Results<Ok<ProjectCreationStatus>, NotFound>> AwaitCreationFinished(
Guid projectId,
SyncHostedService syncHostedService,
IProjectLookupService projectLookupService,
IOptions<FwHeadlessConfig> config,
CancellationToken cancellationToken)
{
using var activity = FwHeadlessActivitySource.Value.StartActivity();
activity?.SetTag("app.project_id", projectId);

try
{
var result = await syncHostedService.AwaitCreationFinished(projectId, cancellationToken);
if (result is not null)
{
activity?.SetStatus(ActivityStatusCode.Ok, result.Status.ToString());
return TypedResults.Ok(result);
}
}
catch (OperationCanceledException)
{
// The caller's wait was cancelled; the creation was not (it takes no cancellation token).
// Report that distinctly so a caller can't read a timeout as a failed creation.
activity?.SetStatus(ActivityStatusCode.Unset, "Timed out awaiting creation");
return TypedResults.Ok(ProjectCreationStatus.TimedOutAwaitingCreation);
}

// Nothing in flight and nothing remembered, so fall back to what the project itself tells us.
var status = await DetermineCreationStatus(projectId, syncHostedService, projectLookupService, config);
if (status is null)
{
activity?.SetStatus(ActivityStatusCode.Error, "Project not found");
return TypedResults.NotFound();
}
activity?.SetStatus(ActivityStatusCode.Ok, status.Status.ToString());
return TypedResults.Ok(status);
}

/// <summary>
/// Creation status for a project, or null when LexBox has no such project. In-memory state wins
/// when we have it; otherwise the status is derived from durable state, so it survives a FwHeadless
/// restart and a result that has aged out of the cache.
/// </summary>
private static async Task<ProjectCreationStatus?> DetermineCreationStatus(
Guid projectId,
SyncHostedService syncHostedService,
IProjectLookupService projectLookupService,
IOptions<FwHeadlessConfig> config)
{
if (syncHostedService.IsProjectBeingCreated(projectId)) return ProjectCreationStatus.Creating;
var recent = syncHostedService.TryGetRecentCreationResult(projectId);
if (recent is not null) return recent;

var projectCode = await projectLookupService.GetProjectCode(projectId);
if (projectCode is null) return null;

// A successful creation leaves the .fwdata on disk; a failed one deletes the project folder
// (ProjectCreationService.CleanupLocalProject). Note this says "project data is here", not "this
// process created it" -- a project cloned by an ordinary sync looks the same, which is the right
// answer for a caller asking whether the project is ready to use.
var fwDataProject = config.Value.GetFwDataProject(projectCode, projectId);
return File.Exists(fwDataProject.FilePath)
? ProjectCreationStatus.Created
: ProjectCreationStatus.NotCreated;
}
}
2 changes: 2 additions & 0 deletions backend/FwHeadless/Services/ISendReceiveService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface ISendReceiveService
Task<int> PendingCommitCountOutgoing(FwDataProject project, string? projectCode);
Task<int> PendingCommitCountBothWays(FwDataProject project, string? projectCode);
Task CommitFile(string filePath, string commitMessage);
Task InitRepo(string folder);
Task SwitchBranch(string folder, string branchName, bool createBranch = true, string commitMessage = "");
}
Loading
Loading