Esempio n. 1
0
        internal async Task InitWorkerNodesAsync(string serviceName)
        {
            var workerIdSortedSetCacheKey = string.Format(SharedCachingConsts.WorkerIdSortedSetCacheKey, serviceName);

            if (!_redisProvider.KeyExists(workerIdSortedSetCacheKey))
            {
                _logger.LogInformation("Starting InitWorkerNodes:{0}", workerIdSortedSetCacheKey);

                var flag = await _distributedLocker.LockAsync(workerIdSortedSetCacheKey);

                if (!flag.Success)
                {
                    await Task.Delay(300);
                    await InitWorkerNodesAsync(serviceName);
                }

                long count = 0;
                try
                {
                    var set = new Dictionary <long, double>();
                    for (long index = 0; index <= YitterSnowFlake.MaxWorkerId; index++)
                    {
                        set.Add(index, DateTime.Now.GetTotalMilliseconds());
                    }
                    count = await _redisProvider.ZAddAsync(workerIdSortedSetCacheKey, set);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message, ex);
                }
                finally
                {
                    await _distributedLocker.SafedUnLockAsync(workerIdSortedSetCacheKey, flag.LockValue);
                }

                _logger.LogInformation("Finlished InitWorkerNodes:{0}:{1}", workerIdSortedSetCacheKey, count);
            }
            else
            {
                _logger.LogInformation("Exists WorkerNodes:{0}", workerIdSortedSetCacheKey);
            }
        }