Esempio n. 1
0
        private void startServices()
        {
            running = true;
            startTryCount++;
            if (startTryCount > StartRetryMax)
            {
                running = false;
                EPSEventLog.WriteEntry("Failed to start service threads " + StartRetryMax + " times in a row." + Environment.NewLine +
                                       "No more attempts will be made until the service is restarted.", EventLogEntryType.Error);
                return;
            }
            try
            {
                GetConfigValue(ref RemotingPort, "RemotingPort");
                GetConfigValue(ref MasterMemMaxMB, "MasterMemMaxMB");
                GetConfigValue(ref MaintQuerySleepTime, "MaintQuerySleepTime");
                GetConfigValue(ref MaintThreadMax, "MaintThreadMax");
                GetConfigValue(ref MaintThreadSleepTime, "MaintThreadSleepTime");
                GetConfigValue(ref MaintQueryWaitTime, "MaintQueryWaitTime");
                GetConfigValue(ref MaintTimeout, "MaintTimeout");
                GetConfigValue(ref ShortMaintTimeout, "ShortMaintTimeout");
                GetConfigValue(ref MaintProcessPriority, "MaintProcessPriority");
                GetConfigValue(ref MaintMemMaxMB, "MaintMemMaxMB");
                GetConfigValue(ref TaskQuerySleepTime, "TaskQuerySleepTime");
                GetConfigValue(ref TaskThreadMax, "TaskThreadMax");
                GetConfigValue(ref TaskThreadSleepTime, "TaskThreadSleepTime");
                GetConfigValue(ref TaskQueryWaitTime, "TaskQueryWaitTime");
                GetConfigValue(ref TaskTimeout, "TaskTimeout");
                GetConfigValue(ref ShortTaskTimeout, "ShortTaskTimeout");
                GetConfigValue(ref TaskProcessPriority, "TaskProcessPriority");
                GetConfigValue(ref TaskMemMaxMB, "TaskMemMaxMB");
                GetConfigValue(ref ServiceExitInterval, "ServiceExitInterval");
                GetConfigValue(ref ServiceExitMax, "ServiceExitMax");
                GetConfigValue(ref ServiceProcessPriority, "ServiceProcessPriority");
                GetConfigValue(ref ServiceMemMaxMB, "ServiceMemMaxMB");
                GetConfigValue(ref StopProcessTimeout, "StopProcessTimeout");
                GetConfigValue(ref StartRetryMax, "StartRetryMax");
                GetConfigValueBoolean(ref VerboseLogging, "VerboseLogging");

                // starting monitorMyHealthThread
                monitorMyHealthThread = new Thread(new ThreadStart(monitorMyHealth));
                monitorMyHealthThread.Start();

                // start remoting
                try
                {
                    channel = new TcpChannel(RemotingPort);
                    ChannelServices.RegisterChannel(channel);
                    RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingObject), "EncounterPROService", WellKnownObjectMode.SingleCall);
                }
                catch (Exception exc)
                {
                    EPSEventLog.WriteEntry("Could not start remoting to support the EncounterPRO Agent monitor application.  The port (" +
                                           RemotingPort.ToString() + ") may already be in use.  Services will continue to function properly, but the monitor application will not work." +
                                           "To change the port that EncounterPRO Agent uses to enable remoting, refer to the following JMJ KB article:" + Environment.NewLine +
                                           "https://www.jmjtech.com/kb/Article.aspx?ArticleNumber=10178" + Environment.NewLine + Environment.NewLine +
                                           exc.ToString(), System.Diagnostics.EventLogEntryType.Warning);
                }

                int computerID;

                computerID = GetComputerID();

                // Initiate ServiceThreadManager array
                serviceThreadManager = new ServiceThreadManager(dbConInfo[0], dbConInfo[1], computerID, MaintThreadMax, MaintQuerySleepTime, MaintThreadSleepTime, MaintQueryWaitTime, MaintTimeout, ShortMaintTimeout, MaintProcessPriority, MaintMemMaxMB, VerboseLogging, StopProcessTimeout);
                serviceThreadManager.StartManager();

                EPSEventLog.WriteEntry("Service thread manager started.", EventLogEntryType.Information);
                startTryCount = 0;
            }
            catch (Exception exc)
            {
                EPSEventLog.WriteEntry("Failed to start service threads." + Environment.NewLine + exc.ToString(), EventLogEntryType.Error);
                stopServices();
            }
        }
Esempio n. 2
0
        public static string getServiceInfo()
        {
            if (null == sm)
            {
                throw new Exception("No instance of ServiceManager is running");
            }

            System.Data.SqlClient.SqlConnection con = InternalMethods.GetNewConnection(sm.dbConInfo[0], sm.dbConInfo[1]);
            SqlCommand cmd = null;

            try
            {
                System.IO.MemoryStream   ms = new System.IO.MemoryStream();
                System.Xml.XmlTextWriter w  = new System.Xml.XmlTextWriter(ms, System.Text.Encoding.UTF8);

                w.WriteStartDocument();
                w.WriteStartElement("ServiceInfo");

                if (sm.serviceWrappers != null && sm.serviceWrappers.Length > 0)
                {
                    foreach (ServiceWrapper sw in sm.serviceWrappers)
                    {
                        w.WriteStartElement("Service");
                        try
                        {
                            w.WriteElementString("ID", sw.ServiceID.ToString());
                            cmd = new SqlCommand("SELECT service_name FROM o_server_component WHERE service_id = " + sw.ServiceID.ToString(), con);
                            string serviceName = (string)cmd.ExecuteScalar();
                            w.WriteElementString("Description", serviceName);
                            w.WriteElementString("PID", sw.PID.ToString());
                            w.WriteElementString("RunningTime", Math.Round(sw.AgentProcess.Runtime.TotalHours, 2).ToString() + " hours");
                            w.WriteElementString("Memory", sw.CurrentMemoryMB.ToString() + " MB");
                            w.WriteElementString("MaxMemory", Math.Round(((double)sw.AgentProcess.MaxMemoryUsage) / Math.Pow(2d, 20d), 2).ToString() + " MB");
                        }
                        catch {}
                        w.WriteEndElement();
                    }
                }

                if (sm.serviceThreadManager != null)
                {
                    ServiceThreadManager stm = sm.serviceThreadManager;

                    w.WriteStartElement("ProcessPools");

                    w.WriteStartElement("ProcessPool");
                    w.WriteAttributeString("type", "short");
                    w.WriteElementString("MaxProcesses", stm.sMaxRunningProcesses.ToString());
                    w.WriteStartElement("QueuedProcesses");
                    if (stm.sQueuedProcessArray != null && stm.sQueuedProcessArray.Length > 0)
                    {
                        foreach (ProcessInfo pi in stm.sQueuedProcessArray)
                        {
                            w.WriteStartElement("QueuedProcess");
                            try
                            {
                                w.WriteElementString("Application", pi.Application);
                                w.WriteElementString("Arguments", pi.Arguments);
                            }
                            catch {}
                            w.WriteEndElement();
                        }
                    }
                    w.WriteEndElement();
                    w.WriteStartElement("RunningProcesses");
                    if (stm.sRunningProcessArray != null && stm.sRunningProcessArray.Length > 0)
                    {
                        foreach (AgentProcess ap in stm.sRunningProcessArray)
                        {
                            w.WriteStartElement("RunningProcess");
                            // WPItem
                            try
                            {
                                if (ap.Arguments.IndexOf("WPITEM") > -1)
                                {
                                    w.WriteElementString("Type", "WPItem");
                                    w.WriteElementString("ID", ap.TaskId.ToString());
                                    cmd = new SqlCommand("SELECT description FROM p_patient_wp_item WHERE patient_workplan_item_id = " + ap.TaskId.ToString(), con);
                                    string wpitem = (string)cmd.ExecuteScalar();
                                    w.WriteElementString("Description", wpitem);
                                    w.WriteElementString("User", ap.TaskUser);
                                    w.WriteElementString("PID", ap.ProcessId.ToString());
                                    w.WriteElementString("RunningTime", Math.Round(ap.Runtime.TotalMinutes, 2).ToString() + " minutes");
                                    w.WriteElementString("MaxRunningTime", Math.Round(ap.MaxRuntime.TotalMinutes, 2).ToString() + " minutes");
                                    w.WriteElementString("Memory", Math.Round(((double)ap.MemoryUsage) / Math.Pow(2d, 20d), 2).ToString() + " MB");
                                    w.WriteElementString("MaxMemory", Math.Round(((double)ap.MaxMemoryUsage) / Math.Pow(2d, 20d), 2).ToString() + " MB");
                                    cmd = new SqlCommand("SELECT retries FROM p_patient_wp_item WHERE patient_workplan_item_id = " + ap.TaskId.ToString(), con);
                                    string retries = cmd.ExecuteScalar().ToString();
                                    w.WriteElementString("Retries", retries);
                                }
                                // Scheduled
                                else if (ap.Arguments.IndexOf("SCHEDULE") > -1)
                                {
                                    w.WriteElementString("Type", "Scheduled");
                                    w.WriteElementString("ID", ap.TaskId.ToString());
                                    cmd = new SqlCommand("SELECT service FROM o_service_schedule WHERE service_sequence = " + ap.TaskId.ToString(), con);
                                    string service = (string)cmd.ExecuteScalar();
                                    w.WriteElementString("Description", service);
                                    w.WriteElementString("User", ap.TaskUser);
                                    w.WriteElementString("PID", ap.ProcessId.ToString());
                                    w.WriteElementString("RunningTime", Math.Round(ap.Runtime.TotalMinutes, 2).ToString() + " minutes");
                                    w.WriteElementString("MaxRunningTime", Math.Round(ap.MaxRuntime.TotalMinutes, 2).ToString() + " minutes");
                                    w.WriteElementString("Memory", Math.Round(((double)ap.MemoryUsage) / Math.Pow(2d, 20d), 2).ToString() + " MB");
                                    w.WriteElementString("MaxMemory", Math.Round(((double)ap.MaxMemoryUsage) / Math.Pow(2d, 20d), 2).ToString() + " MB");
                                    w.WriteElementString("Retries", null);
                                }
                            }
                            catch {}
                            w.WriteEndElement();
                        }
                    }
                    w.WriteEndElement();
                    w.WriteEndElement();

                    w.WriteStartElement("ProcessPool");
                    w.WriteAttributeString("type", "long");
                    w.WriteElementString("MaxProcesses", stm.lMaxRunningProcesses.ToString());
                    w.WriteStartElement("QueuedProcesses");
                    if (stm.lQueuedProcessArray != null && stm.lQueuedProcessArray.Length > 0)
                    {
                        foreach (ProcessInfo pi in stm.lQueuedProcessArray)
                        {
                            w.WriteStartElement("QueuedProcess");
                            try
                            {
                                w.WriteElementString("Application", pi.Application);
                                w.WriteElementString("Arguments", pi.Arguments);
                            }
                            catch {}
                            w.WriteEndElement();
                        }
                    }
                    w.WriteEndElement();
                    w.WriteStartElement("RunningProcesses");
                    if (stm.lRunningProcessArray != null && stm.lRunningProcessArray.Length > 0)
                    {
                        foreach (AgentProcess ap in stm.lRunningProcessArray)
                        {
                            w.WriteStartElement("RunningProcess");
                            try
                            {
                                // WPItem
                                if (ap.Arguments.IndexOf("WPITEM") > -1)
                                {
                                    w.WriteElementString("Type", "WPItem");
                                    w.WriteElementString("ID", ap.TaskId.ToString());
                                    cmd = new SqlCommand("SELECT description FROM p_patient_wp_item WHERE patient_workplan_item_id = " + ap.TaskId.ToString(), con);
                                    string wpitem = (string)cmd.ExecuteScalar();
                                    w.WriteElementString("Description", wpitem);
                                    w.WriteElementString("User", ap.TaskUser);
                                    w.WriteElementString("PID", ap.ProcessId.ToString());
                                    w.WriteElementString("RunningTime", Math.Round(ap.Runtime.TotalMinutes, 2).ToString() + " minutes");
                                    w.WriteElementString("MaxRunningTime", Math.Round(ap.MaxRuntime.TotalMinutes, 2).ToString() + " minutes");
                                    w.WriteElementString("Memory", Math.Round(((double)ap.MemoryUsage) / Math.Pow(2d, 20d), 2).ToString() + " MB");
                                    w.WriteElementString("MaxMemory", Math.Round(((double)ap.MaxMemoryUsage) / Math.Pow(2d, 20d), 2).ToString() + " MB");
                                    cmd = new SqlCommand("SELECT retries FROM p_patient_wp_item WHERE patient_workplan_item_id = " + ap.TaskId.ToString(), con);
                                    string retries = cmd.ExecuteScalar().ToString();
                                    w.WriteElementString("Retries", retries);
                                }
                                // Scheduled
                                else if (ap.Arguments.IndexOf("SCHEDULE") > -1)
                                {
                                    w.WriteElementString("Type", "Scheduled");
                                    w.WriteElementString("ID", ap.TaskId.ToString());
                                    cmd = new SqlCommand("SELECT service FROM o_service_schedule WHERE service_sequence = " + ap.TaskId.ToString(), con);
                                    string service = (string)cmd.ExecuteScalar();
                                    w.WriteElementString("Description", service);
                                    w.WriteElementString("User", ap.TaskUser);
                                    w.WriteElementString("PID", ap.ProcessId.ToString());
                                    w.WriteElementString("RunningTime", Math.Round(ap.Runtime.TotalMinutes, 2).ToString() + " minutes");
                                    w.WriteElementString("MaxRunningTime", Math.Round(ap.MaxRuntime.TotalMinutes, 2).ToString() + " minutes");
                                    w.WriteElementString("Memory", Math.Round(((double)ap.MemoryUsage) / Math.Pow(2d, 20d), 2).ToString() + " MB");
                                    w.WriteElementString("MaxMemory", Math.Round(((double)ap.MaxMemoryUsage) / Math.Pow(2d, 20d), 2).ToString() + " MB");
                                    w.WriteElementString("Retries", null);
                                }
                            }
                            catch {}
                            w.WriteEndElement();
                        }
                    }
                    w.WriteEndElement();
                    w.WriteEndElement();
                    w.WriteEndElement();
                }
                w.WriteEndElement();
                w.WriteEndDocument();
                w.Flush();

                ms.Position = 0;

                System.IO.StreamReader sr = new System.IO.StreamReader(ms, System.Text.Encoding.UTF8);
                string results            = sr.ReadToEnd();
                sr.Close();

                return(results);
            }
            catch (Exception exc)
            {
                EPSEventLog.WriteEntry(exc.ToString(), EventLogEntryType.Error);
                return(null);
            }
            finally
            {
                try{ con.Close(); }
                catch {}
            }
        }