using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.Timers; using System.ConsoleApp; using System.ConsoleApp.Commands; using System.Net.ApiService; using Keln.Progress.EventHandler; namespace Keln.Progress { public class ProcessInfo { private Process _process; private Timer _timer = new Timer(10000); private Timer _forceExitTimer = new Timer(10000); private Timer _restartTimer = new Timer(10000); private int _port; private string _uri; private eState _state = eState.NotRunning; private System.IO.FileInfo _fi; /*public delegate void StateChangeEventHandle(); public delegate void StartEventHandler(); public delegate void StopEventHandler();*/ public event EventHandler StateChange_Event; protected virtual void OnStateChange(StateChangeEventArgs e) { EventHandler handler = StateChange_Event; if (handler != null) { handler(this, e); } } public event EventHandler Start_Event; protected virtual void OnStart(StartEventArgs e) { EventHandler handler = Start_Event; if (handler != null) { handler(this, e); } } public event EventHandler Stop_Event; protected virtual void OnStop(StopEventArgs e) { EventHandler handler = Stop_Event; if (handler != null) { handler(this, e); } } public ProcessInfo(string processPath) { this._process = new Process(); this.InitProcess(processPath); this._process.Exited += _process_Exited; this._process.EnableRaisingEvents = true; this.initTimer(); this._state = eState.NotRunning; } public ProcessInfo(string processPath, bool Autostart) { this._process = new Process(); this.InitProcess(processPath); this._process.Exited += _process_Exited; this._process.EnableRaisingEvents = true; this.initTimer(); if(Autostart && this._state != eState.Already_Running) { this.Start(); } } public ProcessInfo(string processPath, EventHandler StateChangeEvent) { this._process = new Process(); this.InitProcess(processPath); this._process.Exited += _process_Exited; this._process.EnableRaisingEvents = true; this.initTimer(); this.StateChange_Event = StateChangeEvent; OnStateChange(new StateChangeEventArgs() { State = this._state }); } public ProcessInfo(string processPath, bool Autostart, EventHandler StateChangeEvent) { this._process = new Process(); this.InitProcess(processPath); this._process.Exited += _process_Exited; this._process.EnableRaisingEvents = true; this.initTimer(); this.StateChange_Event = StateChangeEvent; if (Autostart && this._state != eState.Already_Running) { this.Start(); } else { OnStateChange(new StateChangeEventArgs() { State = this._state }); } } private void InitProcess(string path) { this._fi = new System.IO.FileInfo(path); Process[] currentRunning = Process.GetProcesses(); bool bFound = false; string xpath = this._fi.FullName; string name = this._fi.Name; if (name.Contains(".")) name = name.Substring(0, name.LastIndexOf(".")); foreach (Process p in currentRunning) { if(p.ProcessName == name && p.MainModule.FileName == xpath) { bFound = true; this._process = Process.GetProcessById(p.Id); this._state = eState.Already_Running; break; } } if (!bFound) { this._process.StartInfo.FileName = this._fi.FullName; this._process.StartInfo.UseShellExecute = true; } } private void initTimer() { this._forceExitTimer.Elapsed += Timer_ForceExit; this._restartTimer.Elapsed += Timer_Restart; this._timer.Elapsed += Timer_CheckProcess; } private void Timer_CheckProcess(object sender, ElapsedEventArgs e) { if(this._process.HasExited && this._state == eState.Running) { this._doRestart(); } } public void Start() { if (this._process != null && this._fi.Exists) { this._state = eState.Running; OnStart(new StartEventArgs()); OnStateChange(new StateChangeEventArgs() { State = this._state }); //BaseCommand.WriteLine("Starte Service: " + this._fi.Name, ConsoleColor.Green); this._process.Start(); } else { this._state = eState.NotRunning; OnStateChange(new StateChangeEventArgs() { State = this._state }); } } public void Restart() { if (this._process != null && this._fi.Exists) { this._state = eState.Restart; OnStateChange(new StateChangeEventArgs() { State = this._state }); //BaseCommand.WriteLine("Retarte Service: " + this._fi.Name, ConsoleColor.Yellow); this._doRestart(); } } public string ReadOutput() { if (this._process != null) { if (this._process.StandardOutput == null) return ""; return this._process.StandardOutput.ReadToEnd(); } return ""; } private void _doRestart() { if (this._process != null && this._fi.Exists) { if (this._state == eState.Running) { // Restart application this._state = eState.Restart; OnStateChange(new StateChangeEventArgs() { State = this._state }); this._restartTimer.Start(); } else if (this._state == eState.Restart) { this._restartTimer.Start(); } } } public void Stop() { if (this._process != null && this._fi.Exists) { this._state = eState.Stopping; OnStateChange(new StateChangeEventArgs() { State = this._state }); if (!string.IsNullOrEmpty(this._uri)) { client.Get_String(this._uri + "/qqq.cgi"); this._forceExitTimer.Start(); } else { this._process.Kill(); this._process.Close(); this._state = eState.Stopped; OnStateChange(new StateChangeEventArgs() { State = this._state }); } //BaseCommand.WriteLine("Stop Service: " + this._fi.Name, ConsoleColor.Red); } } private void Timer_ForceExit(object sender, ElapsedEventArgs e) { try { this._process.Kill(); } catch(Exception ex) { } this._process.Close(); this._forceExitTimer.Stop(); this._state = eState.Stopped; OnStateChange(new StateChangeEventArgs() { State = this._state }); } private void _process_Exited(object sender, EventArgs e) { //BaseCommand.WriteLine("Service was exit: " + this._fi.Name, ConsoleColor.Red); this._doRestart(); } private void Timer_Restart(object sender, ElapsedEventArgs e) { this._restartTimer.Stop(); //BaseCommand.WriteLine("About to restart: " + this._fi.Name, ConsoleColor.Green); this.Start(); } public eState State { get { return this._state; } } public TimeSpan TotalProcessorTime { get { if (this._process != null && this._state == eState.Running) return this._process.TotalProcessorTime; return new TimeSpan(0); } } public long VirtualMemorySize64 { get { if (this._process != null && this._state == eState.Running) return this._process.VirtualMemorySize64; return 0; } } public TimeSpan UserProcessorTime { get { if (this._process != null && this._state == eState.Running) return this._process.UserProcessorTime; return new TimeSpan(0); } } public DateTime StartTime { get { if (this._process != null && this._state == eState.Running) return this._process.StartTime; return new DateTime(); } } public string ProcessName { get { if (this._process != null && this._state == eState.Running) return this._process.ProcessName; return ""; } } public long PrivateMemorySize64 { get { if (this._process != null && this._state == eState.Running) return this._process.PrivateMemorySize64; return 0; } } public long PeakVirtualMemorySize64 { get { if (this._process != null && this._state == eState.Running) return this._process.PeakVirtualMemorySize64; return 0; } } public long PeakWorkingSet64 { get { if (this._process != null && this._state == eState.Running) return this._process.PeakWorkingSet64; return 0; } } public long PeakPagedMemorySize64 { get { if (this._process != null && this._state == eState.Running) return this._process.PeakPagedMemorySize64; return 0; } } public long PagedSystemMemorySize64 { get { if (this._process != null && this._state == eState.Running) return this._process.PagedSystemMemorySize64; return 0; } } public long PagedMemorySize64 { get { if(this._process != null && this._state == eState.Running) return this._process.PagedMemorySize64; return 0; } } public int ExitCode { get { if (this._process != null && this._state == eState.Running) return this._process.ExitCode; return -1; } } } }