-
Notifications
You must be signed in to change notification settings - Fork 758
/
Copy pathServerController.cs
216 lines (188 loc) · 8.34 KB
/
ServerController.cs
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
208
209
210
211
212
213
214
215
216
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information
namespace DotNetNuke.Entities.Host
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Web.Caching;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Data;
using DotNetNuke.Entities.Controllers;
using DotNetNuke.Framework;
using DotNetNuke.Instrumentation;
using DotNetNuke.Services.Log.EventLog;
using DotNetNuke.UI.WebControls;
public class ServerController
{
public const string DefaultUrlAdapter = "DotNetNuke.Entities.Host.ServerWebRequestAdapter, DotNetNuke";
private const string CacheKey = "WebServers";
private const int CacheTimeout = 20;
private const CacheItemPriority CachePriority = CacheItemPriority.High;
private static readonly DataProvider DataProvider = DataProvider.Instance();
private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(ServerController));
public static bool UseAppName
{
get
{
var uniqueServers = new Dictionary<string, string>();
foreach (ServerInfo server in GetEnabledServers())
{
uniqueServers[server.ServerName] = server.IISAppName;
}
return uniqueServers.Count < GetEnabledServers().Count;
}
}
public static void ClearCachedServers()
{
DataCache.RemoveCache(CacheKey);
}
public static void DeleteServer(int serverID)
{
DataProvider.Instance().DeleteServer(serverID);
ClearCachedServers();
}
public static List<ServerInfo> GetEnabledServers()
{
return GetServers()?.Where(i => i.Enabled).ToList() ?? new List<ServerInfo>();
}
/// <summary>
/// Gets the servers, order by last activity date in descending order.
/// </summary>
/// <param name="lastMinutes">The number of recent minutes activity had to occur</param>
/// <returns>A list of servers with activity within the specified minutes</returns>
public static List<ServerInfo> GetEnabledServersWithActivity(int lastMinutes = 10)
{
return GetServersNoCache()
.Where(i => i.Enabled == true && DateTime.Now.Subtract(i.LastActivityDate).TotalMinutes <= lastMinutes)
.OrderByDescending(i => i.LastActivityDate)
.ToList();
}
/// <summary>
/// Gets the servers, that have no activtiy in the specified time frame.
/// </summary>
/// <param name="lastMinutes">The number of recent minutes activity had to occur</param>
/// <returns>A list of servers with no activity for the specified minutes. Defaults to 24 hours</returns>
public static List<ServerInfo> GetInActiveServers(int lastMinutes = 1440)
{
return GetServersNoCache()
.Where(i => DateTime.Now.Subtract(i.LastActivityDate).TotalMinutes > lastMinutes && i.ServerName != Environment.MachineName)
.OrderByDescending(i => i.LastActivityDate)
.ToList();
}
public static string GetExecutingServerName()
{
string executingServerName = Globals.ServerName;
if (UseAppName)
{
executingServerName += "-" + Globals.IISAppName;
}
Logger.Debug("GetExecutingServerName:" + executingServerName);
return executingServerName;
}
public static string GetServerName(ServerInfo webServer)
{
string serverName = webServer.ServerName;
if (UseAppName)
{
serverName += "-" + webServer.IISAppName;
}
Logger.Debug("GetServerName:" + serverName);
return serverName;
}
public static List<ServerInfo> GetServers()
{
var servers = CBO.GetCachedObject<List<ServerInfo>>(new CacheItemArgs(CacheKey, CacheTimeout, CachePriority), GetServersCallBack);
return servers;
}
public static void UpdateServer(ServerInfo server)
{
DataProvider.Instance().UpdateServer(server.ServerID, server.Url, server.UniqueId, server.Enabled, server.ServerGroup);
ClearCachedServers();
}
public static void UpdateServerActivity(ServerInfo server)
{
var allServers = GetServers();
var existServer = allServers.FirstOrDefault(s => s.ServerName == server.ServerName && s.IISAppName == server.IISAppName);
var serverId = DataProvider.Instance().UpdateServerActivity(server.ServerName, server.IISAppName, server.CreatedDate, server.LastActivityDate, server.PingFailureCount, server.Enabled);
server.ServerID = serverId;
if (existServer == null
|| string.IsNullOrEmpty(existServer.Url)
|| (string.IsNullOrEmpty(existServer.UniqueId) && !string.IsNullOrEmpty(GetServerUniqueId())))
{
// try to detect the server url from url adapter.
server.Url = existServer == null || string.IsNullOrEmpty(existServer.Url) ? GetServerUrl() : existServer.Url;
// try to detect the server unique id from url adapter.
server.UniqueId = existServer == null || string.IsNullOrEmpty(existServer.UniqueId) ? GetServerUniqueId() : existServer.UniqueId;
UpdateServer(server);
ClearCachedServers(); // Only clear the cache if we added a server
}
else
{
// Just update the existing item in the cache
existServer.LastActivityDate = server.LastActivityDate;
existServer.Enabled = server.Enabled;
DataCache.SetCache(ServerController.CacheKey, allServers);
}
// log the server info
var log = new LogInfo();
log.AddProperty(existServer != null ? "Server Updated" : "Add New Server", server.ServerName);
log.AddProperty("IISAppName", server.IISAppName);
log.AddProperty("Last Activity Date", server.LastActivityDate.ToString());
log.LogTypeKey = existServer != null ? EventLogController.EventLogType.WEBSERVER_UPDATED.ToString()
: EventLogController.EventLogType.WEBSERVER_CREATED.ToString();
LogController.Instance.AddLog(log);
}
public static IServerWebRequestAdapter GetServerWebRequestAdapter()
{
var adapterConfig = HostController.Instance.GetString("WebServer_ServerRequestAdapter", DefaultUrlAdapter);
var adapterType = Reflection.CreateType(adapterConfig);
return Reflection.CreateInstance(adapterType) as IServerWebRequestAdapter;
}
private static List<ServerInfo> GetServersNoCache()
{
return CBO.FillCollection<ServerInfo>(DataProvider.GetServers());
}
private static object GetServersCallBack(CacheItemArgs cacheItemArgs)
{
return CBO.FillCollection<ServerInfo>(DataProvider.GetServers());
}
private static string GetServerUrl()
{
try
{
var adpapter = GetServerWebRequestAdapter();
if (adpapter == null)
{
return string.Empty;
}
return adpapter.GetServerUrl();
}
catch (Exception ex)
{
Logger.Error(ex);
return string.Empty;
}
}
private static string GetServerUniqueId()
{
try
{
var adpapter = GetServerWebRequestAdapter();
if (adpapter == null)
{
return string.Empty;
}
return adpapter.GetServerUniqueId();
}
catch (Exception ex)
{
Logger.Error(ex);
return string.Empty;
}
}
}
}