public void AddProcessQueue(ICommonRedisClient redisClient, string queueName) { string lockKey = string.Format("{0}-{1}-LockKey", CONST_WAIT_PROCESS_QUEUES, queueName); using (var dl = redisClient.AcquireLock(lockKey, new TimeSpan(0, 0, 0, 10))) { if (dl != null) { if (!redisClient.SortedSetContainsItem(CONST_WAIT_PROCESS_QUEUES, queueName)) { redisClient.AddItemToSortedSet(CONST_WAIT_PROCESS_QUEUES, queueName, DateTime.Now.Ticks); } } } redisClient.SetEntryInHash(CONST_LAST_PROCESS_TIME_QUEUES, queueName, DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat)); }
//处理任务 public void StartToQueueProcess(ICommonRedisClient redisClient) { var queueName = redisClient.PopItemWithLowestScoreFromSortedSet(CONST_WAIT_PROCESS_QUEUES); bool needProcess = false; if (!string.IsNullOrWhiteSpace(queueName)) { string lockKey = $"{CONST_IN_PROCESSING_QUEUES}-{queueName}-lockKey"; using (var dl = redisClient.AcquireLock(lockKey, new TimeSpan(0, 0, 0, 10))) { if (dl != null) { DateTime inProcessTime = DateTime.MinValue; var strInprocessTime = redisClient.GetValueFromHash(CONST_IN_PROCESSING_QUEUES, queueName); if (!string.IsNullOrWhiteSpace(strInprocessTime)) { DateTime.TryParse(strInprocessTime, out inProcessTime); } if (inProcessTime == DateTime.MinValue) { redisClient.SetEntryInHash(CONST_IN_PROCESSING_QUEUES, queueName, DateTime.Now.ToString(BaseSystemInfo.DateFormat)); needProcess = true; } else { if (inProcessTime.AddMinutes(5) < DateTime.Now) { redisClient.RemoveEntryFromHash(CONST_IN_PROCESSING_QUEUES, queueName); } } } else { LogUtilities.WriteLine("获取锁失败"); } } } if (needProcess) { if (!string.IsNullOrEmpty(queueName)) { LogUtilities.WriteLine($"处理的任务队列是{queueName}"); redisClient.Remove(queueName); } } }