Ejemplo n.º 1
0
        public async Task ShouldLogError_IfUpdateTryCountEntryIsUnsuccessful()
        {
            //Arrange
            var service = SetupService();

            _repository.GetAsync <MessageWrapper <int> >(Arg.Any <string>())
            .Returns(Task.FromResult(new Result <MessageWrapper <int> >
            {
                IsSuccessfull = true, Item = new MessageWrapper <int> {
                    Body = 1
                }
            }));
            _repository.UpdateAsync(Arg.Any <string>(), Arg.Any <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"));
        }
Ejemplo n.º 2
0
        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);
        }