protected void OnProcessStopping(AgentProcess process)
 {
     if (null != ProcessStopping)
     {
         ProcessStopping(process, new EventArgs());
     }
 }
 protected void OnProcessNotHealthy(AgentProcess process)
 {
     if (null != ProcessNotHealthy)
     {
         ProcessNotHealthy(process, new EventArgs());
     }
 }
 /// <summary>
 /// Disassociates an AgentProcess with the pool.
 /// </summary>
 /// <param name="Process">AgentProcess object to remove from the pool.</param>
 public void RemoveProcess(AgentProcess Process)
 {
     Process.Started  -= new EventHandler(Process_Started);
     Process.Stopped  -= new EventHandler(Process_Stopped);
     Process.Stopping -= new EventHandler(Process_Stopping);
     runningProcesses.Remove(Process);
 }
 private void run()
 {
     while (keepRunning)
     {
         if (RunningProcesses < MaxRunningProcesses && processQueue.Count > 0)
         {
             ProcessInfo  processInfo = (ProcessInfo)processQueue.Dequeue();
             AgentProcess newProcess  = new AgentProcess();
             newProcess.Application    = processInfo.Application;
             newProcess.Arguments      = processInfo.Arguments;
             newProcess.MaxMemoryUsage = MaxPerProcessMemoryUsage;
             newProcess.MaxRuntime     = MaxPerProcessRuntime;
             newProcess.Priority       = ProcessPriority;
             newProcess.VerboseLogging = VerboseLogging;
             newProcess.Started       += new EventHandler(Process_Started);
             newProcess.Stopped       += new EventHandler(Process_Stopped);
             newProcess.Stopping      += new EventHandler(Process_Stopping);
             newProcess.Start();
         }
         for (int i = runningProcesses.Count - 1; i > -1; i--)
         {
             try
             {
                 AgentProcess p = (AgentProcess)runningProcesses[i];
                 if (!p.IsHealthy)
                 {
                     OnProcessNotHealthy(p);
                 }
             }
             catch {}
         }
         System.Threading.Thread.Sleep(1000);
     }
 }
        private void shortPool_ProcessNotHealthy(object sender, EventArgs e)
        {
            AgentProcess p = (AgentProcess)sender;

            // Check whether service timed out
            if (p.Runtime > p.MaxRuntime)
            {             // Service timed out
                // Attempt to add the process to the long pool.
                // Increase timeout
                p.MaxRuntime = longPool.MaxPerProcessRuntime;
                if (!longPool.AddProcess(p))
                {
                    EPSEventLog.WriteEntry("Extended queue rejected process.  Process will be terminated." + Environment.NewLine +
                                           p.ToString(), System.Diagnostics.EventLogEntryType.Warning);
                    p.Stop();                     // Stop the process if the long pool will not accept it.
                }
                else
                {                 // Must call RemoveProcess AFTER any possible process stopping,
                    //because RemoveProcess unregisters events
                    shortPool.RemoveProcess(p);
                    EPSEventLog.WriteEntry("Process was moved to extended queue." + Environment.NewLine +
                                           p.ToString(), System.Diagnostics.EventLogEntryType.Warning);
                }
            }
            else
            {             // Service exceeded memory limit
                EPSEventLog.WriteEntry("Process has exceeded maximum allowed memory.  Process will be terminated." + Environment.NewLine +
                                       p.ToString(), System.Diagnostics.EventLogEntryType.Warning);
                p.Stop();
            }
        }
        private void longPool_ProcessStopping(object sender, EventArgs e)
        {
            AgentProcess p = (AgentProcess)sender;

            if (p.Arguments.StartsWith("WPITEM"))
            {             // only call service error for WPITEM problems
                ServiceError(p.TaskId, p.TaskUser);
            }
        }
 /// <summary>
 /// Add an existing AgentProcess to the pool if there is room.
 /// </summary>
 /// <param name="Process">An instantiated AgentProcess object.</param>
 /// <returns>A bool indicating whether the AgentProcess was added to the pool.</returns>
 public bool AddProcess(AgentProcess Process)
 {
     if (processQueue.Count == 0 && RunningProcesses < MaxRunningProcesses)
     {
         runningProcesses.Add(Process);
         Process.Started  += new EventHandler(Process_Started);
         Process.Stopped  += new EventHandler(Process_Stopped);
         Process.Stopping += new EventHandler(Process_Stopping);
         return(true);
     }
     else
     {
         return(false);
     }
 }
//
//		private long serviceMemMax
//		{
//			get
//			{
//				return (((long)serviceMemMaxMB) * ((long)Math.Pow(2d,20d)));
//			}
//		}

        public ServiceWrapper(int ServiceID, int ExitInterval, int ExitMax, int ServiceProcessPriority, int ServiceMemMaxMB, int VerboseLogging, int StopProcessTimeout)
        {
            string path = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);

            exitInterval = ExitInterval;
            exitMax      = ExitMax;
//			serviceProcessPriority = ServiceProcessPriority;
            this.ServiceID = ServiceID;
//			this.serviceMemMaxMB=ServiceMemMaxMB;
//			verboseLogging = VerboseLogging==1;
            p                    = new AgentProcess();
            p.Application        = System.IO.Path.Combine(path, "EproServer.exe");
            p.Arguments          = "SERVICE=" + this.ServiceID.ToString();
            p.MaxMemoryUsage     = (uint)(ServiceMemMaxMB * Math.Pow(2d, 20d));
            p.StopProcessTimeout = TimeSpan.FromMilliseconds(StopProcessTimeout);
            p.VerboseLogging     = VerboseLogging == 1;
            switch (ServiceProcessPriority)
            {
            case 1:
                p.Priority = System.Diagnostics.ProcessPriorityClass.Idle;
                break;

            case 2:
                p.Priority = System.Diagnostics.ProcessPriorityClass.BelowNormal;
                break;

            case 3:
            default:
                p.Priority = System.Diagnostics.ProcessPriorityClass.Normal;
                break;

            case 4:
                p.Priority = System.Diagnostics.ProcessPriorityClass.AboveNormal;
                break;

            case 5:
                p.Priority = System.Diagnostics.ProcessPriorityClass.High;
                break;
            }
            p.Started  += new EventHandler(p_Started);
            p.Stopped  += new EventHandler(p_Stopped);
            p.Stopping += new EventHandler(p_Stopping);
        }
        private void longPool_ProcessNotHealthy(object sender, EventArgs e)
        {
            // Stop unhealthy processes in long pool
            AgentProcess p = (AgentProcess)sender;

            // Check whether service timed out
            if (p.Runtime > p.MaxRuntime)
            {             // Service timed out
                EPSEventLog.WriteEntry("Process timed out in extended queue.  Process will be terminated." + Environment.NewLine +
                                       p.ToString(), System.Diagnostics.EventLogEntryType.Warning);
                p.Stop();
            }
            else
            {             // Service exceeded memory limit
                EPSEventLog.WriteEntry("Process has exceeded maximum allowed memory.  Process will be terminated." + Environment.NewLine +
                                       p.ToString(), System.Diagnostics.EventLogEntryType.Warning);
                p.Stop();
            }
        }