public static Boolean UpdateServiceCollection(VSNext.Mongo.Entities.Enums.ServerType ServerType, String NodeName) { try { VSNext.Mongo.Repository.Repository <VSNext.Mongo.Entities.Nodes> repository = new VSNext.Mongo.Repository.Repository <VSNext.Mongo.Entities.Nodes>(connectionString); FilterDefinition <VSNext.Mongo.Entities.Nodes> filterDef = repository.Filter.Eq(i => i.Name, NodeName); if (repository.Find(filterDef).ToList()[0].CollectionResets.Where(x => x.DeviceType == ServerType.ToString() && x.Reset.HasValue && x.Reset.Value).Count() > 0) { FilterDefinitionBuilder <VSNext.Mongo.Entities.CollectionReset> filterDefBuilder = new FilterDefinitionBuilder <VSNext.Mongo.Entities.CollectionReset>(); filterDef = filterDef & repository.Filter.ElemMatch(x => x.CollectionResets, filterDefBuilder.Eq(x => x.DeviceType, ServerType.ToString())); UpdateDefinition <VSNext.Mongo.Entities.Nodes> updateDef = repository.Updater.Set(i => i.CollectionResets.ElementAt(-1).DateCleared, DateTime.Now) .Set(i => i.CollectionResets.ElementAt(-1).Reset, false); try { repository.Update(filterDef, updateDef); } catch (Exception ex) { } return(true); } return(false); } catch (Exception ex) { } return(false); }
public static MonitoredItems.MonitoredDevice SelectServerToMonitor(MonitoredItems.MonitoredDevicesCollection collection) { DateTime tNow = DateTime.Now; MonitoredItems.MonitoredDevice SelectedServer = null; VSFramework.RegistryHandler myRegistry = new VSFramework.RegistryHandler(); if (collection.Count == 0) { return(null); } //Look for ScanNow's try { //LogUtils.WriteDeviceHistoryEntry("All", "SelectServer", tNow.ToString() + " >>> Looking for " + collection.get_Item(0).ServerType, LogUtils.LogLevel.Verbose); } catch (Exception ex) { //LogUtils.WriteDeviceHistoryEntry("All", "SelectServer", tNow.ToString() + " >>> Exception getting server type. Exception: " + ex.Message.ToString(), LogUtils.LogLevel.Verbose); } try { if (String.IsNullOrWhiteSpace(connectionString)) { connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["VitalSignsMongo"].ToString(); } VSNext.Mongo.Repository.Repository <VSNext.Mongo.Entities.Server> serverRepository = new VSNext.Mongo.Repository.Repository <VSNext.Mongo.Entities.Server>(connectionString); VSNext.Mongo.Repository.Repository <VSNext.Mongo.Entities.ServerOther> serverOtherRepository = new VSNext.Mongo.Repository.Repository <VSNext.Mongo.Entities.ServerOther>(connectionString); List <VSNext.Mongo.Entities.Server> list = new List <Server>(); if (Enums.Utility.getEnumFromDescription <Enums.ServerType>(collection.get_Item(0).ServerType).getServerOther()) { FilterDefinition <VSNext.Mongo.Entities.ServerOther> filterDef = serverOtherRepository.Filter.Eq(x => x.ScanNow, true) & serverOtherRepository.Filter.Eq(x => x.Type, collection.get_Item(0).ServerType); list = serverOtherRepository.Find(filterDef).ToList().Select(x => new Server() { Id = x.Id, DeviceType = x.Type }).ToList(); } else { FilterDefinition <VSNext.Mongo.Entities.Server> filterDef = serverRepository.Filter.Eq(x => x.ScanNow, true) & serverRepository.Filter.Eq(x => x.DeviceType, collection.get_Item(0).ServerType); list = serverRepository.Find(filterDef).ToList(); } if (list.Count > 0) { for (int i = 0; i < list.Count(); i++) { SelectedServer = collection.FindByObjectId(list[i].Id); if (SelectedServer != null) { break; } } if (SelectedServer != null) { if (Enums.Utility.getEnumFromDescription <Enums.ServerType>(collection.get_Item(0).ServerType).getServerOther()) { FilterDefinition <VSNext.Mongo.Entities.ServerOther> filterDef = serverOtherRepository.Filter.Eq(x => x.Id, SelectedServer.ServerObjectID); UpdateDefinition <VSNext.Mongo.Entities.ServerOther> updateDef = serverOtherRepository.Updater.Set(x => x.ScanNow, false); serverOtherRepository.Update(filterDef, updateDef); } else { FilterDefinition <VSNext.Mongo.Entities.Server> filterDef = serverRepository.Filter.Eq(x => x.Id, SelectedServer.ServerObjectID); UpdateDefinition <VSNext.Mongo.Entities.Server> updateDef = serverRepository.Updater.Set(x => x.ScanNow, false); serverRepository.Update(filterDef, updateDef); } LogUtils.WriteDeviceHistoryEntry("All", "SelectServer", tNow.ToString() + " >>> Selecting " + SelectedServer.Name + " because the status is " + SelectedServer.Status, LogUtils.LogLevel.Verbose); return(SelectedServer); } } } catch (Exception ex) { LogUtils.WriteDeviceHistoryEntry("All", "SelectServer", tNow.ToString() + " Exception finding a ScanNow server. Exception: " + ex.Message.ToString()); } //Scans not scanned servers try { foreach (MonitoredItems.MonitoredDevice server in collection) { if (server.Status == "Not Scanned" && server.Enabled == true && server.IsBeingScanned == false) { LogUtils.WriteDeviceHistoryEntry("All", "SelectServer", tNow.ToString() + " >>> Selecting " + server.Name + " because the status is " + server.Status, LogUtils.LogLevel.Verbose); return(server); } } } catch (Exception ex) { LogUtils.WriteDeviceHistoryEntry("All", "SelectServer", tNow.ToString() + " Exception finding not scanned servers. Exception: " + ex.Message.ToString()); } //Scans not responding server try { foreach (MonitoredItems.MonitoredDevice server in collection) { if (server.Status == "Not Responding" && server.Enabled == true && server.IsBeingScanned == false) { if (DateTime.Compare(tNow, server.NextScan) > 0) { LogUtils.WriteDeviceHistoryEntry("All", "SelectServer", tNow.ToString() + " >>> Selecting " + server.Name + " because status is " + server.Status + ". Next scheduled scan is " + server.NextScan.ToShortTimeString()); return(server); } } } } catch (Exception ex) { LogUtils.WriteDeviceHistoryEntry("All", "SelectServer", tNow.ToString() + " Exception finding a not responding server. Exception: " + ex.Message.ToString()); } try { MonitoredItems.MonitoredDevicesCollection ScanCanidates = new MonitoredItems.MonitoredDevicesCollection(); foreach (MonitoredItems.MonitoredDevice server in collection) { if (server.IsBeingScanned == false && server.Enabled == true) { tNow = DateTime.Now; if (DateTime.Compare(tNow, server.NextScan) > 0) { ScanCanidates.Add(server); } } } if (ScanCanidates.Count == 0) { Thread.Sleep(10000); return(null); } LogUtils.WriteDeviceHistoryEntry("All", "SelectServer", tNow.ToString() + " >>> Scan Canidates are: " + String.Join(",", ScanCanidates.Cast <MonitoredItems.MonitoredDevice>().Select(x => x.Name))); //Returns the server that is the most overdue tNow = DateTime.Now; var returnServer = ScanCanidates.Cast <MonitoredItems.MonitoredDevice>().OrderBy(x => x.NextScan).ToList()[0]; LogUtils.WriteDeviceHistoryEntry("All", "SelectServer", tNow.ToString() + " >>> Selecting " + returnServer.Name + " since it is next due to scan at " + returnServer.NextScan.ToString()); return(returnServer); } catch (Exception ex) { LogUtils.WriteDeviceHistoryEntry("All", "SelectServer", tNow.ToString() + " Exception finding a server that is due to be scanned. Exception: " + ex.Message.ToString()); } Thread.Sleep(10000); return(null); }