protected override void OnStart(string[] args) { // Update the service state to Start Pending. ServiceStatus serviceStatus = new ServiceStatus(); serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING; serviceStatus.dwWaitHint = 100000; SetServiceStatus(this.ServiceHandle, ref serviceStatus); eventLog1.WriteEntry("Service Initiating...", EventLogEntryType.Information, eventId++); //OLD TIMER, ONLY EXECUTED ON INTERVAL //Initialize Timer Object to poll when the service should run //Timer timer = new Timer(); //timer.Interval = 120000; // 2 minutes //timer.Elapsed += new ElapsedEventHandler(this.OnTimer); //timer.Start(); // Set initial Interval to the difference in milliseconds from current time and the scheduled time _timer.Enabled = true; _timer.Interval = _scheduleTime.Subtract(DateTime.Now).TotalSeconds * 1000; _timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed); // Initialize APIHandler to route commands through apiHandler = new APIHandler(); apiHandler.SetEventLog(eventLog1); apiHandler.Refresh(ref eventId); // Initialize ExcelData excelData = new ExcelData(eventLog1); excelData.Refresh(apiHandler.cm, ref eventId, eventLog1); eventId++; // Update the service state to Running. serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING; SetServiceStatus(this.ServiceHandle, ref serviceStatus); eventLog1.WriteEntry("Service Started.", EventLogEntryType.Information, eventId++); }
public void ExcelQueueUpdate(ExcelData excelData, ref int eventId, bool wipeData = false) { cm.BeginLog(); cm.LogMessage("Beginning WFM Agent Queue Update Process using the UCCX API."); cm.LogMessage(""); int numFailed = 0; int numAgentsProcessed = 0; foreach (ExcelAgent excelAgent in excelData.excelAgents) { Stopwatch sw = new Stopwatch(); sw.Start(); // Determine Agent URL via apiData.ResourcesData string agentUserId = apiData.ResourcesData.Resource.Where(p => p.FirstName + " " + p.LastName == excelAgent.agentName).First().UserID; string agentUrl = $"{cm.RootURL}/resource/{agentUserId}"; try { // Determine which Resource from APIData.ResourcesData corresponds to the current excelAgent being processed Resource agentInfo = apiData.ResourcesData.Resource.Where(p => p.FirstName + " " + p.LastName == excelAgent.agentName).First(); // Serialize XML related to the current excelAgent being processed using Resource Object XmlDocument xml = SerializeXml(agentInfo); // Isolate Old skillMap Node XmlNode node = xml.SelectSingleNode("/resource/skillMap"); // Determine Agent's desired Queue ExcelSkill newQueue = excelData.excelSkills.Where(p => p.Name == excelAgent.Queue).First(); // Build Skill Map XML to replace current using Agent's desired Queue XmlDocument xmlSkillMap = new XmlDocument(); string skillMapString = BuildSkillMap(newQueue); // Determine Action based on whether or not error occurred in BuildSkillMap() if (skillMapString != "ERROR") { xmlSkillMap.LoadXml(skillMapString); // Create new XmlNode object to replace old skillMap with XmlNode newNode = xmlSkillMap.SelectSingleNode("/skillMap"); // If wipeData == True, remove all skills from Agents if (wipeData == false) { // Replace skillMap Node with new skillMap Node node.InnerXml = newNode.InnerXml; } else { // Remove all skills from Agents node.InnerXml = ""; } // Make PUT Request to update Agent Skill Map try { // Call Method to make PUT Request to API to update Agent skillMap cm.LogMessage($"Attempting to update {excelAgent.agentName} ({agentUserId}) to new Queue: {excelAgent.Queue}\n\tAgent refURL: {agentUrl}"); eventNum = eventId; HttpWebResponse requestResponse = UpdateAgentResource(xml.OuterXml, agentUrl); eventId++; sw.Stop(); if (requestResponse.StatusCode == HttpStatusCode.OK) { eventLog.WriteEntry($"Successfully updated the Agent data with Response Code: {requestResponse.StatusCode}\nDuration: ({sw.ElapsedMilliseconds}ms)"); cm.LogMessage($"Successfully updated the Agent data with Response Code: {requestResponse.StatusCode}\nDuration: ({sw.ElapsedMilliseconds}ms)"); } else { throw new System.ArgumentException($"Error - Status Code Returned: {requestResponse.StatusCode} -- {requestResponse.StatusDescription}"); } } catch (Exception e) { numFailed += 1; eventLog.WriteEntry("Invalid HttpWebResponse.StatusCode while attempting to UPDATE Agent Resource Data (" + agentUserId + ").\n" + e.Message.ToString(), EventLogEntryType.Warning, ++eventId); cm.LogMessage("-->Invalid HttpWebResponse.StatusCode while attempting to UPDATE Agent Resource Data (" + agentUserId + ")"); cm.LogMessage(e.Message.ToString()); } } else { numFailed += 1; } numAgentsProcessed += 1; } catch (Exception e) { eventLog.WriteEntry($"Error Occurred while updating current user {agentUserId}.\nAgent URL Endpoint Used: {agentUrl}\nError Caught: {e.Message.ToString()}\nError Source: {e.Source.ToString()}\nError Stack Trace: {e.StackTrace.ToString()}", EventLogEntryType.Error, eventId++); cm.LogMessage($"Error Occurred -- {e.Message.ToString()}"); cm.LogMessage($"Stack Trace: {e.StackTrace.ToString()}"); cm.LogMessage($"Continuing to the next agent..."); } cm.LogMessage(""); } cm.EndLog(); cm.BeginLog(); EndProcessLog(excelData.excelAgents.Count, numFailed); cm.EndLog(); eventLog.WriteEntry("UCCX API Agent Queue Update has finished.", EventLogEntryType.Information, eventId++); }