public Reaper(IBackend backend, ILog logger, List <Models.Setting> settings, NotificationDelay notificationDelay) { _backend = backend; _logger = logger; _notificationDelay = notificationDelay; this.settings = settings; }
/// <summary> /// Determine if the virtual machine has previously been notified /// The last_notified value is used against the current time to check that it has not /// had notifications within the duration in settings /// </summary> /// <returns>bool</returns> public bool NotifiedWithinTimePeriod() { bool result = false; // Use a notification object to determine if the group already exists in the table // for this time period // NotificationDelay previousNotification = (NotificationDelay) notificationDelay.Get( // new string[] { subscriptionId, virtualMachine.ResourceGroupName, "virtualMachine", virtualMachine.Name }, // new string[] { "subscription", "group_name", "type", "name" } // ); // Create a dictionary of fields and values for the query Dictionary <string, dynamic> criteria = new Dictionary <string, dynamic>(); criteria.Add("subscription_id", subscriptionId); criteria.Add("group_name", resourceGroup.GetName()); criteria.Add("type", "virtual_machine"); criteria.Add("name", virtualMachine.Name); Response response = notificationDelay.Get(criteria); NotificationDelay previousNotification = response.GetData(); if (previousNotification != null) { int delay = Convert.ToInt32(settings.First(s => s.name == "notify_delay").value); result = Utilities.DelayExpired(previousNotification.last_notified, delay, timeNowUtc); // if (result) // { // logger.LogDebug("{0} - {1}: last notified {2} seconds ago", virtualMachine.ResourceGroupName, virtualMachine.Name, elapsed.ToString()); // } } return(result); }
/// <summary> /// Determine if the resource group has previously been notified /// The last_notified value is used against the current time to check that it has not /// had notifications within the duration in settings /// </summary> /// <returns></returns> private bool NotifiedWithinTimePeriod() { bool result = false; // Use a notification object to determine if the group already exists in the table // for this time period // NotificationDelay previousNotification = (NotificationDelay) notificationDelay.Get( // new string[] { subscriptionId, resourceGroup.Name, "resourceGroup" }, // new string[] { "subscription", "group_name", "type" } // ); // Create a dictionary of fields and values for the query Dictionary <string, dynamic> criteria = new Dictionary <string, dynamic>(); criteria.Add("subscription_id", subscriptionId); criteria.Add("group_name", resourceGroup.Name); criteria.Add("type", "resourceGroup"); NotificationDelay nd = new NotificationDelay(_backend, _logger); Response response = nd.Get(criteria); dynamic data = response.GetData(); if (data.Count == 1) { // determine how many seconds have elapsed since the last notification //int elapsed = (DateTime.UtcNow - previousNotification.last_notified).Seconds; // get the notification delay //int notifyDelay = Convert.ToInt32(settings.First(s => s.name == "notify_delay").value); //result = elapsed > notifyDelay; NotificationDelay previousNotification = (NotificationDelay)data.First(); int delay = Convert.ToInt32(settings.First(s => s.name == "notify_delay").value); result = Utilities.DelayExpired(previousNotification.last_notified, delay, timeNowUtc); // if (result) // { // _logger.Debug("Last notified {elapsed} seconds ago: {group}", elapsed.ToString(), resourceGroup.Name); // } } return(result); }
public Group( IResourceGroup resourceGroup, IBackend backend, ILog logger, List <Setting> settings, NotificationDelay notificationDelay, string subscriptionId, IEnumerable <LocationTZ> timezones, DateTime timeNowUtc ) { this.resourceGroup = resourceGroup; this._backend = backend; this._logger = logger; this.settings = settings; this.notificationDelay = notificationDelay; this.subscriptionId = subscriptionId; this.timezones = timezones; this.timeNowUtc = timeNowUtc; }
/// <summary> /// runReaper executes the Reaper for the timer process /// It is responsible for gathering the settings and the current subscription /// that are required by the reaper. This is so that the reaper does not need to /// access the DB directly /// </summary> private async Task runReaper() { Response response = new Response(); List <Models.Setting> settings = new List <Models.Setting>(); List <Models.Subscription> subscriptions = new List <Models.Subscription>(); List <Models.LocationTZ> locationTZs = new List <Models.LocationTZ>(); // get the necessary settings by category string[] categories = new string[] { "tags", "slack", "lifecycle" }; // get the settings the reaper will use Dictionary <string, dynamic> criteria = new Dictionary <string, dynamic>(); criteria.Add("category", categories); IModel setting = new Setting(_backend, _logger); response = setting.Get(criteria); settings = response.GetData(); // get the subscriptions known to the system IModel subscription = new Subscription(_backend, _logger); response = subscription.GetByName(); subscriptions = response.GetData(); // get the location timezones known to the system LocationTZ locationTZ = new LocationTZ(_backend, _logger); // Create a notification delay object for the reaper to work with NotificationDelay notificationDelay = new NotificationDelay(_backend, _logger); // Create an instance of the reaper and process it Reaper reaper = new Reaper(_backend, _logger, settings, notificationDelay); reaper.Process(subscriptions, locationTZ); }
public VirtualMachine( string subscriptionId, IVirtualMachine virtualMachine, ILog logger, IEnumerable <Setting> settings, NotificationDelay notificationDelay, Group resourceGroup, IEnumerable <LocationTZ> timezones, DateTime timeNowUtc ) { this.subscriptionId = subscriptionId; this.virtualMachine = virtualMachine; this.logger = logger; this.settings = settings; this.notificationDelay = notificationDelay; this.resourceGroup = resourceGroup; this.timezones = timezones; // timezone.GetByName().GetData(); this.timeNowUtc = timeNowUtc; // Using the settings, set the start and stop time for the vm startTime = settings.First(s => s.name == "vm_start").value; stopTime = settings.First(s => s.name == "vm_stop").value; }