public static void Run(bool console = false) { BackgroundPool.config = new SettingsConfiguration(); oUsToMonitor = config.OUsToMonitor; oUsDNToMonitor = config.OUsDNToMonitor; try { InitializeLogging(console); PerformStartupChecks(config); hb = new HeartbeatMonitor(); hb.configuration = config; hb.Start(); threadBackgroundWorker.IsBackground = true; if (threadBackgroundWorker.ThreadState.HasFlag(System.Threading.ThreadState.Unstarted)) { threadBackgroundWorker.Start(); } } catch (Exception e) { if (e.InnerException != null) { Console.Error.WriteLine(e.InnerException.Message); log.LogFatal(e.InnerException.Message); } Console.Error.WriteLine(e.Message); log.LogFatal(e.Message); throw; } }
private static void BackgroundPoll() { bool logVerbose = config.LogVerbose; //var accountToMonitor = config.SamAccountsToMonitor; //if (accountToMonitor.Count > 0) // log.LogDebug("Monitoring only " + accountToMonitor.Count + " accounts: " + string.Join(";", accountToMonitor)); if (oUsToMonitor.Count > 0) { log.LogDebug("Monitoring only " + oUsToMonitor.Count + " OU(s): " + string.Join(";", oUsToMonitor)); } if (oUsDNToMonitor.Count > 0) { log.LogDebug("Monitoring only " + oUsDNToMonitor.Count + " OU(s) DNs: " + string.Join(";", oUsDNToMonitor)); } // get attributes for replicate: var allAtributes = ADHintsConfigurationSection.GetAllAttributeNames(); PollAD.AddSourcePropNames(allAtributes); PollAD.AddDestinationPropNames(allAtributes); PollAD.Log = log; CCMApi.log = log; CCMApi.interCcmSpacing = config.interCcmSpacing; #if DEBUG uniTest(); #endif InitializeAllAccounts(); while (true) { try { if (!canWork) { break; } if (File.Exists(Path.Combine(HomeFolder, config.CCMCPRFile))) { cprContent = SettingsConfiguration.LoadCPRFile(Path.Combine(HomeFolder, config.CCMCPRFile)); } else { cprContent = SettingsConfiguration.CPRFileContent; } PollAD ad = GetAvailableAD(config.SourceADServers, lastHighUSNs, false); bool success = false; if (ad == null) { log.LogError("Unable to connect to any AD Servers"); } else { if (ad.ChangedUsersProperties.Count == 0) { success = true; // no changes } else { int cnt = ad.ChangedUsersProperties.Count; //log.LogInfo("Found update for " + cnt + " user(s) in Source AD. Current USN = " + ad.CurrentHighUSN + ". InvocationID=" + ad.GetInvocationID); FilterAccounts(ad.ChangedUsersProperties, config.DestADServers.Select(s => s.ServerUserName), oUsToMonitor, oUsDNToMonitor); if (cnt - ad.ChangedUsersProperties.Count > 0) { log.LogInfo("Filtered out " + (cnt - ad.ChangedUsersProperties.Count) + " accounts"); } if (ad.ChangedUsersProperties.Count == 0) { success = true; // no changes } else if (0 != PutToDestinationAD(config.DestADServers, ad.ChangedUsersProperties, false)) { success = true; // saved to Dest AD } // if at least 1 user is succeeded then assume that whole iteration is succeeded // If all users are failed than seems to be CCM or AD is down and iteration should run again. } // if iteration is secceeded and highUSN is changed than save it to DB if (success && (!lastHighUSNs.ContainsKey(ad.GetInvocationID) || lastHighUSNs[ad.GetInvocationID] != ad.CurrentHighUSN)) { lastHighUSNs[ad.GetInvocationID] = ad.CurrentHighUSN; // SaveCurrentHighUSN(ad.GetInvocationID, ad.CurrentHighUSN); } } if (initializationFails.Count > 0) { log.LogInfo("Process " + initializationFails.Count + " accounts which failed on startup ..."); PutToDestinationAD(config.DestADServers, initializationFails.Values.ToList(), false); } } catch (Exception ex) { log.LogError(ex, "BackgroundPoll"); } finally { stopEvent.WaitOne(config.PoolsInterval * 1000); // each 2 sec } } }