Ejemplo n.º 1
0
        public async Task ExecuteInLockAsync(string lockKey, Func <object, Task <object> > action, object state)
        {
            var redisLock = await RedLock.AcquireAsync(_redisDatabase, GetRedisKey(lockKey), _timeout, _expiries);

            try
            {
                await action(state);
            }
            finally
            {
                await redisLock.DisposeAsync();
            }
        }
Ejemplo n.º 2
0
        public async Task ExecuteInLockAsync(string lockKey, Action action)
        {
            var redisLock = await RedLock.AcquireAsync(_redisDatabase, GetRedisKey(lockKey), _timeout, _expiries);

            try
            {
                action();
            }
            finally
            {
                await redisLock.DisposeAsync();
            }
        }
Ejemplo n.º 3
0
        private async Task ProcessQueueTaskAsync(BufferBlock <WorkContext> queue)
        {
            while (await queue.OutputAvailableAsync())
            {
                var context = await queue.ReceiveAsync();

                lock (context)
                {
                    context.IsRunning = true;
                    if (context.IsTimeout)
                    {
                        context.TaskCompletionSource.TrySetException(new DistributedLockTimeoutException($"Failed to acquire lock on {context.LockKey} within given timeout ({_timeout})"));
                        continue;
                    }
                }

                var redisLock = default(RedLock);
                try
                {
                    redisLock = await RedLock.AcquireAsync(_redisDatabase, GetRedisKey(context.LockKey), context.ExpirationTime - DateTime.UtcNow, _expiries);

                    await context.Action();
                }
                catch (Exception ex)
                {
                    context.TaskCompletionSource.TrySetException(ex);
                }
                finally
                {
                    context.TaskCompletionSource.TrySetResult(true);

                    if (redisLock != null)
                    {
                        try
                        {
                            await redisLock.DisposeAsync();
                        }
                        catch
                        {
                        }
                    }
                }
            }
            ;
        }