private async Task SendReceiveBookAsync(User user, Connection conn, TranslateProject project, TranslateDocumentSet docSet, ParatextProject paratextProject, string docType, string bookId) { string projectPath = GetProjectPath(project, paratextProject); if (!Directory.Exists(projectPath)) { Directory.CreateDirectory(projectPath); } Document <Delta> doc = GetShareDBDocument(conn, project, docSet, docType); string fileName = GetBookTextFileName(projectPath, bookId); if (File.Exists(fileName)) { await SyncBookAsync(user, paratextProject, fileName, bookId, doc); } else { await CloneBookAsync(user, paratextProject, fileName, bookId, doc); } }
private Document <Delta> GetShareDBDocument(Connection conn, TranslateProject project, TranslateDocumentSet docSet, string docType) { return(conn.Get <Delta>($"sf_{project.ProjectCode}", $"{docSet.Id}:{docType}")); }
private async Task DeleteBookAsync(Connection conn, TranslateProject project, TranslateDocumentSet docSet, ParatextProject paratextProject, string docType, string bookId) { string projectPath = GetProjectPath(project, paratextProject); File.Delete(GetBookTextFileName(projectPath, bookId)); Document <Delta> doc = GetShareDBDocument(conn, project, docSet, docType); await doc.FetchAsync(); await doc.DeleteAsync(); }
public async Task RunAsync(PerformContext context, IJobCancellationToken cancellationToken, string userId, string jobId) { SendReceiveJob job = await _jobRepo.UpdateAsync(j => j.Id == jobId, u => u .Set(j => j.BackgroundJobId, context.BackgroundJob.Id) .Set(j => j.State, SendReceiveJob.SyncingState)); if (job == null) { return; } try { SendReceiveOptions options = _options.Value; if ((await _userRepo.TryGetAsync(userId)).TryResult(out User user)) { if ((await _projectRepo.TryGetAsync(job.ProjectRef)).TryResult(out TranslateProject project)) { if (!Directory.Exists(options.TranslateDir)) { Directory.CreateDirectory(options.TranslateDir); } IRepository <TranslateDocumentSet> docSetRepo = _docSetRepoFactory.Create(project); using (var conn = new Connection(new Uri(options.ShareDBUrl))) { await conn.ConnectAsync(); ParatextProject sourceParatextProject = project.Config.Source.ParatextProject; IReadOnlyList <string> sourceBooks = await _paratextService.GetBooksAsync(user, sourceParatextProject.Id); ParatextProject targetParatextProject = project.Config.Target.ParatextProject; IReadOnlyList <string> targetBooks = await _paratextService.GetBooksAsync(user, targetParatextProject.Id); var booksToSendReceive = new HashSet <string>(); booksToSendReceive.UnionWith(sourceBooks); booksToSendReceive.IntersectWith(targetBooks); var booksToDelete = new HashSet <string>(); booksToDelete.UnionWith(GetBooksToDelete(project, sourceParatextProject, sourceBooks)); booksToDelete.UnionWith(GetBooksToDelete(project, targetParatextProject, targetBooks)); int step = 0; int stepCount = booksToSendReceive.Count + booksToDelete.Count; foreach (string bookId in booksToSendReceive) { if (!BookNames.TryGetValue(bookId, out string name)) { name = bookId; } TranslateDocumentSet docSet = await docSetRepo.UpdateAsync(ds => ds.BookId == bookId, u => u.SetOnInsert(ds => ds.Name, name) .SetOnInsert(ds => ds.BookId, bookId) .Set(ds => ds.IsDeleted, false), true); await SendReceiveBookAsync(user, conn, project, docSet, sourceParatextProject, "source", bookId); await SendReceiveBookAsync(user, conn, project, docSet, targetParatextProject, "target", bookId); step++; job = await UpdateProgress(job, step, stepCount); } foreach (string bookId in booksToDelete) { TranslateDocumentSet docSet = await docSetRepo.UpdateAsync(ds => ds.BookId == bookId, u => u.Set(ds => ds.IsDeleted, true)); await DeleteBookAsync(conn, project, docSet, sourceParatextProject, "source", bookId); await DeleteBookAsync(conn, project, docSet, targetParatextProject, "target", bookId); step++; job = await UpdateProgress(job, step, stepCount); } await conn.CloseAsync(); } job = await _jobRepo.UpdateAsync(job, u => u .Set(j => j.State, SendReceiveJob.IdleState) .Unset(j => j.BackgroundJobId)); await _projectRepo.UpdateAsync(project, u => u.Set(p => p.LastSyncedDate, job.DateModified)); } } } catch (Exception e) { _logger.LogError(e, "Error occurred while executing Paratext S/R job '{Job}'", job.Id); await _jobRepo.UpdateAsync(job, u => u .Set(j => j.State, SendReceiveJob.HoldState) .Unset(j => j.BackgroundJobId)); } }