public async Task ResetInProcessing(string uowId) { var result = await _repository.GetAsync <UowStatus>(uowId); if (!result.IsSuccessfull) { throw new InvalidOperationException($"Retrieval of uow status information for {uowId} failed with message: {result.ErrorMessage} and code: {result.ErrorCode}"); } var retrialWrapper = new UowStatus(result.Item.CurrentRetrialCount, isInProcessing: false); await _repository.DeleteAsync(uowId); await _repository.CreateAsync(new CreateKVRequest <UowStatus> { Item = retrialWrapper, Key = uowId }); }
public async Task ShouldLogError_IfCreateTryCountEntryIsUnsuccessful() { //Arrange var service = SetupService(); _repository.GetAsync <MessageWrapper <int> >(Arg.Any <string>()) .Returns(Task.FromResult(new Result <MessageWrapper <int> > { IsSuccessfull = true })); _repository.CreateAsync(Arg.Any <CreateKVRequest <MessageWrapper <int> > >()) .Returns(Task.FromResult(new ActionResult { IsSuccessfull = false })); //Act await service.ShouldRequeueAsync("123", 0, -1, 1); //Assert Assert.Equal(1, service.ReceivedCalls().Count(c => c.GetMethodInfo().Name == "LogErrorAsync")); }
public virtual async Task <bool> ShouldRequeueAsync(string uowId, long creationEpoch, int maxAgeInSeconds, int maxNumberOfRetries) { var tryCountKey = $"{TryCountKeyPrefix}{uowId}"; var createdAt = Epoch.AddSeconds(creationEpoch); if (maxAgeInSeconds != -1 && (DateTime.UtcNow - createdAt).TotalSeconds > maxAgeInSeconds) { await LogInfoAsync($"ImplementMeService detected an expired message with UOWId {uowId}", "ShouldRequeue", new { UOWId = uowId, }); return(false); } if (maxNumberOfRetries != -1) { var getResult = await _repository.GetAsync <MessageWrapper <int> >(tryCountKey); if (getResult != null && !getResult.IsSuccessfull) { await LogErrorAsync(getResult.ErrorCode, getResult.ErrorMessage, "GetMessageTryCount"); return(true); } var uowTryCountEntry = getResult?.Item; if (uowTryCountEntry == null) { await LogInfoAsync( $"ImplementMeService did not find tryCount entry for UOWId {uowId}, assuming first try", "ShouldRequeue", new { UOWId = uowId, TryCount = 1, }); uowTryCountEntry = new MessageWrapper <int> { Body = 1, }; var result = await _repository.CreateAsync(new CreateKVRequest <MessageWrapper <int> > { Item = uowTryCountEntry, Key = tryCountKey, ExpireInSeconds = -1, }); if (!result.IsSuccessfull) { await LogErrorAsync(result.ErrorCode, result.ErrorMessage, "CreateMessageTryCountEntry"); } } else { uowTryCountEntry.Body++; await LogInfoAsync( $"ImplementMeService detected try number {uowTryCountEntry.Body} for UOWId {uowId}", "ShouldRequeue", new { UOWId = uowId, TryCount = uowTryCountEntry.Body, }); var result = await _repository.UpdateAsync(tryCountKey, uowTryCountEntry); if (!result.IsSuccessfull) { await LogErrorAsync(result.ErrorCode, result.ErrorMessage, "UpdateMessageTryCountEntry"); } else { await LogInfoAsync( $"ImplementMeService successfully updated message tryCount for UOWId {uowId}", "ShouldRequeue", new { UOWId = uowId, }); } } var shouldRequeue = uowTryCountEntry.Body < maxNumberOfRetries; await LogInfoAsync( $"ImplementMeService detected message tryCount {uowTryCountEntry.Body} for UOWId {uowId} with MaxNumberOfRetries {maxNumberOfRetries}", "ShouldRequeue", new { UOWId = uowId, ShouldRequeue = shouldRequeue, }); return(shouldRequeue); } return(true); }