Пример #1
0
        private void InitializeStrongTimer()
        {
            int minInterval = 30;

            if (Interval < minInterval && Interval != 0)
            {
                ConsoleHelper.WriteError($"指定的时间周期必须大于或等于 {minInterval} 秒,用户指定的值:{Interval}");
                Environment.Exit(-1);
            }

            var intervalSec = (int)TimeSpan.FromSeconds(Interval).TotalMilliseconds;

            _strongTimer = new StrongTimer
            {
                Period     = intervalSec == 0 ? 5000 : intervalSec,
                RunOnStart = true
            };

            _strongTimer.Elapsed += (sender, args) => { AsyncContext.Run(async() => { await UpdateRecord(); }); };

            _strongTimer.Start();
            ConsoleHelper.WriteMessage("程序已经开始运行...");

            if (Interval == 0)
            {
                _strongTimer.Stop();
                ConsoleHelper.WriteMessage("程序执行完成...");
                Environment.Exit(0);
            }
        }
Пример #2
0
        private void InitializeStrongTimer()
        {
            if (Interval < 60 && Interval != 0)
            {
                ConsoleHelper.WriteError($"指定的时间周期必须大于或等于 60 秒,用户指定的值:{Interval}");
                Environment.Exit(-1);
            }

            var intervalSec = (int)TimeSpan.FromSeconds(Interval).TotalMilliseconds;

            _strongTimer = new StrongTimer
            {
                Period     = intervalSec == 0 ? 5000 : intervalSec,
                RunOnStart = true
            };

            _strongTimer.Elapsed += (sender, args) =>
            {
                AsyncContext.Run(async() =>
                {
                    var records = (await _apiRequestTool.GetRecordsWithMainDomainAsync(ConfigurationHelper.Configuration.MainDomain))
                                  .SelectTokens($"$.DomainRecords.Record[*]")
                                  .Select(x => new AliCloudRecordModel
                    {
                        RecordId = x.SelectToken("$.RecordId")?.Value <string>(),
                        SubName  = x.SelectToken("$.RR")?.Value <string>(),
                        Value    = x.SelectToken("$.Value")?.Value <string>()
                    })
                                  .ToList();

                    var currentPubicIp = (await NetworkHelper.GetPublicNetworkIp()).Replace("\n", "");

                    foreach (var subDomain in ConfigurationHelper.Configuration.SubDomains)
                    {
                        var record = records.FirstOrDefault(x => x.SubName == subDomain.SubDomain);
                        if (record == null)
                        {
                            continue;
                        }
                        if (record.Value == currentPubicIp)
                        {
                            continue;
                        }

                        // 更新指定的子域名 IP。
                        var result = (await _apiRequestTool.UpdateRecordAsync(record.RecordId, currentPubicIp, subDomain)).SelectToken("$.RecordId").Value <string>();
                        if (result == null || result != record.RecordId)
                        {
                            ConsoleHelper.WriteError($"记录 {record.SubName} 更新失败...");
                        }
                    }
                });
            };

            _strongTimer.Start();
            ConsoleHelper.WriteMessage("程序已经开始运行...");

            if (Interval == 0)
            {
                _strongTimer.Stop();
                ConsoleHelper.WriteMessage("程序执行完成...");
                Environment.Exit(0);
            }
        }