예제 #1
0
        /// <summary>
        /// 定时刷新灾备状态,主要根据灾备目录下对应文件的内容
        /// </summary>
        private void SwitchRefresher()
        {
            _switchRefresher = new Timer(x =>
            {
                try
                {
                    string filePath = Path.Combine(_failoverDir, UtilAndComs.FAILOVER_SWITCH);
                    if (!DiskCache.IsFileExists(filePath))
                    {
                        _switchParams.AddOrUpdate(FAILOVER_MODE_NAME, "false", (k, v) => "false");
                        _logger.Debug($"failover switch is not found, {filePath}");
                        return;
                    }

                    long modified = DiskCache.GetFileLastModifiedTime(filePath);
                    if (_failoverLastModifiedMillis < modified)
                    {
                        _failoverLastModifiedMillis = modified;
                        string failover             = DiskCache.ReadFile(filePath);

                        if (!string.IsNullOrEmpty(failover))
                        {
                            var lines = failover.Split(new string[] { DiskCache.GetLineSeparator() }, StringSplitOptions.RemoveEmptyEntries);
                            foreach (var line in lines)
                            {
                                if ("1".Equals(line.Trim()))
                                {
                                    _switchParams.AddOrUpdate(FAILOVER_MODE_NAME, "true", (k, v) => "true");
                                    _logger.Info($"{FAILOVER_MODE_NAME} is on");
                                    FailoverFileReader();
                                }
                                else if ("0".Equals(line.Trim()))
                                {
                                    _switchParams.AddOrUpdate(FAILOVER_MODE_NAME, "false", (k, v) => "false");
                                    _logger.Info($"{FAILOVER_MODE_NAME} is off");
                                }
                            }
                        }
                        else
                        {
                            _switchParams.AddOrUpdate(FAILOVER_MODE_NAME, "false", (k, v) => "false");
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, "[NA] failed to read failover switch.");
                }

                _switchRefresher.Change(SWITCH_REFRESHER_PERIOD, Timeout.Infinite);
            }, null, SWITCH_REFRESHER_DUETIME, Timeout.Infinite);
        }