loop through stuck process and kill them
Gitea Actions Demo / Explore-Gitea-Actions (8.0.x) (push) Successful in 2m7s

This commit is contained in:
2025-11-13 14:53:17 +00:00
parent 21b7f862f9
commit 873438b4bf
+260 -187
View File
@@ -1,188 +1,261 @@
using System.Configuration; using System.Configuration;
using System.Configuration.Internal; using System.Configuration.Internal;
//using Microsoft.Extensions.Configuration; using System.Diagnostics;
namespace PrometheusServiceManager;
using System.ServiceProcess; //using Microsoft.Extensions.Configuration;
namespace PrometheusServiceManager;
using System.ServiceProcess;
// versions
// V1 main release to customers system
// V1.1 adding the Administrator option at the start // versions
// cancelled as the program would crash , to be investigated // V1 main release to customers system
// V1.2 displaying starting complete 2025/10/15 // V1.1 adding the Administrator option at the start
static class Program // cancelled as the program would crash , to be investigated
{ // V1.2 displaying starting complete 2025/10/15
private static readonly Dictionary<string,string> ServicesList = ConfigurationManager.AppSettings.AllKeys.ToDictionary(key => key, key => ConfigurationManager.AppSettings[key]); // V1.3 detect stuck processes and kill them
public static bool KeycloakHidden = true; static class Program
public static bool PostgreSqlHidden = true; {
private static readonly Dictionary<string,string> ServicesList = ConfigurationManager.AppSettings.AllKeys.ToDictionary(key => key, key => ConfigurationManager.AppSettings[key]);
public static readonly string version = "V1.2"; public static bool KeycloakHidden = true;
public static bool PostgreSqlHidden = true;
[STAThread]
public static readonly string version = "V1.3";
private static void StartService(string serviceName, int timeoutMilliseconds)
{ [STAThread]
ServiceController service = new ServiceController(serviceName);
try private static void StartService(string serviceName, int timeoutMilliseconds)
{ {
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); ServiceController service = new ServiceController(serviceName);
try
service.Start(); {
service.WaitForStatus(ServiceControllerStatus.Running, timeout); TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
}
catch service.Start();
{ service.WaitForStatus(ServiceControllerStatus.Running, timeout);
// ... }
} catch
} {
// ...
private static void StopService(string serviceName, int timeoutMilliseconds) }
{ }
ServiceController service = new ServiceController(serviceName);
try private static void StopService(string serviceName, int timeoutMilliseconds)
{ {
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); ServiceController service = new ServiceController(serviceName);
try
service.Stop(); {
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
}
catch service.Stop();
{ service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
// ... }
} catch
} {
public static void StartServices() // ...
{ }
if(!Form1.KeycloakCheckBox.Checked) }
{ public static void StartServices()
Console.WriteLine("Keycloak unchecked"); {
ServicesList.Remove("_keycloak"); if(!Form1.KeycloakCheckBox.Checked)
Form1.KeycloakCheckBox.Visible = false; {
Console.WriteLine("Keycloak unchecked");
} ServicesList.Remove("_keycloak");
foreach (KeyValuePair<string,string> Service in ServicesList) Form1.KeycloakCheckBox.Visible = false;
{
}
//Console.WriteLine("checked: "+Form1.KeycloakCheckBox.Checked); foreach (KeyValuePair<string,string> Service in ServicesList)
//write the service name in the log {
Console.WriteLine("Starting: "+Service.Key);
//start the service //Console.WriteLine("checked: "+Form1.KeycloakCheckBox.Checked);
StartService(Service.Key, 1000); //write the service name in the log
//update the text box Console.WriteLine("Starting: "+Service.Key);
Form1.TextDisplay.Text = "Starting: "+Service.Key; //start the service
Form1.TextDisplay.Update(); StartService(Service.Key, 1000);
//wait 1 second before //update the text box
Thread.Sleep(TimeSpan.FromSeconds(1)); Form1.TextDisplay.Text = "Starting: "+Service.Key;
Form1.TextDisplay.Update();
//displaying end of operation //wait 1 second before
Form1.TextDisplay.Text = "Starting complete ! "; Thread.Sleep(TimeSpan.FromSeconds(1));
Form1.TextDisplay.Update();
//displaying end of operation
} Form1.TextDisplay.Text = "Starting complete ! ";
Form1.TextDisplay.Update();
}
}
public static void ShutdownServices(bool keycloakRestartRequired) }
{
if(!Form1.KeycloakCheckBox.Checked)
{ public static void ShutdownServices(bool keycloakRestartRequired)
Console.WriteLine("Keycloak unchecked"); {
ServicesList.Remove("_keycloak"); //DisplayNetstat();
Form1.KeycloakCheckBox.Visible = false; Thread.Sleep(TimeSpan.FromSeconds(3));
} if(!Form1.KeycloakCheckBox.Checked)
foreach (KeyValuePair<string,string> Service in ServicesList.Reverse()) {
{ Console.WriteLine("Keycloak unchecked");
//write the service name in the log ServicesList.Remove("_keycloak");
Console.WriteLine("Stopping: "+Service.Key); Form1.KeycloakCheckBox.Visible = false;
//start the service }
StopService(Service.Key, 1000); foreach (KeyValuePair<string,string> Service in ServicesList.Reverse())
//update the text box {
Form1.TextDisplay.Text = "Stopping: "+Service.Key; //write the service name in the log
Form1.TextDisplay.Update(); Console.WriteLine("Stopping: "+Service.Key);
//wait 1 second before //start the service
Thread.Sleep(TimeSpan.FromSeconds(1)); StopService(Service.Key, 1000);
//update the text box
//displaying end of operation Form1.TextDisplay.Text = "Stopping: "+Service.Key;
Form1.TextDisplay.Text = "Stopping complete ! "; Form1.TextDisplay.Update();
Form1.TextDisplay.Update(); //wait 1 second before
Thread.Sleep(TimeSpan.FromSeconds(1));
} //check if there is a process still active
Process[] localByName = Process.GetProcessesByName(Service.Value);
} if (localByName.Length > 0)
public static void RestartServices(bool keycloakRestartRequired) {
{ Form1.TextDisplay.Text = "there is a process running ";
if(!Form1.KeycloakCheckBox.Checked) Form1.TextDisplay.Update();
{ Thread.Sleep(TimeSpan.FromSeconds(1));
Console.WriteLine("Keycloak unchecked");
ServicesList.Remove("_keycloak"); //loop through every process if multiples
Form1.KeycloakCheckBox.Visible = false; foreach (Process process in localByName)
{
} process.Kill();
//stop section }
foreach (KeyValuePair<string,string> Service in ServicesList.Reverse())
{ Form1.TextDisplay.Text = "kill done ";
//write the service name in the log Form1.TextDisplay.Update();
Console.WriteLine("Stopping: "+Service.Key);
//stop the service }
StopService(Service.Key, 1000);
//update the text box //displaying end of operation
Form1.TextDisplay.Text = "Stopping: "+Service.Key; Form1.TextDisplay.Text = "Stopping complete ! ";
Form1.TextDisplay.Update(); Form1.TextDisplay.Update();
//wait 1 second before
Thread.Sleep(TimeSpan.FromSeconds(1));
}
//displaying end of operation
Form1.TextDisplay.Text = "Stopping complete ! "; }
Form1.TextDisplay.Update(); public static void RestartServices(bool keycloakRestartRequired)
{
if(!Form1.KeycloakCheckBox.Checked)
} {
Form1.TextDisplay.Text = "Stopping Complete, waiting... "; Console.WriteLine("Keycloak unchecked");
Form1.TextDisplay.Update(); ServicesList.Remove("_keycloak");
//wait 3 second before Form1.KeycloakCheckBox.Visible = false;
Thread.Sleep(TimeSpan.FromSeconds(3));
}
//start section //stop section
foreach (KeyValuePair<string,string> Service in ServicesList) foreach (KeyValuePair<string,string> Service in ServicesList.Reverse())
{ {
//write the service name in the log //write the service name in the log
Console.WriteLine("Starting: "+Service.Key); Console.WriteLine("Stopping: "+Service.Key);
//start the service //stop the service
StartService(Service.Key, 1000); StopService(Service.Key, 1000);
//update the text box //update the text box
Form1.TextDisplay.Text = "Starting: "+Service.Key; Form1.TextDisplay.Text = "Stopping: "+Service.Key;
Form1.TextDisplay.Update(); Form1.TextDisplay.Update();
//wait 1 second before //wait 1 second before
Thread.Sleep(TimeSpan.FromSeconds(1)); Thread.Sleep(TimeSpan.FromSeconds(1));
//displaying end of operation //displaying end of operation
Form1.TextDisplay.Text = "Starting complete ! "; Form1.TextDisplay.Text = "Stopping complete ! ";
Form1.TextDisplay.Update(); Form1.TextDisplay.Update();
}
} }
static void Main() Form1.TextDisplay.Text = "Stopping Complete, waiting... ";
{ Form1.TextDisplay.Update();
//wait 3 second before
Thread.Sleep(TimeSpan.FromSeconds(3));
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration. //start section
foreach (KeyValuePair<string,string> Service in ServicesList)
if (ServicesList.ContainsKey("_keycloak")) {
{ //write the service name in the log
Program.KeycloakHidden = false; Console.WriteLine("Starting: "+Service.Key);
Console.WriteLine("keycloak is there"); //start the service
} StartService(Service.Key, 1000);
if (ServicesList.ContainsKey("_postgresql")) //update the text box
{ Form1.TextDisplay.Text = "Starting: "+Service.Key;
Program.PostgreSqlHidden = false; Form1.TextDisplay.Update();
Console.WriteLine("postgresql is there"); //wait 1 second before
} Thread.Sleep(TimeSpan.FromSeconds(1));
ApplicationConfiguration.Initialize();
Application.Run(new Form1()); //check if there is a process still active
Process[] localByName = Process.GetProcessesByName(Service.Value);
if (localByName.Length > 0)
} {
Form1.TextDisplay.Text = "there is a process running ";
Form1.TextDisplay.Update();
Thread.Sleep(TimeSpan.FromSeconds(1));
//loop through every process if multiples
foreach (Process process in localByName)
{
process.Kill();
}
Form1.TextDisplay.Text = "kill done ";
Form1.TextDisplay.Update();
}
//displaying end of operation
Form1.TextDisplay.Text = "Starting complete ! ";
Form1.TextDisplay.Update();
}
}
public static void DisplayNetstat()
{
try
{
Process[] localByName = Process.GetProcessesByName("TurnaroundService.api");
if (localByName.Length > 0)
{
Form1.TextDisplay.Text = "there is a service running ";
Form1.TextDisplay.Update();
Thread.Sleep(TimeSpan.FromSeconds(2));
localByName[0].Kill();
Form1.TextDisplay.Text = "kill done ";
Form1.TextDisplay.Update();
}
}
catch (Exception e)
{
Form1.TextDisplay.Text = "Kill failed ! ";
Form1.TextDisplay.Update();
Console.WriteLine(e);
throw;
}
}
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
if (ServicesList.ContainsKey("_keycloak"))
{
Program.KeycloakHidden = false;
Console.WriteLine("keycloak is there");
}
if (ServicesList.ContainsKey("_postgresql"))
{
Program.PostgreSqlHidden = false;
Console.WriteLine("postgresql is there");
}
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
} }