|
12 | 12 | using System.Web.Hosting; |
13 | 13 | using System.Management; |
14 | 14 | #endif |
| 15 | + |
15 | 16 | namespace StackifyLib.Models |
16 | 17 | { |
17 | 18 | public class EnvironmentDetail |
@@ -109,17 +110,42 @@ private void GetAzureInfo() |
109 | 110 | /// Get the EC2 Instance name if it exists else null |
110 | 111 | /// </summary> |
111 | 112 | #if NET451 || NET45 || NET40 |
| 113 | + |
| 114 | + public static string GetDeviceName() |
| 115 | + { |
| 116 | + var deviceName = Environment.MachineName; |
| 117 | + var isDefaultDeviceNameEc2 = IsEc2MachineName(deviceName); |
| 118 | + |
| 119 | + if (Config.IsEc2 == null || Config.IsEc2 == true || isDefaultDeviceNameEc2) |
| 120 | + { |
| 121 | + var ec2InstanceId = GetEC2InstanceId(); |
| 122 | + if (string.IsNullOrWhiteSpace(ec2InstanceId) == false) |
| 123 | + { |
| 124 | + deviceName = ec2InstanceId; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return deviceName; |
| 129 | + } |
| 130 | + |
112 | 131 | public static string GetEC2InstanceId() |
113 | 132 | { |
114 | 133 | string r = null; |
115 | 134 |
|
116 | 135 | // SF-6804: Frequent Calls to GetEC2InstanceId |
117 | 136 | bool skipEc2InstanceIdUpdate = false; |
118 | | - var threshold = TimeSpan.FromMinutes(Config.Ec2InstanceMetadataUpdateThresholdMinutes); |
119 | | - lock (ec2InstanceLock) |
| 137 | + if (Config.Ec2InstanceMetadataUpdateThresholdMinutes > 0) |
120 | 138 | { |
121 | | - skipEc2InstanceIdUpdate = ec2InstanceIdLastUpdate != null && ec2InstanceIdLastUpdate < DateTimeOffset.UtcNow.Subtract(threshold); |
122 | | - r = string.IsNullOrWhiteSpace(ec2InstanceId) ? null : ec2InstanceId; |
| 139 | + var threshold = TimeSpan.FromMinutes(Config.Ec2InstanceMetadataUpdateThresholdMinutes); |
| 140 | + lock (ec2InstanceLock) |
| 141 | + { |
| 142 | + skipEc2InstanceIdUpdate = ec2InstanceIdLastUpdate != null && ec2InstanceIdLastUpdate < DateTimeOffset.UtcNow.Subtract(threshold); |
| 143 | + r = string.IsNullOrWhiteSpace(ec2InstanceId) ? null : ec2InstanceId; |
| 144 | + } |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + skipEc2InstanceIdUpdate = true; |
123 | 149 | } |
124 | 150 |
|
125 | 151 | if (skipEc2InstanceIdUpdate) |
@@ -162,17 +188,43 @@ public static string GetEC2InstanceId() |
162 | 188 | return r; |
163 | 189 | } |
164 | 190 | #else |
| 191 | + public static string GetDeviceName() |
| 192 | + { |
| 193 | + var deviceName = Process.GetCurrentProcess().MachineName; |
| 194 | + var isDefaultDeviceNameEc2 = IsEc2MachineName(deviceName); |
| 195 | + |
| 196 | + if (Config.IsEc2 == null || Config.IsEc2 == true || isDefaultDeviceNameEc2) |
| 197 | + { |
| 198 | + var instanceID_task = GetEC2InstanceId(); |
| 199 | + instanceID_task.Wait(); |
| 200 | + if (string.IsNullOrWhiteSpace(instanceID_task.Result) == false) |
| 201 | + { |
| 202 | + deviceName = instanceID_task.Result; |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + return deviceName; |
| 207 | + } |
| 208 | + |
165 | 209 | public static async Task<string> GetEC2InstanceId() |
166 | 210 | { |
167 | 211 | string r = null; |
168 | 212 |
|
169 | 213 | // SF-6804: Frequent Calls to GetEC2InstanceId |
170 | 214 | bool skipEc2InstanceIdUpdate = false; |
171 | | - var threshold = TimeSpan.FromMinutes(Config.Ec2InstanceMetadataUpdateThresholdMinutes); |
172 | | - lock (ec2InstanceLock) |
| 215 | + |
| 216 | + if (Config.Ec2InstanceMetadataUpdateThresholdMinutes > 0) |
173 | 217 | { |
174 | | - skipEc2InstanceIdUpdate = ec2InstanceIdLastUpdate != null && ec2InstanceIdLastUpdate < DateTimeOffset.UtcNow.Subtract(threshold); |
175 | | - r = string.IsNullOrWhiteSpace(ec2InstanceId) ? null : ec2InstanceId; |
| 218 | + var threshold = TimeSpan.FromMinutes(Config.Ec2InstanceMetadataUpdateThresholdMinutes); |
| 219 | + lock (ec2InstanceLock) |
| 220 | + { |
| 221 | + skipEc2InstanceIdUpdate = ec2InstanceIdLastUpdate != null && ec2InstanceIdLastUpdate < DateTimeOffset.UtcNow.Subtract(threshold); |
| 222 | + r = string.IsNullOrWhiteSpace(ec2InstanceId) ? null : ec2InstanceId; |
| 223 | + } |
| 224 | + } |
| 225 | + else |
| 226 | + { |
| 227 | + skipEc2InstanceIdUpdate = true; |
176 | 228 | } |
177 | 229 |
|
178 | 230 | if (skipEc2InstanceIdUpdate) |
@@ -211,7 +263,23 @@ public static async Task<string> GetEC2InstanceId() |
211 | 263 |
|
212 | 264 | return r; |
213 | 265 | } |
| 266 | + |
214 | 267 | #endif |
| 268 | + private static bool IsEc2MachineName(string machineName) |
| 269 | + { |
| 270 | + if (string.IsNullOrWhiteSpace(machineName)) |
| 271 | + { |
| 272 | + return false; |
| 273 | + } |
| 274 | + |
| 275 | + if (machineName.StartsWith("EC2") && machineName.Contains("-")) |
| 276 | + { |
| 277 | + return true; |
| 278 | + } |
| 279 | + |
| 280 | + return false; |
| 281 | + } |
| 282 | + |
215 | 283 | /// <summary> |
216 | 284 | /// Get the display name of the windows service if it is a windows service |
217 | 285 | /// </summary> |
@@ -348,15 +416,7 @@ public EnvironmentDetail(bool loadDetails) |
348 | 416 | } |
349 | 417 | #endif |
350 | 418 |
|
351 | | - |
352 | | -#if NET451 || NET45 || NET40 |
353 | | - |
354 | | - DeviceName = GetEC2InstanceId() ?? Environment.MachineName; |
355 | | -#else |
356 | | - var instanceID_task = GetEC2InstanceId(); |
357 | | - instanceID_task.Wait(); |
358 | | - DeviceName = instanceID_task.Result ?? Process.GetCurrentProcess().MachineName; |
359 | | -#endif |
| 419 | + DeviceName = GetDeviceName(); |
360 | 420 |
|
361 | 421 | #if NET451 || NET45 || NET40 |
362 | 422 | if (string.IsNullOrEmpty(AppName) && !isWebRequest) |
|
0 commit comments