예제 #1
0
        public async Task <bool> Handle(T message)
        {
            var lockKey      = $"{message.UniqueKey()}-{typeof(T).Name.ToLower()}-{_handlerName}";
            var lockResponse = await _messageLock.TryAquireLockAsync(lockKey, TimeSpan.FromSeconds(_timeOut)).ConfigureAwait(false);

            if (!lockResponse.DoIHaveExclusiveLock)
            {
                if (lockResponse.IsMessagePermanentlyLocked)
                {
                    return(RemoveTheMessageFromTheQueue);
                }

                return(LeaveItInTheQueue);
            }

            try
            {
                var successfullyHandled = await _inner.Handle(message).ConfigureAwait(false);

                if (successfullyHandled)
                {
                    await _messageLock.TryAquireLockPermanentlyAsync(lockKey).ConfigureAwait(false);
                }
                return(successfullyHandled);
            }
            catch
            {
                await _messageLock.ReleaseLockAsync(lockKey).ConfigureAwait(false);

                throw;
            }
        }
        public async Task <bool> Handle(T message)
        {
            string lockKey = $"{message.UniqueKey()}-{_lockSuffixKeyForHandler}";
            MessageLockResponse lockResponse = await _messageLock.TryAquireLockAsync(lockKey, _timeout).ConfigureAwait(false);

            if (!lockResponse.DoIHaveExclusiveLock)
            {
                if (lockResponse.IsMessagePermanentlyLocked)
                {
                    return(RemoveTheMessageFromTheQueue);
                }

                return(LeaveItInTheQueue);
            }

            try
            {
                bool successfullyHandled = await _inner.Handle(message).ConfigureAwait(false);

                if (successfullyHandled)
                {
                    await _messageLock.TryAquireLockPermanentlyAsync(lockKey).ConfigureAwait(false);
                }

                return(successfullyHandled);
            }
            catch (Exception)
            {
                await _messageLock.ReleaseLockAsync(lockKey).ConfigureAwait(false);

                throw;
            }
        }
예제 #3
0
    protected override async Task <bool> RunInnerAsync(HandleMessageContext context, Func <CancellationToken, Task <bool> > func, CancellationToken stoppingToken)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }
        if (func == null)
        {
            throw new ArgumentNullException(nameof(func));
        }

        string lockKey = $"{context.Message.UniqueKey()}-{_lockSuffixKeyForHandler}";

        MessageLockResponse lockResponse = await _messageLock.TryAcquireLockAsync(lockKey, _timeout).ConfigureAwait(false);

        if (!lockResponse.DoIHaveExclusiveLock)
        {
            if (lockResponse.IsMessagePermanentlyLocked)
            {
                _logger.LogDebug("Failed to acquire lock for message with key {MessageLockKey} as it is permanently locked.", lockKey);
                return(RemoveTheMessageFromTheQueue);
            }

            _logger.LogDebug("Failed to acquire lock for message with key {MessageLockKey}; returning message to queue.", lockKey);
            return(LeaveItInTheQueue);
        }

        try
        {
            _logger.LogDebug("Acquired lock for message with key {MessageLockKey}.", lockKey);

            bool successfullyHandled = await func(stoppingToken).ConfigureAwait(false);

            if (successfullyHandled)
            {
                await _messageLock.TryAcquireLockPermanentlyAsync(lockKey).ConfigureAwait(false);

                _logger.LogDebug("Acquired permanent lock for message with key {MessageLockKey}.", lockKey);
            }

            return(successfullyHandled);
        }
        catch (Exception)
        {
            await _messageLock.ReleaseLockAsync(lockKey).ConfigureAwait(false);

            _logger.LogDebug("Released lock for message with key {MessageLockKey}.", lockKey);
            throw;
        }
    }
        public async Task <bool> Handle(T message)
        {
            string lockKey = $"{message.UniqueKey()}-{_lockSuffixKeyForHandler}";
            MessageLockResponse lockResponse = await _messageLock.TryAquireLockAsync(lockKey, _timeout).ConfigureAwait(false);

            if (!lockResponse.DoIHaveExclusiveLock)
            {
                if (lockResponse.IsMessagePermanentlyLocked)
                {
                    _logger.LogDebug("Failed to acquire lock for message with key {MessageLockKey} as it is permanently locked.", lockKey);
                    return(RemoveTheMessageFromTheQueue);
                }

                _logger.LogDebug("Failed to acquire lock for message with key {MessageLockKey}; returning message to queue.", lockKey);
                return(LeaveItInTheQueue);
            }

            try
            {
                _logger.LogDebug("Acquired lock for message with key {MessageLockKey}.", lockKey);

                bool successfullyHandled = await _inner.Handle(message).ConfigureAwait(false);

                if (successfullyHandled)
                {
                    await _messageLock.TryAquireLockPermanentlyAsync(lockKey).ConfigureAwait(false);

                    _logger.LogDebug("Acquired permanent lock for message with key {MessageLockKey}.", lockKey);
                }

                return(successfullyHandled);
            }
            catch (Exception)
            {
                await _messageLock.ReleaseLockAsync(lockKey).ConfigureAwait(false);

                _logger.LogDebug("Released lock for message with key {MessageLockKey}.", lockKey);
                throw;
            }
        }