/// <summary> /// Actions on timer event /// </summary> /// <param name="source"></param> /// <param name="e"></param> private static void OnTimerEvent(object source, ElapsedEventArgs e) { try { List <RunningInstance> instances = ServersWatcher.Instances; // Write running instances to hosts file HostsManager hostsMgr = new HostsManager(); hostsMgr.OpenHostsFile(); foreach (RunningInstance inst in instances) { hostsMgr.AddRecord(inst.PrivateIpAddress, ServersWatcher.GetInstanceName(inst)); } hostsMgr.CommitHostsFile(); //Apply ElasticIP for current host: EC2Helper ec2Helper = EC2Helper.Make(); RunningInstance instance = ec2Helper.GetCurrentInstance(instances); if (instance == null) { return; } if (ec2Helper.IsElasticIpAssigned(instance)) { // ElasticIP assigned to instance, update tags Tag tag = new Tag().WithKey(EC2Helper.TAG_ELASTIC_IP).WithValue(instance.IpAddress); ec2Helper.AddTagToInstance(instance.InstanceId, tag); } else { var tag = ec2Helper.GetElasticIpTag(instance); // Delete tag if exists if (tag != null) { ec2Helper.DeleteTag(instance.InstanceId, tag); } } } catch (Exception ex) { Logger.Log(ex.Message, EventLogEntryType.Error); throw new Exception(ex.Message, ex); } }
/// <summary> /// On service stop actions /// </summary> protected override void OnStop() { try { if (_timer != null) { _timer.Stop(); _timer.Dispose(); } HostsManager hostsMgr = new HostsManager(); hostsMgr.ClearHostsFile(); Logger.Log("Exapark Server Discoverer Service was stopped", EventLogEntryType.Information); } catch (Exception ex) { Logger.Log(ex.Message, EventLogEntryType.Error); } }