public override void RemoveHash(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            _transaction.KeyDeleteAsync(RedisStorage.GetRedisKey(key));
        }
        public void RemoveHash(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            _transaction.QueueCommand(x => x.Remove(RedisStorage.GetRedisKey(key)));
        }
Example #3
0
        public HashSet <string> GetAllItemsFromSet(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            var result = Redis.GetAllItemsFromSortedSet(RedisStorage.GetRedisKey(key));

            return(new HashSet <string>(result));
        }
Example #4
0
        public override Dictionary <string, string> GetAllEntriesFromHash(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            var result = Redis.HashGetAll(RedisStorage.GetRedisKey(key)).ToStringDictionary();

            return(result.Count != 0 ? result : null);
        }
Example #5
0
        public override void SetRangeInHash(string key, IEnumerable <KeyValuePair <string, string> > keyValuePairs)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (keyValuePairs == null)
            {
                throw new ArgumentNullException("keyValuePairs");
            }

            Redis.HashSet(RedisStorage.GetRedisKey(key), keyValuePairs.ToHashEntries());
        }
        public void SetRangeInHash(
            string key, IEnumerable <KeyValuePair <string, string> > keyValuePairs)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (keyValuePairs == null)
            {
                throw new ArgumentNullException("keyValuePairs");
            }

            _transaction.QueueCommand(
                x => x.SetRangeInHash(RedisStorage.GetRedisKey(key), keyValuePairs));
        }
Example #7
0
        public override HashSet <string> GetAllItemsFromSet(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            HashSet <string> result = new HashSet <string>();

            foreach (var item in Redis.SortedSetScan(RedisStorage.GetRedisKey(key)))
            {
                result.Add(item.Element);
            }

            return(result);
        }
Example #8
0
        public RedisSubscription([NotNull] RedisStorage storage, [NotNull] ISubscriber subscriber)
        {
            if (storage == null)
            {
                throw new ArgumentNullException(nameof(storage));
            }
            if (subscriber == null)
            {
                throw new ArgumentNullException(nameof(subscriber));
            }

            _storage = storage;
            Channel  = _storage.GetRedisKey("JobFetchChannel");

            _subscriber = subscriber;
            _subscriber.Subscribe(Channel, (channel, value) => _mre.Set());
        }
 public void Requeue()
 {
     if (_storage.UseTransactions)
     {
         var transaction = _redis.CreateTransaction();
         // prevent double entry for requeued job
         transaction.ListRemoveAsync(_storage.GetRedisKey($"queue:{Queue}"), JobId, -1);
         transaction.ListRightPushAsync(_storage.GetRedisKey($"queue:{Queue}"), JobId);
         RemoveFromFetchedList(transaction);
         transaction.Execute();
     }
     else
     {
         _redis.ListRemoveAsync(_storage.GetRedisKey($"queue:{Queue}"), JobId, -1);
         _redis.ListRightPushAsync(_storage.GetRedisKey($"queue:{Queue}"), JobId);
         RemoveFromFetchedList(_redis);
     }
     _requeued = true;
 }
Example #10
0
 public override void AddRangeToSet([NotNull] string key, [NotNull] IList <string> items)
 {
     _transaction.SortedSetAddAsync(_storage.GetRedisKey(key), items.Select(x => new SortedSetEntry(x, 0)).ToArray());
 }
Example #11
0
 public override IDisposable AcquireDistributedLock([NotNull] string resource, TimeSpan timeout)
 {
     return(RedisLock.Acquire(Redis, _storage.GetRedisKey(resource), timeout));
 }
Example #12
0
 public long ScheduledCount()
 {
     return(UseConnection(redis =>
                          redis.SortedSetLength(_storage.GetRedisKey("schedule"))));
 }
Example #13
0
 public override IDisposable AcquireDistributedLock([NotNull] string resource, TimeSpan timeout)
 {
     return(new RedisLock(Redis, _storage.GetRedisKey(resource), _jobStorageIdentity + Thread.CurrentThread.ManagedThreadId, timeout));
 }
        void IServerComponent.Execute(CancellationToken cancellationToken)
        {
            using (var connection = (RedisConnection)_storage.GetConnection())
            {
                var redis = connection.Redis;

                foreach (var key in ProcessedKeys)
                {
                    var redisKey = _storage.GetRedisKey(key);

                    var count = redis.ListLength(redisKey);
                    if (count == 0)
                    {
                        continue;
                    }

                    Logger.InfoFormat("Removing expired records from the '{0}' list...", key);

                    const int batchSize    = 100;
                    var       keysToRemove = new List <string>();

                    for (var last = count - 1; last >= 0; last -= batchSize)
                    {
                        var first = Math.Max(0, last - batchSize + 1);

                        var jobIds = redis.ListRange(redisKey, first, last).ToStringArray();
                        if (jobIds.Length == 0)
                        {
                            continue;
                        }

                        var pipeline = redis.CreateBatch();
                        var tasks    = new Task[jobIds.Length];

                        for (var i = 0; i < jobIds.Length; i++)
                        {
                            tasks[i] = pipeline.KeyExistsAsync(_storage.GetRedisKey($"job:{jobIds[i]}"));
                        }

                        pipeline.Execute();
                        Task.WaitAll(tasks);

                        keysToRemove.AddRange(jobIds.Where((t, i) => !((Task <bool>)tasks[i]).Result));
                    }

                    if (keysToRemove.Count == 0)
                    {
                        continue;
                    }

                    Logger.InfoFormat("Removing {0} expired jobs from '{1}' list...", keysToRemove.Count, key);

                    using (var transaction = connection.CreateWriteTransaction())
                    {
                        foreach (var jobId in keysToRemove)
                        {
                            transaction.RemoveFromList(key, jobId);
                        }

                        transaction.Commit();
                    }
                }
            }

            cancellationToken.WaitHandle.WaitOne(_checkInterval);
        }
Example #15
0
 public override IDisposable AcquireDistributedLock([NotNull] string resource, TimeSpan timeout)
 {
     //return RedisLock.Acquire(Redis, _storage.GetRedisKey(resource), timeout);
     return(RedisClient.Lock(_storage.GetRedisKey(resource), (int)timeout.TotalSeconds));
 }
Example #16
0
 public long ScheduledCount()
 {
     return(UseConnection(redis =>
                          redis.ZCard(_storage.GetRedisKey("schedule"))));
 }
 /// <summary>
 /// 添加列表到 Set
 /// </summary>
 /// <param name="key">键</param>
 /// <param name="items">列表</param>
 public override void AddRangeToSet([NotNull] string key, [NotNull] IList <string> items) => _redisClientPipe.ZAdd(_storage.GetRedisKey(key), items.Select(x => (0M, (object)x)).ToArray());
        /// <summary>
        /// 获取队列列表
        /// </summary>
        public IList <QueueWithTopEnqueuedJobsDto> Queues() =>
        UseConnection(redis =>
        {
            var queues = redis.SMembers(_storage.GetRedisKey("queues"));

            var result = new List <QueueWithTopEnqueuedJobsDto>(queues.Length);

            foreach (var queue in queues)
            {
                string[] firstJobIds = null;
                long length          = 0;
                long fetched         = 0;


                Task[] tasks = new Task[3];
                tasks[0]     = _redisClient.LRangeAsync(
                    _storage.GetRedisKey($"queue:{queue}"), -5, -1)
                               .ContinueWith(x => firstJobIds = x.Result);

                tasks[1] = _redisClient.LLenAsync(_storage.GetRedisKey($"queue:{queue}"))
                           .ContinueWith(x => length = x.Result);

                tasks[2] = _redisClient.LLenAsync(_storage.GetRedisKey($"queue:{queue}:dequeued"))
                           .ContinueWith(x => fetched = x.Result);
                Task.WaitAll(tasks);

                var jobs = GetJobsWithProperties(
                    redis,
                    firstJobIds,
                    new[] { "State" },
                    new[] { "EnqueuedAt", "State" },
                    (job, jobData, state) => new EnqueuedJobDto
                {
                    Job             = job,
                    State           = jobData[0],
                    EnqueuedAt      = JobHelper.DeserializeNullableDateTime(state[0]),
                    InEnqueuedState = jobData[0].Equals(state[1], StringComparison.OrdinalIgnoreCase)
                });

                result.Add(new QueueWithTopEnqueuedJobsDto
                {
                    Name      = queue,
                    FirstJobs = jobs,
                    Length    = length,
                    Fetched   = fetched
                });
            }

            return(result);
        });
 /// <summary>
 /// 获取分布式锁
 /// </summary>
 /// <param name="resource">资源</param>
 /// <param name="timeout">超时时间</param>
 public override IDisposable AcquireDistributedLock([NotNull] string resource, TimeSpan timeout) => RedisClient.Lock(_storage.GetRedisKey(resource), (int)timeout.TotalSeconds);