コード例 #1
0
        private static bool Run(AppSettings appSettings, IInitializationService initialization, Master.INodeSecurityService nodeSecurityService)
        {
            bool allSuccessful = appSettings.Nodes.Count > 0;

            if (allSuccessful)
            {
                var nodes = new List <Node>();
                foreach (var n in appSettings.Nodes)
                {
                    var node = initialization.GetNode(n.Id);
                    if (node == null)
                    {
                        allSuccessful = false;
                        Console.WriteLine($"伺服器未登錄 NodeId:{n.Id} 資訊");
                    }
                    else
                    {
                        nodes.Add(node);

                        if (node.Version == n.Version)
                        {
                            Console.WriteLine($"NodeId:{n.Id} 不須更新");
                        }
                        else
                        {
                            allSuccessful = node.CopyTo(appSettings.SymmetricServerPath) && node.Write(appSettings.SymmetricServerPath);

                            if (string.IsNullOrEmpty(node.RegistrationUrl) && allSuccessful)
                            {
                                int check = 0;

                                initialization.CreateTables(appSettings.SymmetricServerPath, node);
                                do
                                {
                                    check        += 1;
                                    allSuccessful = initialization.CheckTables();
                                    Thread.Sleep(1000);
                                } while (!allSuccessful && check < 3);
                                if (!allSuccessful)
                                {
                                    throw new Exception($"NodeId:{n.Id} 資料表處理失敗,有可能是資料庫 pg_hba.conf 設定錯誤");
                                }

                                allSuccessful = initialization.NodeGroups(node) && initialization.SynchronizationMethod(node) &&
                                                initialization.Node(node) && initialization.Channel() && initialization.Triggers() &&
                                                initialization.Router() && initialization.Relationship();

                                if (allSuccessful)
                                {
                                    check = 0;
                                    var nodeIds = node.MasterNode.Register(appSettings.SymmetricServerPath, node);
                                    do
                                    {
                                        check        += 1;
                                        allSuccessful = nodeSecurityService.CheckRegister(nodeIds);
                                        Thread.Sleep(1000);
                                    } while (!allSuccessful && check < 3);
                                    if (!allSuccessful)
                                    {
                                        throw new Exception($"NodeId:{n.Id} 註冊 client node 失敗");
                                    }
                                }
                                else
                                {
                                    Console.WriteLine($"NodeId:{n.Id} 初始化失敗");
                                }
                            }

                            if (allSuccessful)
                            {
                                n.Version = node.Version;
                            }
                            else
                            {
                                Console.WriteLine($"NodeId:{n.Id} 設定配置失敗");
                            }
                        }
                    }
                }

                if (allSuccessful)
                {
                    string contents = JsonConvert.SerializeObject(appSettings);
                    string path     = Directory.GetCurrentDirectory();
                    path = Path.GetFullPath(path + "\\appsettings.json");

                    try
                    {
                        File.WriteAllText(path, contents);
                    }
                    catch
                    {
                        allSuccessful = false;
                    }

                    if (allSuccessful)
                    {
                        Parallel.ForEach(nodes, n => n.RunOnlyOnce(appSettings.SymmetricServerPath, initialization));
                    }
                }
            }

            return(allSuccessful);
        }