Skip to content
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

Address race condition where: (see comment) #4559

Merged
merged 3 commits into from
Dec 30, 2024
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
5 changes: 5 additions & 0 deletions source/backend/api/Services/DocumentQueueService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ public async Task<PimsDocumentQueue> PollForDocument(PimsDocumentQueue documentQ
this.Logger.LogError("Unable to find document queue with {id}", documentQueue.DocumentQueueId);
throw new KeyNotFoundException($"Unable to find document queue with matching id: {documentQueue.DocumentQueueId}");
}
else if (databaseDocumentQueue.DocumentQueueStatusTypeCode != DocumentQueueStatusTypes.PROCESSING.ToString())
{
this.Logger.LogError("Document Queue {documentQueueId} is not in valid state, aborting poll.", documentQueue.DocumentQueueId);
return databaseDocumentQueue;
}

var relatedDocument = _documentRepository.TryGet(documentQueue.DocumentId.Value);

Expand Down
7 changes: 5 additions & 2 deletions source/backend/dal/Repositories/DocumentQueueRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ public PimsDocumentQueue GetByDocumentId(long documentId)
public PimsDocumentQueue Update(PimsDocumentQueue queuedDocument, bool removeDocument = false)
{
queuedDocument.ThrowIfNull(nameof(queuedDocument));
var existingQueuedDocument = TryGetById(queuedDocument.DocumentQueueId);

var existingQueuedDocument = TryGetById(queuedDocument.DocumentQueueId) ?? throw new KeyNotFoundException($"DocumentQueueId {queuedDocument.DocumentQueueId} not found.");
if (existingQueuedDocument?.DocumentQueueStatusTypeCode == DocumentQueueStatusTypes.SUCCESS.ToString() && queuedDocument.DocumentQueueStatusTypeCode != DocumentQueueStatusTypes.SUCCESS.ToString())
{
throw new InvalidOperationException($"DocumentQueueId {queuedDocument.DocumentQueueId} is already completed.");
}
if (!removeDocument)
{
queuedDocument.Document = existingQueuedDocument.Document;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public async Task PollForDocument_RelatedDocumentMayanIdNull_UpdatesStatusToPIMS
var service = CreateDocumentQueueServiceWithPermissions(Permissions.SystemAdmin);
var documentQueue = new PimsDocumentQueue { DocumentQueueId = 1, DocumentId = 1 };
var relatedDocument = new PimsDocument { MayanId = null };
var databaseDocumentQueue = new PimsDocumentQueue { DocumentQueueId = 1 };
var databaseDocumentQueue = new PimsDocumentQueue { DocumentQueueId = 1, DocumentQueueStatusTypeCode = DocumentQueueStatusTypes.PROCESSING.ToString() };
var documentRepositoryMock = this._helper.GetService<Mock<IDocumentRepository>>();
var documentQueueRepositoryMock = this._helper.GetService<Mock<IDocumentQueueRepository>>();
var documentServiceMock = this._helper.GetService<Mock<IDocumentService>>();
Expand All @@ -230,7 +230,7 @@ public async Task PollForDocument_GetStorageDocumentDetailFails_UpdatesStatusToP
var service = CreateDocumentQueueServiceWithPermissions(Permissions.SystemAdmin);
var documentQueue = new PimsDocumentQueue { DocumentQueueId = 1, DocumentId = 1 };
var relatedDocument = new PimsDocument { MayanId = 1 };
var databaseDocumentQueue = new PimsDocumentQueue { DocumentQueueId = 1 };
var databaseDocumentQueue = new PimsDocumentQueue { DocumentQueueId = 1, DocumentQueueStatusTypeCode = DocumentQueueStatusTypes.PROCESSING.ToString() };
var documentDetailsResponse = new ExternalResponse<DocumentDetailModel> { Status = ExternalResponseStatus.Error };
var documentRepositoryMock = this._helper.GetService<Mock<IDocumentRepository>>();
var documentQueueRepositoryMock = this._helper.GetService<Mock<IDocumentQueueRepository>>();
Expand Down Expand Up @@ -283,7 +283,7 @@ public async Task PollForDocument_FileLatestIdNotNull_UpdatesStatusToSuccess()
var service = CreateDocumentQueueServiceWithPermissions(Permissions.SystemAdmin);
var documentQueue = new PimsDocumentQueue { DocumentQueueId = 1, DocumentId = 1 };
var relatedDocument = new PimsDocument { MayanId = 1 };
var databaseDocumentQueue = new PimsDocumentQueue { DocumentQueueId = 1 };
var databaseDocumentQueue = new PimsDocumentQueue { DocumentQueueId = 1, DocumentQueueStatusTypeCode = DocumentQueueStatusTypes.PROCESSING.ToString() };
var documentDetailModel = new DocumentDetailModel { FileLatest = new FileLatestModel { Id = 1 } };
var documentDetailsResponse = new ExternalResponse<DocumentDetailModel> { Status = ExternalResponseStatus.Success, Payload = documentDetailModel };
var documentRepositoryMock = this._helper.GetService<Mock<IDocumentRepository>>();
Expand Down
Loading