public async void MonitorQueue() { do { var chatQueues = _queueDataModel.Get(); if (chatQueues != null && chatQueues.Any()) { var inactiveChats = chatQueues.Values.Where(x => x.CurrentAgent == null); if (inactiveChats.Any()) { var agents = _agentDataModel.Get(); if (agents != null && agents.Any()) { var unservicedChats = inactiveChats.OrderBy(x => x.ChatEntryTime); foreach (var unservicedChat in unservicedChats) { var availableAgents = agents.Values.Where(x => x.ActiveChats.Count() < x.MaxChats && x.IsPaused == false); if (availableAgents.Any()) { var leastActiveAgent = availableAgents.OrderBy(x => x.ServiceCount).FirstOrDefault(); unservicedChat.CurrentAgent = leastActiveAgent.ID; unservicedChat.ExpiryTime = DateTime.UtcNow.AddMinutes(Constants.DefaultChatExpiry); leastActiveAgent.ServiceCount += 1; leastActiveAgent.ActiveChats.Add(unservicedChat.ClientID); _agentDataModel.Update(leastActiveAgent); _queueDataModel.Update(unservicedChat); _chatHub.StartChat(unservicedChat.ClientID.ToString(), leastActiveAgent.ID.ToString()); } } } } } await Task.Delay(10000); } while (true); }
public void StartAgent(string name, bool isRefresh = false) { var cookies = this.Context.RequestCookies; if (cookies.ContainsKey("AdminID")) { var adminID = this.Context.RequestCookies["AdminID"].Value; AgentHelper agentHelper = new AgentHelper(_agentDataModel, _queueDataModel, _chatHub); var agent = agentHelper.RegisterAgent(name, new Guid(Context.ConnectionId), new Guid(adminID)); if (agent != null && isRefresh) { foreach (var activeChat in agent.ActiveChats) { _chatHub.StartChat(activeChat.ToString(), agent.ID.ToString()); } } } }