Skip to content

Commit

Permalink
defer closing the fw project with a using statement. Make SendReceive…
Browse files Browse the repository at this point in the history
… async
  • Loading branch information
hahn-kev committed Nov 6, 2024
1 parent 935c565 commit 919efb8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
12 changes: 6 additions & 6 deletions backend/FwHeadless/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@
logger.LogDebug("crdtFile: {crdtFile}", crdtFile);
logger.LogDebug("fwDataFile: {fwDataFile}", fwDataProject.FilePath);

var fwdataApi = SetupFwData(fwDataProject, srService, projectCode, logger, fwDataFactory);
var fwdataApi = await SetupFwData(fwDataProject, srService, projectCode, logger, fwDataFactory);
using var deferCloseFwData = fwDataFactory.DeferClose(fwDataProject);
var crdtProject = await SetupCrdtProject(crdtFile, projectLookupService, projectId, projectsService, projectFolder, fwdataApi.ProjectId, config.Value.LexboxUrl);

var miniLcmApi = await services.OpenCrdtProject(crdtProject);
Expand All @@ -96,28 +97,27 @@

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

await crdtSyncService.Sync();
var srResult2 = srService.SendReceive(fwDataProject, projectCode);
var srResult2 = await srService.SendReceive(fwDataProject, projectCode);
logger.LogInformation("Send/Receive result after CRDT sync: {srResult2}", srResult2.Output);
return TypedResults.Ok(result);
}

static FwDataMiniLcmApi SetupFwData(FwDataProject fwDataProject,
static async Task<FwDataMiniLcmApi> SetupFwData(FwDataProject fwDataProject,
SendReceiveService srService,
string projectCode,
ILogger<Program> logger,
FwDataFactory fwDataFactory)
{
if (File.Exists(fwDataProject.FilePath))
{
var srResult = srService.SendReceive(fwDataProject, projectCode);
var srResult = await srService.SendReceive(fwDataProject, projectCode);
logger.LogInformation("Send/Receive result: {srResult}", srResult.Output);
}
else
{
var srResult = srService.Clone(fwDataProject, projectCode);
var srResult = await srService.Clone(fwDataProject, projectCode);
logger.LogInformation("Send/Receive result: {srResult}", srResult.Output);
}

Expand Down
16 changes: 10 additions & 6 deletions backend/FwHeadless/SendReceiveHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ public SendReceiveAuth(FwHeadlessConfig config) : this(config.LexboxUsername, co

public record LfMergeBridgeResult(string Output, string ProgressMessages);

private static LfMergeBridgeResult CallLfMergeBridge(string method, IDictionary<string, string> flexBridgeOptions)
private static async Task<LfMergeBridgeResult> CallLfMergeBridge(string method, IDictionary<string, string> flexBridgeOptions)
{
var progress = new StringBuilderProgress();
LfMergeBridge.LfMergeBridge.Execute(method, progress, flexBridgeOptions.ToDictionary(), out var lfMergeBridgeOutputForClient);
var lfMergeBridgeOutputForClient = await Task.Run(() =>
{
LfMergeBridge.LfMergeBridge.Execute(method, progress, flexBridgeOptions.ToDictionary(), out var output);
return output;
});
return new LfMergeBridgeResult(lfMergeBridgeOutputForClient, progress.ToString());
}

Expand All @@ -45,7 +49,7 @@ private static Uri BuildSendReceiveUrl(string baseUrl, string projectCode, SendR
return builder.Uri;
}

public static LfMergeBridgeResult SendReceive(FwDataProject project, string? projectCode = null, string baseUrl = "http://localhost", SendReceiveAuth? auth = null, string fdoDataModelVersion = "7000072", string? commitMessage = null)
public static async Task<LfMergeBridgeResult> SendReceive(FwDataProject project, string? projectCode = null, string baseUrl = "http://localhost", SendReceiveAuth? auth = null, string fdoDataModelVersion = "7000072", string? commitMessage = null)
{
projectCode ??= project.Name;
var fwdataInfo = new FileInfo(project.FilePath);
Expand All @@ -65,10 +69,10 @@ public static LfMergeBridgeResult SendReceive(FwDataProject project, string? pro
{ "user", "LexBox" },
};
if (commitMessage is not null) flexBridgeOptions["commitMessage"] = commitMessage;
return CallLfMergeBridge("Language_Forge_Send_Receive", flexBridgeOptions);
return await CallLfMergeBridge("Language_Forge_Send_Receive", flexBridgeOptions);
}

public static LfMergeBridgeResult CloneProject(FwDataProject project, string? projectCode = null, string baseUrl = "http://localhost", SendReceiveAuth? auth = null, string fdoDataModelVersion = "7000072")
public static async Task<LfMergeBridgeResult> CloneProject(FwDataProject project, string? projectCode = null, string baseUrl = "http://localhost", SendReceiveAuth? auth = null, string fdoDataModelVersion = "7000072")
{
projectCode ??= project.Name;
var fwdataInfo = new FileInfo(project.FilePath);
Expand All @@ -84,6 +88,6 @@ public static LfMergeBridgeResult CloneProject(FwDataProject project, string? pr
{ "languageDepotRepoUri", repoUrl.ToString() },
{ "deleteRepoIfNoSuchBranch", "false" },
};
return CallLfMergeBridge("Language_Forge_Clone", flexBridgeOptions);
return await CallLfMergeBridge("Language_Forge_Clone", flexBridgeOptions);
}
}
8 changes: 4 additions & 4 deletions backend/FwHeadless/SendReceiveService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace FwHeadless;

public class SendReceiveService(IOptions<FwHeadlessConfig> config)
{
public SendReceiveHelpers.LfMergeBridgeResult SendReceive(FwDataProject project, string? projectCode, string? commitMessage = null)
public async Task<SendReceiveHelpers.LfMergeBridgeResult> SendReceive(FwDataProject project, string? projectCode, string? commitMessage = null)
{
return SendReceiveHelpers.SendReceive(
return await SendReceiveHelpers.SendReceive(
project: project,
projectCode: projectCode,
baseUrl: config.Value.HgWebUrl,
Expand All @@ -17,9 +17,9 @@ public SendReceiveHelpers.LfMergeBridgeResult SendReceive(FwDataProject project,
);
}

public SendReceiveHelpers.LfMergeBridgeResult Clone(FwDataProject project, string? projectCode)
public async Task<SendReceiveHelpers.LfMergeBridgeResult> Clone(FwDataProject project, string? projectCode)
{
return SendReceiveHelpers.CloneProject(
return await SendReceiveHelpers.CloneProject(
project: project,
projectCode: projectCode,
baseUrl: config.Value.HgWebUrl,
Expand Down
6 changes: 6 additions & 0 deletions backend/FwLite/FwDataMiniLcmBridge/FwDataFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FwDataMiniLcmBridge.Api;
using FwDataMiniLcmBridge.LcmUtils;
using LexCore.Utils;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -130,4 +131,9 @@ public void CloseProject(FwDataProject project)
if (lcmCache is null) return;
cache.Remove(cacheKey);
}

public IDisposable DeferClose(FwDataProject project)
{
return Defer.Action(() => CloseProject(project));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<InternalsVisibleTo Include="LcmDebugger" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\LexCore\LexCore.csproj" />
<ProjectReference Include="..\MiniLcm\MiniLcm.csproj" />
</ItemGroup>

Expand Down

0 comments on commit 919efb8

Please sign in to comment.