-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLoadBalance.cs
More file actions
207 lines (184 loc) · 5.05 KB
/
LoadBalance.cs
File metadata and controls
207 lines (184 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using Microsoft.WindowsAzure.ServiceRuntime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Configuration;
using Telerik.Sitefinity.LoadBalancing.Configuration;
using Telerik.Sitefinity.Scheduling;
using Telerik.Sitefinity.Services;
namespace Aptera.Azure.LoadBalancing
{
public class LoadBalance
{
#region Properties
/// <summary>
/// Gets Azure IPs
/// </summary>
private static IEnumerable<string> SitefinityInternalEndpointIPs
{
get
{
var ips = new List<string>();
try
{
var currentRole = RoleEnvironment.Roles["SitefinityWebApp"];
if (currentRole != null)
{
foreach (var instance in currentRole.Instances)
{
string ip = GetInternalIP(instance);
if (!ip.IsNullOrEmpty())
{
ips.Add(ip);
}
}
}
}
catch
{
//used to surpress exception so it can be ran outside Azure
}
return ips;
}
}
/// <summary>
/// Gets/Sets IPs from Sitefinity Config
/// </summary>
private static IEnumerable<string> ConfiguredUrls
{
get
{
try
{
var lbConfig = Config.Get<SystemConfig>()
.LoadBalancingConfig;
if (lbConfig != null && lbConfig.URLS != null)
{
return lbConfig.URLS
.Select<InstanceUrlConfigElement, String>(entry => entry.Value)
.ToList();
}
else
return new List<String>();
}
catch (Exception ex)
{
ex.ToString();
return new List<String>();
}
}
set
{
try
{
ConfigManager manager = Config.GetManager();
SystemConfig section = manager.GetSection<SystemConfig>();
if (section.LoadBalancingConfig == null)
{
section.LoadBalancingConfig = new LoadBalancingConfig(section);
}
var instanceUrlList = new ConfigElementList<InstanceUrlConfigElement>(section.LoadBalancingConfig);
if (value != null)
{
foreach (var url in value.ToList())
{
var elem = new InstanceUrlConfigElement(instanceUrlList);
elem.Value = url;
instanceUrlList.Add(elem);
}
}
section.LoadBalancingConfig.URLS = instanceUrlList;
manager.Provider.SuppressSecurityChecks = true;
manager.SaveSection(section);
manager.Provider.SuppressSecurityChecks = false;
}
catch
{ // suppress exceptions
}
}
}
#endregion
#region Methods
/// <summary>
/// Get Azure RoleInstance IP
/// </summary>
/// <param name="ri"></param>
/// <returns></returns>
private static string GetInternalIP(RoleInstance ri)
{
var internalEndpoint = ri.InstanceEndpoints["SitefinityInternalEndpoint"];
if (internalEndpoint != null)
{
return internalEndpoint.IPEndpoint.Address.ToString();
}
else
{
return String.Empty;
}
}
/// <summary>
/// Runs the load balace config editor and schedules a task.
/// </summary>
public static void InitializeLoadBalancingMaintenance()
{
MaintainLoadBalancingUrls();
CreateMaintainLoadBalancingUrlsTask();
}
/// <summary>
/// Maintian URLs based on the number of Azure instances
/// </summary>
/// <param name="removeThisInstance"></param>
public static void MaintainLoadBalancingUrls(bool removeThisInstance = false)
{
var liveUrls = SitefinityInternalEndpointIPs.Select(ip => "http://" + ip + "/");
var configuredUrls = ConfiguredUrls;
if (removeThisInstance)
{
liveUrls = liveUrls.Except(new string[] { "http://" + GetInternalIP(RoleEnvironment.CurrentRoleInstance) + "/" });
}
if (liveUrls.Except(configuredUrls).Count() > 0 // there are running role instances that are not in the configs
||
configuredUrls.Except(liveUrls).Count() > 0 // there are configed role instances that are no longer running
||
liveUrls.Count() == 1 // there is only one running role instance
)
{ // update the load balancing configs
if (liveUrls.Count() > 1)
{
ConfiguredUrls = liveUrls;
}
else
{
ConfiguredUrls = new List<String>();
}
}
}
internal static void CreateMaintainLoadBalancingUrlsTask()
{
SchedulingManager manager = SchedulingManager.GetManager();
var key = LoadBalanceTask.GuidKey;
var existingTasks = manager.GetTaskData().Where(i => i.Key == key).ToList();
if (existingTasks.Count() > 0)
{
foreach (var task in existingTasks)
{
manager.DeleteTaskData(task);
}
manager.SaveChanges();
}
existingTasks = manager.GetTaskData().Where(i => i.Key == key).ToList();
if (existingTasks.Count() == 0)
{
var newTask = new LoadBalanceTask();
//Execution time must be expressed in UTC
newTask.ExecuteTime = DateTime.UtcNow.AddMinutes(1);
newTask.Key = LoadBalanceTask.GuidKey;
manager.AddTask(newTask);
manager.SaveChanges();
}
}
#endregion
}
}