Skip to content

Commit

Permalink
Add option to execute custom command for service/app restart
Browse files Browse the repository at this point in the history
  • Loading branch information
zimbres committed Nov 15, 2024
1 parent b9858c7 commit ea3870a
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
tag=$(git describe --tags --abbrev=0)
release_name="ServiceMonitor-$tag-${{ matrix.target }}"
dotnet publish src/ServiceMonitor/ServiceMonitor.csproj --framework net8.0 --runtime "${{ matrix.target }}" --no-self-contained -p:PublishSingleFile=true -c Release -o "$release_name"
dotnet publish src/ServiceMonitor/ServiceMonitor.csproj --framework net8.0 --runtime "${{ matrix.target }}" --no-self-contained -p:PublishSingleFile=false -c Release -o "$release_name"
if [ "${{ matrix.target }}" == "win-x64" ]; then
7z a -tzip "${release_name}.zip" "./${release_name}/*"
Expand Down
3 changes: 2 additions & 1 deletion src/ServiceMonitor/Configurations/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ internal class Configuration
public List<Service> Services { get; set; }
}

internal class Service
public class Service
{
public string ServiceName { get; set; }
public string IpAddress { get; set; }
public int Port { get; set; }
public string HttpUrl { get; set; }
public string WordToCheck { get; set; }
public string CustomCommand { get; set; }
}
3 changes: 1 addition & 2 deletions src/ServiceMonitor/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@

using System.Diagnostics.CodeAnalysis;

//[assembly: SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>", Scope = "member", Target = "~M:ServiceMonitor.Worker.RestartService(System.String)")]
[assembly: SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>", Scope = "member", Target = "~M:ServiceMonitor.Services.ControlService.RestartService(System.String)")]
[assembly: SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>", Scope = "member", Target = "~M:ServiceMonitor.Services.ControlService.RestartService(ServiceMonitor.Configurations.Service)")]
1 change: 1 addition & 0 deletions src/ServiceMonitor/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
global using ServiceMonitor;
global using ServiceMonitor.Configurations;
global using ServiceMonitor.Services;
global using System.Management.Automation;
global using System.Net.Sockets;
global using System.Reflection;
global using System.ServiceProcess;
Expand Down
11 changes: 6 additions & 5 deletions src/ServiceMonitor/ServiceMonitor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<UserSecretsId>dotnet-ServiceMonitor-02b8361b-7a93-472d-a2d6-7b2e8f7973af</UserSecretsId>
<AssemblyVersion>$(PackageVersion)</AssemblyVersion>
<FileVersion>$(PackageVersion)</FileVersion>
<Version>2.0.0.2</Version>
<Version>3.0.0.1</Version>
<ApplicationIcon>service.ico</ApplicationIcon>
<Copyright>Zimbres.COM</Copyright>
</PropertyGroup>
Expand All @@ -17,9 +17,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="System.ServiceProcess.ServiceController" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.4.6" />
<PackageReference Include="System.ServiceProcess.ServiceController" Version="8.0.1" />
</ItemGroup>
</Project>
25 changes: 17 additions & 8 deletions src/ServiceMonitor/Services/ControlService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,39 @@ public ControlService(ILogger<ControlService> logger)
_logger = logger;
}

public void RestartService(string serviceName)
public void RestartService(Service service)
{
try
{
using var serviceController = new ServiceController(serviceName);
if (service.CustomCommand != null)
{
using PowerShell powerShell = PowerShell.Create();
powerShell.AddScript(service.CustomCommand);
var processes = powerShell.Invoke();
_logger.LogWarning("Custom command '{service.CustomCommand}' executed.", service.CustomCommand);
return;
}

using var serviceController = new ServiceController(service.ServiceName);
if (serviceController.Status == ServiceControllerStatus.Stopped)
{
_logger.LogWarning("Starting service {serviceName}...", serviceName);
_logger.LogWarning("Starting service {serviceName}...", service.ServiceName);
serviceController.Start();
}

else if (serviceController.Status == ServiceControllerStatus.Running)
{
_logger.LogWarning("Restarting service {serviceName}...", serviceName);
_logger.LogWarning("Restarting service {serviceName}...", service.ServiceName);
serviceController.Stop();
serviceController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(60));
serviceController.Start();
}

if (serviceController.Status == ServiceControllerStatus.StopPending)
{
_logger.LogWarning("Waiting service {serviceName} to stop...", serviceName);
_logger.LogWarning("Waiting service {serviceName} to stop...", service.ServiceName);
serviceController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(60));
_logger.LogWarning("Starting service {serviceName}...", serviceName);
_logger.LogWarning("Starting service {serviceName}...", service.ServiceName);
serviceController.Start();
}
serviceController.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(60));
Expand Down
4 changes: 2 additions & 2 deletions src/ServiceMonitor/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ private async Task ProcessService(Service service)
{
if (!_tcpService.CheckTcpPortListening(service.IpAddress, service.Port))
{
_controlService.RestartService(service.ServiceName);
_controlService.RestartService(service);
}
}

if (service.HttpUrl is not null)
{
if (!await _httpService.CheckHttpAsync(service.HttpUrl, service.WordToCheck))
{
_controlService.RestartService(service.ServiceName);
_controlService.RestartService(service);
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/ServiceMonitor/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"IpAddress": "127.0.0.1",
"Port": 8080
},
{
"ServiceName": "SMTP",
"IpAddress": "127.0.0.1",
"Port": 25,
"CustomCommand": "dir"
},
{
"ServiceName": "YourServiceName",
"HttpUrl": "http://localhost:3000/api/health",
Expand Down

0 comments on commit ea3870a

Please sign in to comment.