public ActionResult RunAction(string receivedData) { MobileAppServerClient.RequestBody rb = JsonConvert.DeserializeObject <MobileAppServerClient.RequestBody>(receivedData); SubServer targetServer = GetAvailableSubServer(); string cacheResultKey = BuildCacheResultKey(rb); if (targetServer == null) { CheckAllocateNewInstance(); return(ResolveResultOnUreachableServer(cacheResultKey)); } Client client = BuildClient(targetServer); client.SendRequest(rb); MobileAppServerClient.OperationResult result = client.GetResult(); if (EnabledCachedResultsForUnreachableServers) { CacheRepository <MobileAppServerClient.OperationResult> .Set(cacheResultKey, result, 380); } if (targetServer.HasLifetime()) { targetServer.RefreshLifetime(); } return(ActionResult.Json(result)); }
private bool IsServerUnavailable(SubServer server) { string key = $"unavailable-{server.Address}:{server.Port}"; Cache <bool> cached = CacheRepository <bool> .Get(key); if (cached != null) { LogController.WriteLog($"The sub-server node '{server.Address}:{server.Port}' is unreachable. It is temporarily ignored but will be reconsidered in less than 120 seconds", ServerLogType.ALERT); } return(cached != null); }
private int GetCurrentThreadCountOnServer(SubServer server, Client client) { LogController.WriteLog($"Querying availability on '{server.Address}:{server.Port}'", ServerLogType.INFO); MobileAppServerClient.RequestBody rb = MobileAppServerClient .RequestBody.Create("ServerInfoController", "GetCurrentThreadsCount"); client.SendRequest(rb); int result = int.Parse(client.ReadResponse().Content.ToString()); return(result); }
private Client BuildClient(SubServer server) { try { Client client = new Client(server.Address, server.Port, server.Encoding, server.BufferSize, server.MaxConnectionAttempts); return(client); } catch { return(null); } }
public static void AddSubServer(string address, int port, Encoding encoding, int bufferSize, int maxConnectionAttempts, int acceptableProcesses) { if (SubServers == null) { SubServers = new List <SubServer>(); } var server = new SubServer(address, port, encoding, bufferSize, maxConnectionAttempts, acceptableProcesses); AddSubServerInternal(server); }
private static void AddSubServerInternal(SubServer server, bool isDynamicInstance = false) { SubServers.Add(server); if (SubServers.Count > 1) { if (isDynamicInstance) { if (NotifiableSubServerRequirement != null) { server.EnableLifetime(NotifiableSubServerRequirement, ServersLifetimeInMinutes); server.OnLifeTimeEnded += SubServer_OnLifeTimeEnded; } } } LogController.WriteLog($"Added sub-server node named as '{server.Address}:{server.Port}'", ServerLogType.INFO); }
private static void SubServer_OnLifeTimeEnded(SubServer subServer) { LogController.WriteLog($"Sub-server '{subServer.Address}:{subServer.Port}' is no longer needed and will be detached from the pool of sub-servers. A notification was issued for the target sub-server to be shut down", ServerLogType.ALERT); SubServers.Remove(subServer); }