private void ProcessRequests(CheckerEntities db) { // if we have free processes if (availableProcesses > 0) { // getting not processed tasks var newTasks = db.Tasks.Where(t => !t.IsProcessed); if (newTasks != null && newTasks.Count() > 0) { // take tasks according to free processes newTasks = newTasks.Take(availableProcesses); CmdRequest cmdRequest = new CmdRequest(); cmdRequest.cmd = CmdTypeEnum.check.ToString(); cmdRequest.servers = new List<CmdServer>(); foreach (var t in newTasks) { availableProcesses--; t.IsProcessed = true; CmdServer srv = new CmdServer { id = t.TaskId, ip = t.Server.Trim(), port = t.Port, login = t.Login.Trim(), pass = t.Password, info = t.Info }; cmdRequest.servers.Add(srv); } db.SaveChanges(); // notifying about new commands OnNewCmd(cmdRequest); } } }
private void ProcessReplies(CheckerEntities db) { lock (replies) { if (replies.Count > 0) { foreach (CmdServerReply reply in replies) { // notify thread server to free process OnCmdReplyProcessed(reply); // increasing availabel process counter availableProcesses++; var task = db.Tasks.FirstOrDefault(t => t.TaskId == reply.id); if (task != null) { task.IsCompleted = true; TaskResult result = new TaskResult { TaskId = task.TaskId, Status = reply.status, Info = string.IsNullOrEmpty(reply.info) ? string.Empty : reply.info, ErrorCode = reply.code.HasValue ? reply.code.Value : 0, ErrorMessage = reply.fail }; db.TaskResults.AddObject(result); } else { Logger.AddInformation(string.Format("Failed to receive task from database with id = {0}", reply.id)); } } db.SaveChanges(); // updating processed reply count OnCmdProcessed(replies.Count); // cleaning replies replies.Clear(); } } }