public IHttpActionResult Process(RequestForProcessing request) { request.Id = Guid.NewGuid(); var httpClient = new HttpClient(); var routeTable = _routeTableStorage.Load(); var freeHosts = routeTable.Routes .Where(r => r.Host != ":-1" && r.CurrentTasks != null && (!r.CurrentTasks.Any() || r.CurrentTasks.All(x => x.PartId == -1))) .ToList(); if (!freeHosts.Any()) { freeHosts = routeTable.Routes .OrderBy(r => r.CurrentTasks.Count(x => x.PartId != -1)) .ToList(); } foreach (var route in freeHosts) { httpClient.BaseAddress = new Uri($"http://{route.Host}"); var result = httpClient.GetAsync("/values/ping").Result; if (result.IsSuccessStatusCode) { if (route.CurrentTasks == null) { route.CurrentTasks = new List <GameTask>(); } route.CurrentTasks.Add(new GameTask() { TaskId = request.Id, PartId = 0 }); route.LastConnection = DateTime.Now; _routeTableStorage.Save(routeTable); var content = new StringContent( JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"); httpClient.PostAsync("/values/process", content); return(Ok(new { TaskId = request.Id, route.Host })); } routeTable.Routes.Remove(route); _routeTableStorage.Save(routeTable); } return(BadRequest("There are not free hosts.")); }
/// <summary> /// Load your modules or register your services here! /// </summary> /// <param name="kernel">The kernel.</param> private static void RegisterServices(IKernel kernel) { RouteTableStorage routeTableStorage = new RouteTableStorage(); kernel.Bind <RouteTableStorage>().ToConstant(routeTableStorage); var routeTable = routeTableStorage.Load(); kernel.Bind <RouteTable>().ToConstant(routeTable); kernel.Bind <IAuthProvider>().ToConstant(new AuthProvider()); }