private async Task <SendReceiveJob> UpdateProgress(SendReceiveJob job, int step, int stepCount) { double percentCompleted = (double)step / stepCount; return(await _jobRepo.UpdateAsync(job, u => u .Set(j => j.PercentCompleted, percentCompleted))); }
public void CancelJob(SendReceiveJob job) { if (job.BackgroundJobId == null) { return; } BackgroundJob.Delete(job.BackgroundJobId); }
public async Task <IActionResult> CreateAsync([FromBody] string projectId) { SendReceiveJob job = await _jobRepo.UpdateAsync(DbNames.Default, j => j.ProjectRef == projectId, b => b.SetOnInsert(j => j.ProjectRef, projectId), true); _sendReceiveService.StartJob(job); SendReceiveJobDto dto = CreateDto(job); return(Created(dto.Href, dto)); }
private SendReceiveJobDto CreateDto(SendReceiveJob job) { return(new SendReceiveJobDto() { Id = job.Id.ToString(), Href = Url.RouteUrl(RouteNames.SendReceiveJobs) + "/" + job.Id, Project = new ResourceDto { Id = job.ProjectRef } }); }
public void StartJob(SendReceiveJob job) { if (job.BackgroundJobId != null) { return; } string id = job.Id; BackgroundJob.Enqueue <SendReceiveRunner>(r => r.RunAsync(null, null, id)); }
public async Task <bool> DeleteJobAsync(SendReceiveJob job) { if (await _jobRepo.DeleteAsync(job)) { if (job.State == SendReceiveJob.PendingState || job.State == SendReceiveJob.SyncingState) { BackgroundJob.Delete(job.BackgroundJobId); } return(true); } return(false); }
public async Task RunAsync(PerformContext context, IJobCancellationToken cancellationToken, string id) { SendReceiveJob job = await _jobRepo.UpdateAsync(DbNames.Default, j => j.Id == id && j.BackgroundJobId == null, b => b.Set(j => j.BackgroundJobId, context.BackgroundJob.Id)); if (job == null) { return; } // TODO: perform send/receive await Task.Delay(5000); await _jobRepo.DeleteAsync(DbNames.Default, job); }
public async Task <IActionResult> GetAsync(string id) { SendReceiveJob job = await _jobRepo.GetAsync(DbNames.Default, id); if (job != null) { switch (await AuthorizeAsync(job.ProjectRef, new Right(Domain.Projects, Operation.View))) { case AuthorizeResult.Success: return(Ok(CreateDto(job))); case AuthorizeResult.Forbidden: return(Forbid()); } } return(NotFound()); }
public async Task <Attempt <SendReceiveJob> > TryCreateJobAsync(string userId, string projectId) { SendReceiveJob job = await _jobRepo.UpdateAsync(j => j.ProjectRef == projectId && (j.State == SendReceiveJob.PendingState || j.State == SendReceiveJob.SyncingState), u => u .SetOnInsert(j => j.ProjectRef, projectId) .SetOnInsert(j => j.State, SendReceiveJob.PendingState) .Inc(j => j.StartCount, 1), true); if (job.StartCount == 1) { // new job, so enqueue the runner string jobId = job.Id; BackgroundJob.Enqueue <ParatextSendReceiveRunner>(r => r.RunAsync(null, null, userId, jobId)); return(Attempt.Success(job)); } return(Attempt.Failure(job)); }
public async Task <IActionResult> DeleteAsync(string id) { SendReceiveJob job = await _jobRepo.GetAsync(DbNames.Default, id); if (job != null) { switch (await AuthorizeAsync(job.ProjectRef, new Right(Domain.Projects, Operation.Edit))) { case AuthorizeResult.Success: if (await _jobRepo.DeleteAsync(DbNames.Default, job)) { _sendReceiveService.CancelJob(job); return(Ok()); } break; case AuthorizeResult.Forbidden: return(Forbid()); } } return(NotFound()); }
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)); } }