private void MainWindow_Loaded(object sender, RoutedEventArgs e) { List <ServiceBaseInfo> serviceList = ServiceLocator.Instance.Value.GetServices(); ServiceBaseInfo watchDogServiceInfo = null; ObservableCollection <ServiceVM> serviceVMCollection = new ObservableCollection <ServiceVM>(); serviceList?.ForEach(s => { if (WatchDogServiceVM.WATCHDOG_SERVICE_NAME.Equals(s.Config?.ServiceName)) { watchDogServiceInfo = s; } else { serviceVMCollection.Add(new ServiceVM() { ServiceInfo = s }); } }); if (watchDogServiceInfo != null) { WatchDogServiceVM watchDogServiceVM = new WatchDogServiceVM() { ServiceInfo = watchDogServiceInfo }; serviceVMCollection.Add(watchDogServiceVM); watchDogServiceVM.SetServiceVMList(serviceVMCollection); } icServiceContainer.ItemsSource = serviceVMCollection; }
public void SetServiceVMList(IEnumerable <ServiceVM> serviceVMList) { _serviceList = new List <ServiceVM>(); if (serviceVMList != null) { foreach (var srvVM in serviceVMList) { if (srvVM.ServiceInfo != null) { if (WATCHDOG_SERVICE_NAME.Equals(srvVM.ServiceInfo.Config?.ServiceName)) { selfInfo = srvVM.ServiceInfo; string serviceBaseDir = selfInfo?.BaseDirectory; if (!string.IsNullOrEmpty(serviceBaseDir)) { string watchDogExeFullPath = Path.Combine(serviceBaseDir, selfInfo?.Config?.ExcutableFilePath); if (File.Exists(watchDogExeFullPath)) { selfFullExcutableFilePath = watchDogExeFullPath; } } } else { _serviceList.Add(srvVM); } } } } }
private bool CheckServiceInfoValid(ServiceBaseInfo baseInfo) { if (baseInfo != null) { if (!string.IsNullOrWhiteSpace(baseInfo.BaseDirectory)) { if (baseInfo.Config != null) { if (!string.IsNullOrWhiteSpace(baseInfo.Config.ExcutableFilePath)) { if (!string.IsNullOrWhiteSpace(baseInfo.Config.ServiceName)) { return(true); } } } } } return(false); }
/// <summary> /// 将新的配置写入到配置文件中 /// </summary> private void SaveConfig() { string serviceBaseDir = selfInfo?.BaseDirectory; if (!string.IsNullOrWhiteSpace(serviceBaseDir)) { string configFilePath = Path.Combine(serviceBaseDir, @"Config.json"); string configJsonStr = string.Empty; ConfigEnitty config = new ConfigEnitty(); config.ServiceList = new List <ServiceWatchInfo>(); if (_serviceList != null) { foreach (var srvVM in _serviceList) { if (!srvVM.Installed) { continue; } ServiceBaseInfo srvInfo = srvVM.ServiceInfo; if (!CheckServiceInfoValid(srvInfo)) { continue; } config.ServiceList.Add(new ServiceWatchInfo() { AutoRecovery = true, FullExePath = Path.Combine(srvInfo.BaseDirectory, srvInfo.Config?.ExcutableFilePath), ServiceName = srvInfo.Config?.ServiceName }); } configJsonStr = JsonConvert.SerializeObject(config, Formatting.Indented); } File.WriteAllText(configFilePath, configJsonStr); } }
/// <summary> /// 注册服务 /// </summary> /// <param name="info"></param> /// <param name="serviceRegisterType"></param> public static void RegisterService(ServiceBaseInfo info, ServiceRegisterTypeEnum serviceRegisterType) { using (var bus = new ZooKeeperBus()) { var node = info.ServiceName.Replace("/", "."); var services = bus.GetData <List <ServiceBaseInfo> >(node) ?? new List <ServiceBaseInfo>(); var item = services.FirstOrDefault(p => p.Ip.Equals(info.Ip)); if (item != null) { services.Remove(item); } switch (serviceRegisterType) { case ServiceRegisterTypeEnum.Add: case ServiceRegisterTypeEnum.Update: services.Add(info); break; case ServiceRegisterTypeEnum.Remove: break; } bus.SetDataWithLock(node, services); } }