Skip to content

Enable cancellation for Lift exports #3664

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

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
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
13 changes: 10 additions & 3 deletions Backend.Tests/Controllers/LiftControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ protected virtual void Dispose(bool disposing)
private ILogger<LiftController> _logger = null!;
private string _projId = null!;
private const string ProjName = "LiftControllerTests";
private const string ExportId = "LiftControllerTestExportId";
private const string UserId = "LiftControllerTestUserId";

[SetUp]
Expand Down Expand Up @@ -336,7 +337,8 @@ public async Task TestModifiedTimeExportsToLift()
word.Modified = Time.ToUtcIso8601(new DateTime(2000, 1, 1));
await _wordRepo.Create(word);

await _liftController.CreateLiftExportThenSignal(_projId, UserId);
_liftService.SetExportInProgress(UserId, ExportId);
await _liftController.CreateLiftExportThenSignal(_projId, UserId, ExportId);
var liftContents = await DownloadAndReadLift(_liftController, _projId);
Assert.That(liftContents, Does.Contain("dateCreated=\"1000-01-01T00:00:00Z\""));
Assert.That(liftContents, Does.Contain("dateModified=\"2000-01-01T00:00:00Z\""));
Expand Down Expand Up @@ -376,7 +378,11 @@ public void TestExportInvalidProjectId()
{
const string invalidProjectId = "INVALID_ID";
Assert.That(
async () => await _liftController.CreateLiftExportThenSignal(invalidProjectId, UserId),
async () =>
{
_liftService.SetExportInProgress(UserId, ExportId);
await _liftController.CreateLiftExportThenSignal(invalidProjectId, UserId, ExportId);
},
Throws.TypeOf<MissingProjectException>());
}

Expand Down Expand Up @@ -444,7 +450,8 @@ public async Task TestDeletedWordsExportToLift()
await _wordService.Update(_projId, UserId, wordToUpdate.Id, word);
await _wordService.DeleteFrontierWord(_projId, UserId, wordToDelete.Id);

await _liftController.CreateLiftExportThenSignal(_projId, UserId);
_liftService.SetExportInProgress(UserId, ExportId);
await _liftController.CreateLiftExportThenSignal(_projId, UserId, ExportId);
var text = await DownloadAndReadLift(_liftController, _projId);
// TODO: Add SIL or other XML assertion library and verify with xpath that the correct entries are
// kept vs deleted
Expand Down
15 changes: 14 additions & 1 deletion Backend.Tests/Mocks/PermissionServiceMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,20 @@ public Task<bool> IsViolationEdit(HttpContext request, string userEditId, string
}

/// <param name="request">
/// Note this parameter is nullable in the mock implementation even though the real implementation it is not
/// Note this parameter is nullable in the mock implementation even though the real implementation is not
/// to support unit testing when `HttpContext`s are not available.
/// </param>
public string GetExportId(HttpContext? request)
{
if (request is null)
{
return NoHttpContextAvailable;
}
return "ExportId";
}

/// <param name="request">
/// Note this parameter is nullable in the mock implementation even though the real implementation is not
/// to support unit testing when `HttpContext`s are not available.
/// </param>
public string GetUserId(HttpContext? request)
Expand Down
20 changes: 16 additions & 4 deletions Backend.Tests/Services/LiftServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class LiftServiceTests

private const string FileName = "file.lift-ranges";
private const string ProjId = "LiftServiceTestsProjId";
private const string ExportId = "LiftServiceTestsExportId";
private const string UserId = "LiftServiceTestsUserId";

[SetUp]
Expand All @@ -27,9 +28,9 @@ public void Setup()
public void ExportInProgressTest()
{
Assert.That(_liftService.IsExportInProgress(UserId), Is.False);
_liftService.SetExportInProgress(UserId, true);
_liftService.SetExportInProgress(UserId, ExportId);
Assert.That(_liftService.IsExportInProgress(UserId), Is.True);
_liftService.SetExportInProgress(UserId, false);
_liftService.CancelRecentExport(UserId);
Assert.That(_liftService.IsExportInProgress(UserId), Is.False);
}

Expand All @@ -39,17 +40,28 @@ public void StoreRetrieveDeleteExportTest()
Assert.That(_liftService.RetrieveExport(UserId), Is.Null);
Assert.That(_liftService.DeleteExport(UserId), Is.False);

_liftService.SetExportInProgress(UserId, true);
_liftService.SetExportInProgress(UserId, ExportId);
Assert.That(_liftService.RetrieveExport(UserId), Is.Null);
Assert.That(_liftService.DeleteExport(UserId), Is.True);
Assert.That(_liftService.DeleteExport(UserId), Is.False);

_liftService.StoreExport(UserId, FileName);
_liftService.SetExportInProgress(UserId, ExportId);
_liftService.StoreExport(UserId, FileName, ExportId);
Assert.That(_liftService.RetrieveExport(UserId), Is.EqualTo(FileName));
Assert.That(_liftService.DeleteExport(UserId), Is.True);
Assert.That(_liftService.RetrieveExport(UserId), Is.Null);
}

[Test]
public void StoreOnlyValidExportsTest()
{
_liftService.SetExportInProgress(UserId, ExportId);
_liftService.StoreExport(UserId, FileName, "expiredExportId");
Assert.That(_liftService.RetrieveExport(UserId), Is.Null);
_liftService.StoreExport(UserId, FileName, ExportId);
Assert.That(_liftService.RetrieveExport(UserId), Is.EqualTo(FileName));
}

[Test]
public void StoreRetrieveDeleteImportTest()
{
Expand Down
45 changes: 33 additions & 12 deletions Backend/Controllers/LiftController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,35 @@ private async Task<IActionResult> AddImportToProject(string liftStoragePath, str
return Ok(countWordsImported);
}

/// <summary> Cancels project export </summary>
/// <returns> ProjectId, if cancel successful </returns>
[HttpGet("cancelexport", Name = "CancelLiftExport")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(string))]
public string CancelLiftExport(string projectId)
{
var userId = _permissionService.GetUserId(HttpContext);
CancelLiftExport(projectId, userId);
return projectId;
}

private string CancelLiftExport(string projectId, string userId)
{
_liftService.CancelRecentExport(userId);
return projectId;
}

/// <summary> Packages project data into zip file </summary>
/// <returns> ProjectId, if export successful </returns>
[HttpGet("export", Name = "ExportLiftFile")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(string))]
public async Task<IActionResult> ExportLiftFile(string projectId)
{
var userId = _permissionService.GetUserId(HttpContext);
return await ExportLiftFile(projectId, userId);
var exportId = _permissionService.GetExportId(HttpContext);
return await ExportLiftFile(projectId, userId, exportId);
}

private async Task<IActionResult> ExportLiftFile(string projectId, string userId)
private async Task<IActionResult> ExportLiftFile(string projectId, string userId, string exportId)
{
if (!await _permissionService.HasProjectPermission(HttpContext, Permission.Export, projectId))
{
Expand Down Expand Up @@ -325,43 +343,46 @@ private async Task<IActionResult> ExportLiftFile(string projectId, string userId
}

// Store in-progress status for the export
_liftService.SetExportInProgress(userId, true);
_liftService.SetExportInProgress(userId, exportId);

// Ensure project has words
var words = await _wordRepo.GetAllWords(projectId);
if (words.Count == 0)
{
_liftService.SetExportInProgress(userId, false);
_liftService.CancelRecentExport(userId);
return BadRequest("No words to export.");
}

// Run the task without waiting for completion.
// This Task will be scheduled within the existing Async executor thread pool efficiently.
// See: https://stackoverflow.com/a/64614779/1398841
_ = Task.Run(() => CreateLiftExportThenSignal(projectId, userId));
_ = Task.Run(() => CreateLiftExportThenSignal(projectId, userId, exportId));
return Ok(projectId);
}

// These internal methods are extracted for unit testing.
internal async Task<bool> CreateLiftExportThenSignal(string projectId, string userId)
internal async Task<bool> CreateLiftExportThenSignal(string projectId, string userId, string exportId)
{
// Export the data to a zip, read into memory, and delete zip.
var exportedFilepath = "";
try
{
var exportedFilepath = await CreateLiftExport(projectId);
// Store the temporary path to the exported file for user to download later.
_liftService.StoreExport(userId, exportedFilepath);
await _notifyService.Clients.All.SendAsync(CombineHub.DownloadReady, userId);
return true;
exportedFilepath = await CreateLiftExport(projectId);
}
catch (Exception e)
{
_logger.LogError("Error exporting project {ProjectId}{NewLine}{Message}:{ExceptionStack}",
projectId, Environment.NewLine, e.Message, e.StackTrace);
_liftService.DeleteExport(userId);
await _notifyService.Clients.All.SendAsync(CombineHub.ExportFailed, userId);
throw;
}
// Store the temporary path to the exported file for user to download later.
var proceed = _liftService.StoreExport(userId, exportedFilepath, exportId);
if (proceed)
{
await _notifyService.Clients.All.SendAsync(CombineHub.DownloadReady, userId);
}
return proceed;
}

internal async Task<string> CreateLiftExport(string projectId)
Expand Down
5 changes: 3 additions & 2 deletions Backend/Interfaces/ILiftService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ public interface ILiftService
Task CreateLiftRanges(List<SemanticDomainFull> projDoms, string rangesDest);

// Methods to store, retrieve, and delete an export string in a common dictionary.
void StoreExport(string userId, string filePath);
bool StoreExport(string userId, string filePath, string exportId);
string? RetrieveExport(string userId);
bool DeleteExport(string userId);
void SetExportInProgress(string userId, bool isInProgress);
void CancelRecentExport(string userId);
void SetExportInProgress(string userId, string exportId);
bool IsExportInProgress(string userId);
void StoreImport(string userId, string filePath);
string? RetrieveImport(string userId);
Expand Down
1 change: 1 addition & 0 deletions Backend/Interfaces/IPermissionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public interface IPermissionService
Task<bool> IsSiteAdmin(HttpContext request);
bool IsUserIdAuthorized(HttpContext request, string userId);
Task<bool> IsViolationEdit(HttpContext request, string userEditId, string projectId);
string GetExportId(HttpContext request);
string GetUserId(HttpContext request);
public bool IsCurrentUserAuthorized(HttpContext request);
Task<User?> Authenticate(string emailOrUsername, string password);
Expand Down
57 changes: 40 additions & 17 deletions Backend/Services/LiftService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
Expand All @@ -11,6 +12,7 @@
using BackendFramework.Interfaces;
using BackendFramework.Models;
using MongoDB.Bson;
using MongoDB.Driver;
using SIL.DictionaryServices.Lift;
using SIL.DictionaryServices.Model;
using SIL.Lift;
Expand Down Expand Up @@ -108,9 +110,15 @@ public class LiftService : ILiftService
private readonly ISemanticDomainRepository _semDomRepo;
private readonly ISpeakerRepository _speakerRepo;

/// A dictionary shared by all Projects for storing and retrieving paths to exported projects.
private readonly Dictionary<string, string> _liftExports;
/// <summary>
/// A dictionary shared by all Projects for tracking exported projects.
/// The value is either ("IN_PROGRESS", exportId) or (filePath, exportId), where
/// exportId identifies the currently valid (e.g. not canceled or deleted) export.
/// </summary>
private readonly ConcurrentDictionary<string, (string, string)> _liftExports;
/// <summary>
/// A dictionary shared by all Projects for storing and retrieving paths to in-process imports.
/// </summary>
private readonly Dictionary<string, string> _liftImports;
private const string FlagTextEmpty = "***";
private const string InProgress = "IN_PROGRESS";
Expand All @@ -125,47 +133,62 @@ public LiftService(ISemanticDomainRepository semDomRepo, ISpeakerRepository spea
Sldr.Initialize(true);
}

_liftExports = new Dictionary<string, string>();
_liftExports = new ConcurrentDictionary<string, (string, string)>();
_liftImports = new Dictionary<string, string>();
}

/// <summary> Invalidate most recent export by no longer storing its exportId. </summary>
public void CancelRecentExport(string userId)
{
_liftExports.TryRemove(userId, out var _);
}

/// <summary> Store status that a user's export is in-progress. </summary>
public void SetExportInProgress(string userId, bool isInProgress)
public void SetExportInProgress(string userId, string exportId)
{
_liftExports.Remove(userId);
if (isInProgress)
{
_liftExports.Add(userId, InProgress);
}
ArgumentException.ThrowIfNullOrEmpty(exportId);
_liftExports.AddOrUpdate(userId, (InProgress, exportId), (k, v) => (InProgress, exportId));
}

/// <summary> Query whether user has an in-progress export. </summary>
public bool IsExportInProgress(string userId)
{
_liftExports.TryGetValue(userId, out var exportPath);
_liftExports.TryGetValue(userId, out var tuple);
var exportPath = tuple.Item1;
return exportPath == InProgress;
}

/// <summary> Store filePath for a user's Lift export. </summary>
public void StoreExport(string userId, string filePath)
/// <returns> If the export has not been cancelled, true; otherwise, false. </returns>
public bool StoreExport(string userId, string filePath, string exportId)
{
_liftExports.Remove(userId);
_liftExports.Add(userId, filePath);
// Store the filepath if the export is valid (i.e. not cancelled).
// If the export is no longer valid, clean up any file created for it.
var valid = _liftExports.TryUpdate(userId, (filePath, exportId), (InProgress, exportId));
if (!valid && File.Exists(filePath))
{
File.Delete(filePath);
}
return valid;
}

/// <summary> Retrieve a stored filePath for the user's Lift export. </summary>
/// <returns> Path to the Lift file on disk. </returns>
public string? RetrieveExport(string userId)
{
_liftExports.TryGetValue(userId, out var exportPath);
_liftExports.TryGetValue(userId, out var tuple);
var exportPath = tuple.Item1;
return exportPath == InProgress ? null : exportPath;
}

/// <summary> Delete a stored Lift export path and its file on disk. </summary>
/// <returns> If the element is successfully found and removed, true; otherwise, false. </returns>
/// <summary>
/// Deleting will invalidate any export the user has in progress at the time this method is called.
/// </summary>
/// <returns> False, if the user did not have any export to delete. True, otherwise. </returns>
public bool DeleteExport(string userId)
{
var removeSuccessful = _liftExports.Remove(userId, out var filePath);
var removeSuccessful = _liftExports.TryRemove(userId, out var tuple);
var filePath = tuple.Item1;
if (removeSuccessful && filePath != InProgress && File.Exists(filePath))
{
File.Delete(filePath);
Expand Down
11 changes: 11 additions & 0 deletions Backend/Services/PermissionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ public async Task<bool> IsViolationEdit(HttpContext request, string userEditId,
return user.WorkedProjects[projectId] != userEditId;
}

/// <returns> TraceIdentifier for the request. If null, returns an empty string. </returns>
public string GetExportId(HttpContext request)
{
var exportId = request.TraceIdentifier;
if (exportId is null)
{
return "";
}
return exportId;
}

/// <summary>Retrieve the User ID from the JWT in a request. </summary>
/// <exception cref="InvalidJwtTokenException"> Throws when null userId extracted from token. </exception>
public string GetUserId(HttpContext request)
Expand Down
1 change: 1 addition & 0 deletions public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
"uploadAvatarTitle": "Set user avatar"
},
"projectExport": {
"canceledExport": "Canceling export for project: {{ val }}",
"cannotExportEmpty": "Project is empty. You cannot export a project with no words.",
"downloadInProgress": "Downloading project: {{ val }}",
"exportFailed": "Failed to export project {{ val }}. Click to clear this message.",
Expand Down
Loading
Loading