Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pode Watchdog Feature (work in progress) #1416

Draft
wants to merge 45 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
829413f
initial code
mdaneri Oct 5, 2024
71bd22a
additional features
mdaneri Oct 6, 2024
68f7fd4
rewrite
mdaneri Oct 7, 2024
7aac5a0
.
mdaneri Oct 8, 2024
05d2455
improvements
mdaneri Oct 9, 2024
96bf571
fix the bidirectional pipeline
mdaneri Oct 10, 2024
30bf4ff
fixed some issues
mdaneri Oct 10, 2024
c8d45d8
fix restart/start/stop
mdaneri Oct 10, 2024
e13db15
fixes
mdaneri Oct 10, 2024
531c07d
new filemonitoring
mdaneri Oct 10, 2024
8cc857d
Multi watchods
mdaneri Oct 11, 2024
d25d581
Add documentation
mdaneri Oct 11, 2024
f95eb59
Added headers to internal functions
mdaneri Oct 12, 2024
13a2718
update example
mdaneri Oct 12, 2024
06a4812
Added samples
mdaneri Oct 12, 2024
bd1a041
Added documentation
mdaneri Oct 12, 2024
0b2a141
fix tests
mdaneri Oct 12, 2024
8df9426
wait for sessions to end
mdaneri Oct 12, 2024
e8c50bd
updates and improvements
mdaneri Oct 13, 2024
4e90395
test fixes
mdaneri Oct 13, 2024
5b7ac2c
fixes to the close pode
mdaneri Oct 13, 2024
3436b9e
Delete server.psd1
mdaneri Oct 13, 2024
0c5fd22
Update Core.ps1
mdaneri Oct 13, 2024
fd81581
temporary log function
mdaneri Oct 14, 2024
b032528
Merge branch 'watchdog' of https://github.com/mdaneri/Pode into watchdog
mdaneri Oct 14, 2024
7e4cc28
add new Write-PodeWatchdogStartupMessage
mdaneri Oct 14, 2024
91a72ff
Update Helpers.ps1
mdaneri Oct 15, 2024
e1594bf
Add OASchema function
mdaneri Oct 15, 2024
4616d3e
Update Watchdog.ps1
mdaneri Oct 15, 2024
0e6c111
Merge remote-tracking branch 'upstream/develop' into watchdog
mdaneri Oct 16, 2024
300a3a6
Update Watchdog.ps1
mdaneri Oct 16, 2024
81b8466
Merge branch 'develop' into watchdog
mdaneri Oct 19, 2024
683af8f
Merge remote-tracking branch 'upstream/develop' into watchdog
mdaneri Oct 20, 2024
ecde664
Merge remote-tracking branch 'upstream/develop' into watchdog
mdaneri Oct 21, 2024
d6f1197
Merge remote-tracking branch 'upstream/develop' into watchdog
mdaneri Oct 23, 2024
1ad00f7
Merge remote-tracking branch 'upstream/develop' into watchdog
mdaneri Oct 23, 2024
83c8fdd
Merge remote-tracking branch 'upstream/develop' into watchdog
mdaneri Oct 27, 2024
a0fa811
Merge remote-tracking branch 'upstream/develop' into watchdog
mdaneri Oct 28, 2024
8a09218
Merge remote-tracking branch 'upstream/develop' into watchdog
mdaneri Oct 30, 2024
0750e17
Merge remote-tracking branch 'upstream/develop' into watchdog
mdaneri Nov 2, 2024
34741dd
Merge remote-tracking branch 'upstream/develop' into watchdog
mdaneri Nov 3, 2024
c12e406
Update pode.build.ps1
mdaneri Nov 22, 2024
3ab428d
Merge remote-tracking branch 'upstream/develop' into watchdog
mdaneri Nov 22, 2024
3fd4680
Merge remote-tracking branch 'upstream/develop' into watchdog
mdaneri Nov 23, 2024
4652c0e
Merge remote-tracking branch 'upstream/develop' into watchdog
mdaneri Nov 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions docs/Tutorials/Watchdog/Overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Watchdog

The Pode Watchdog feature allows you to monitor and manage processes or scripts running within your Pode server. It provides the ability to track the status of a process, log important events, and interact with the process, including via REST API endpoints.

## Features
- **Process Monitoring**: Continuously track the status, uptime, and performance of processes running under the Pode Watchdog.
- **File Monitoring with Automatic Restart**: Automatically restart a monitored process when changes are detected in files it depends on, such as configuration or critical files.
- **Process Control**: Control the monitored processes through REST API commands such as restart, stop, or reset.
- **Logging**: Watchdog supports logging of important events and errors, which can be useful for auditing and debugging.
- **Automatic Restarts**: If a monitored process crashes unexpectedly, Pode Watchdog will automatically restart it to ensure it remains active.

### How It Works
Pode Watchdog monitors processes or files as configured in your Pode server. Once a process is being monitored, you can interact with it using commands or REST API endpoints. Watchdog continuously tracks the process and ensures it remains active by automatically restarting it when necessary—especially when critical files change.

### Typical Use Cases
1. **Process Monitoring**: Monitor long-running scripts or background services and ensure they are continuously running.
2. **File Monitoring with Automatic Restart**: Watch for changes in key files, such as configuration files, and restart the process automatically when changes are detected.
3. **Automatic Restarts**: Ensure critical processes automatically restart if they crash or when monitored files are modified.
4. **Remote Control**: Use API endpoints to control processes remotely—start, stop, reset, or restart them.

---

### Enabling Pode Watchdog
To begin using the Pode Watchdog feature, specify the process or file you wish to monitor. Here’s an example to monitor a script file and automatically restart the process when the file changes:

```powershell
Enable-PodeWatchdog -FilePath './scripts/myProcess.ps1' -FileMonitoring -FileExclude '*.log' -Name 'myProcessWatchdog'
```

- `-FilePath`: Specifies the path to the script or process you want to monitor.
- `-FileMonitoring`: Enables file monitoring to track changes to the specified file.
- `-FileExclude`: Excludes certain files (e.g., `.log` files) from triggering a restart.
- `-Name`: Assigns a unique identifier for this Watchdog instance.

### **Monitoring**
You can monitor process metrics, such as status, uptime, or other performance data, using the `Get-PodeWatchdogProcessMetric` cmdlet.

### **Controlling the Watchdog**
Pode Watchdog provides full control over monitored processes using the `Set-PodeWatchdogProcessState` cmdlet, allowing you to restart, stop, start, or reset the process.

---

### **Example Usage with RESTful Integration**

Here’s an example of how to set up a Pode server with Watchdog and expose REST API routes to monitor and control the process:

```powershell
Start-PodeServer {
# Define an HTTP endpoint
Add-PodeEndpoint -Address localhost -Port 8082 -Protocol Http

# Path to the monitored script
$filePath = "./scripts/myProcess.ps1"

# Set up Watchdog logging
New-PodeLoggingMethod -File -Name 'watchdog' -MaxDays 4 | Enable-PodeErrorLogging

# Enable Watchdog monitoring for the process
Enable-PodeWatchdog -FilePath $filePath -FileMonitoring -FileExclude '*.log' -Name 'myProcessWatchdog'

# Route to check process status
Add-PodeRoute -Method Get -Path '/monitor/status' -ScriptBlock {
Write-PodeJsonResponse -Value (Get-PodeWatchdogProcessMetric -Name 'myProcessWatchdog' -Type Status)
}

# Route to restart the process
Add-PodeRoute -Method Post -Path '/cmd/restart' -ScriptBlock {
Write-PodeJsonResponse -Value @{success = (Set-PodeWatchdogProcessState -Name 'myProcessWatchdog' -State Restart)}
}
}
```

In this example:
- Pode Watchdog is configured to monitor a script (`myProcess.ps1`).
- The Pode server exposes REST API routes to check the process status (`/monitor/status`) and restart the process (`/cmd/restart`).

2 changes: 1 addition & 1 deletion examples/Logging.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ Start-PodeServer {
Set-PodeResponseAttachment -Path 'Anger.jpg'
}

}
}
51 changes: 51 additions & 0 deletions examples/Watchdog/Watchdog-MultipleInstances.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<#
.SYNOPSIS
A sample PowerShell script to set up a Pode server with multiple Pode Watchdog instances for process monitoring.

.DESCRIPTION
This script sets up a Pode server that listens on port 8082 and configures two Pode Watchdog instances to monitor the same script.
It configures logging for the Watchdog service and monitors the provided script file, excluding `.log` files for both instances.
The script dynamically loads the Pode module and enables multiple instances of the Pode Watchdog service for monitoring.

.EXAMPLE
To run the sample: ./Watchdog-MultipleInstances.ps1

.LINK
https://github.com/Badgerati/Pode/blob/develop/examples/Waatchdog/Watchdog-MultipleInstances.ps1

.NOTES
Author: Pode Team
License: MIT License
#>

try {
# Determine paths for the Pode module
$watchdogPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Path
$podePath = Split-Path -Parent -Path (Split-Path -Parent -Path $watchdogPath)

# Import the Pode module from the source path if it exists, otherwise from installed modules
if (Test-Path -Path "$($podePath)/src/Pode.psm1" -PathType Leaf) {
Import-Module "$($podePath)/src/Pode.psm1" -Force -ErrorAction Stop
}
else {
Import-Module -Name 'Pode' -MaximumVersion 2.99 -ErrorAction Stop
}
}
catch { throw }

Start-PodeServer {
# Define a simple HTTP endpoint on localhost:8082
Add-PodeEndpoint -Address localhost -Port 8082 -Protocol Http

# Path to the monitored script
$filePath = "$($watchdogPath)/monitored.ps1"

# Set up logging for the Watchdog service with a 4-day retention period
New-PodeLoggingMethod -File -Name 'watchdog' -MaxDays 4 | Enable-PodeErrorLogging

# Enable the first Pode Watchdog instance to monitor the script file, excluding .log files
Enable-PodeWatchdog -FilePath $filePath -FileMonitoring -Parameters @{Port = 8080 } -FileExclude '*.log' -Name 'watch01'

# Enable the second Pode Watchdog instance to monitor the script file, excluding .log files
Enable-PodeWatchdog -FilePath $filePath -FileMonitoring -Parameters @{Port = 8081 } -FileExclude '*.log' -Name 'watch02'
}
150 changes: 150 additions & 0 deletions examples/Watchdog/Watchdog-OpenApiIntegration.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<#
.SYNOPSIS
A Pode server setup with OpenAPI integration that monitors a script using the Pode Watchdog service.

.DESCRIPTION
This script initializes a Pode server with OpenAPI documentation and multiple routes to monitor the status, listeners, requests,
and signals of a monitored process via the Pode Watchdog service.
It also provides commands to control the state of the monitored process, such as restart, stop, reset, and halt.
The script dynamically loads the Pode module and configures OpenAPI viewers and an editor for documentation.
This sample demonstrates the integration of Pode Watchdog with OpenAPI routes for monitoring and controlling processes.

.EXAMPLE
Run the script to start a Pode server on localhost at port 8082 with OpenAPI documentation:

./Watchdog-OpenApiIntegration.ps1

.LINK
https://github.com/Badgerati/Pode/blob/develop/examples/Watchdog/Watchdog-OpenApiIntegration.ps1

.NOTES
Author: Pode Team
License: MIT License
#>

try {
# Determine paths for the Pode module
$watchdogPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Path
$podePath = Split-Path -Parent -Path (Split-Path -Parent -Path $watchdogPath)

# Import the Pode module from the source path if it exists, otherwise from installed modules
if (Test-Path -Path "$($podePath)/src/Pode.psm1" -PathType Leaf) {
Import-Module "$($podePath)/src/Pode.psm1" -Force -ErrorAction Stop
}
else {
Import-Module -Name 'Pode' -MaximumVersion 2.99 -ErrorAction Stop
}
}
catch { throw }

Start-PodeServer -Threads 2 {
# Define a simple HTTP endpoint on localhost:8082
Add-PodeEndpoint -Address localhost -Port 8082 -Protocol Http

# Enable OpenAPI with OpenAPI version 3.0.3 and disable minimal definitions
Enable-PodeOpenApi -Path '/docs/openapi' -OpenApiVersion '3.0.3' -DisableMinimalDefinitions
Add-PodeOAInfo -Title 'Pode Watchdog sample' -Version 1.0.0

# Enable various OpenAPI viewers for documentation
Enable-PodeOAViewer -Type Swagger -Path '/docs/swagger'
Enable-PodeOAViewer -Type ReDoc -Path '/docs/redoc'
Enable-PodeOAViewer -Type RapiDoc -Path '/docs/rapidoc'
Enable-PodeOAViewer -Type StopLight -Path '/docs/stoplight'
Enable-PodeOAViewer -Type Explorer -Path '/docs/explorer'
Enable-PodeOAViewer -Type RapiPdf -Path '/docs/rapipdf'

# Enable OpenAPI editor and bookmarks for easier documentation navigation
Enable-PodeOAViewer -Editor -Path '/docs/swagger-editor'
Enable-PodeOAViewer -Bookmarks -Path '/docs'

# Path to the monitored script
$filePath = "$($watchdogPath)/monitored.ps1"

# Set up logging for the Watchdog service
New-PodeLoggingMethod -File -Name 'watchdog' -MaxDays 4 | Enable-PodeErrorLogging

# Enable the Pode Watchdog to monitor the script file, excluding .log files
Enable-PodeWatchdog -FilePath $filePath -Parameters @{Port = 8081 } -FileMonitoring -FileExclude '*.log' -Name 'watch01' -RestartServiceAfter 10 -MaxNumberOfRestarts 2 -ResetFailCountAfter 3

# Define OpenAPI schemas for request, listener, signal, and status metrics
$WatchdogSchemaPrefix = 'Watchdog'
Add-PodeWatchdogOASchema -WatchdogSchemaPrefix $WatchdogSchemaPrefix

# REST API to retrieve the list of listeners
Add-PodeRoute -PassThru -Method Get -Path '/monitor/listeners' -ScriptBlock {
Write-PodeJsonResponse -StatusCode 200 -Value (Get-PodeWatchdogProcessMetric -Name 'watch01' -type Listeners)
} | Set-PodeOARouteInfo -Summary 'Retrieves a list of active listeners for the monitored Pode server' -Tags 'Monitor' -OperationId 'getListeners' -PassThru |
Add-PodeOAResponse -StatusCode 200 -Description 'Successful operation' -Content @{'application/json' = "$($WatchdogSchemaPrefix)Listeners" }

# REST API to retrieve the request count
Add-PodeRoute -PassThru -Method Get -Path '/monitor/requests' -ScriptBlock {
Write-PodeJsonResponse -StatusCode 200 -Value (Get-PodeWatchdogProcessMetric -Name 'watch01' -type Requests)
} | Set-PodeOARouteInfo -Summary 'Retrieves the total number of requests handled by the monitored Pode server' -Tags 'Monitor' -OperationId 'getRequests' -PassThru |
Add-PodeOAResponse -StatusCode 200 -Description 'Successful operation' -Content @{'application/json' = "$($WatchdogSchemaPrefix)Requests" }

# REST API to retrieve the process status
Add-PodeRoute -PassThru -Method Get -Path '/monitor/status' -ScriptBlock {
Write-PodeJsonResponse -StatusCode 200 -Value (Get-PodeWatchdogProcessMetric -Name 'watch01' -type Status)
} | Set-PodeOARouteInfo -Summary 'Retrieves the current status and uptime of the monitored Pode server' -Tags 'Monitor' -OperationId 'getStatus' -PassThru |
Add-PodeOAResponse -StatusCode 200 -Description 'Successful operation' -Content @{'application/json' = "$($WatchdogSchemaPrefix)Status" }

# REST API to retrieve signal metrics
Add-PodeRoute -PassThru -Method Get -Path '/monitor/signals' -ScriptBlock {
Write-PodeJsonResponse -StatusCode 200 -Value (Get-PodeWatchdogProcessMetric -Name 'watch01' -type Signals)
} | Set-PodeOARouteInfo -Summary 'Retrieves signal metrics for the monitored Pode server' -Tags 'Monitor' -OperationId 'getSignals' -PassThru |
Add-PodeOAResponse -StatusCode 200 -Description 'Successful operation' -Content @{'application/json' = "$($WatchdogSchemaPrefix)Signals" }

# REST API to retrieve all metrics of the monitored process
Add-PodeRoute -PassThru -Method Get -Path '/monitor' -ScriptBlock {
Write-PodeJsonResponse -StatusCode 200 -Value (Get-PodeWatchdogProcessMetric -Name 'watch01')
} | Set-PodeOARouteInfo -Summary 'Retrieves all monitoring stats for the monitored Pode server' -Tags 'Monitor' -OperationId 'getMonitor' -PassThru |
Add-PodeOAResponse -StatusCode 200 -Description 'Successful operation' -Content @{'application/json' = "$($WatchdogSchemaPrefix)Monitor" }

# REST API to restart the monitored process
Add-PodeRoute -PassThru -Method Post -Path '/cmd/restart' -ScriptBlock {
Write-PodeJsonResponse -StatusCode 200 -Value @{ success = (Set-PodeWatchdogProcessState -Name 'watch01' -State Restart) }
} | Set-PodeOARouteInfo -Summary 'Restarts the monitored Pode server' -Tags 'Command' -OperationId 'restart'

# REST API to reset the monitored process
Add-PodeRoute -PassThru -Method Post -Path '/cmd/reset' -ScriptBlock {
Write-PodeJsonResponse -StatusCode 200 -Value @{ success = (Set-PodeWatchdogProcessState -Name 'watch01' -State Reset) }
} | Set-PodeOARouteInfo -Summary 'Stops and restarts the monitored Pode server process' -Tags 'Command' -OperationId 'reset'

# REST API to stop the monitored process
Add-PodeRoute -PassThru -Method Post -Path '/cmd/stop' -ScriptBlock {
Write-PodeJsonResponse -StatusCode 200 -Value @{ success = (Set-PodeWatchdogProcessState -Name 'watch01' -State Stop) }
} | Set-PodeOARouteInfo -Summary 'Stops the monitored Pode server process' -Tags 'Command' -OperationId 'stop'

# REST API to start the monitored process
Add-PodeRoute -PassThru -Method Post -Path '/cmd/start' -ScriptBlock {
Write-PodeJsonResponse -StatusCode 200 -Value @{ success = (Set-PodeWatchdogProcessState -Name 'watch01' -State Start) }
} | Set-PodeOARouteInfo -Summary 'Starts the monitored Pode server process' -Tags 'Command' -OperationId 'start'

# REST API to terminate (force stop) the monitored process
Add-PodeRoute -PassThru -Method Post -Path '/cmd/terminate' -ScriptBlock {
Write-PodeJsonResponse -StatusCode 200 -Value @{ success = (Set-PodeWatchdogProcessState -Name 'watch01' -State Terminate) }
} | Set-PodeOARouteInfo -Summary 'Terminates (force stops) the monitored Pode server process' -Tags 'Command' -OperationId 'terminate'

# REST API to disable the monitored process
Add-PodeRoute -PassThru -Method Post -Path '/cmd/disable' -ScriptBlock {
Write-PodeJsonResponse -StatusCode 200 -Value @{ success = (Set-PodeWatchdogProcessState -Name 'watch01' -State Disable) }
} | Set-PodeOARouteInfo -Summary 'Disables the monitored Pode server process' -Tags 'Command' -OperationId 'disable'

# REST API to enable the monitored process
Add-PodeRoute -PassThru -Method Post -Path '/cmd/enable' -ScriptBlock {
Write-PodeJsonResponse -StatusCode 200 -Value @{ success = (Set-PodeWatchdogProcessState -Name 'watch01' -State Enable) }
} | Set-PodeOARouteInfo -Summary 'Enables the monitored Pode server process' -Tags 'Command' -OperationId 'enable'

# REST API to disable Auto Restart
Add-PodeRoute -PassThru -Method Post -Path '/settings/noAutoRestart' -ScriptBlock {
Disable-PodeWatchdogAutoRestart -Name 'watch01'
Set-PodeResponseStatus -Code 200
} | Set-PodeOARouteInfo -Summary 'Disables the auto-restart feature for the monitored Pode server process' -Tags 'Settings' -OperationId 'noAutoRestart'

# REST API to enable Auto Restart
Add-PodeRoute -PassThru -Method Post -Path '/settings/autoRestart' -ScriptBlock {
Enable-PodeWatchdogAutoRestart -Name 'watch01'
Set-PodeResponseStatus -Code 200
} | Set-PodeOARouteInfo -Summary 'Enables the auto-restart feature for the monitored Pode server process' -Tags 'Settings' -OperationId 'autoRestart'

}
47 changes: 47 additions & 0 deletions examples/Watchdog/Watchdog-SingleInstance.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<#
.SYNOPSIS
A sample PowerShell script to set up a Pode server with a Pode Watchdog for process monitoring.

.DESCRIPTION
This script sets up a Pode server that listens on port 8082 and monitors a script using the Pode Watchdog service.
It configures logging for the Watchdog service and monitors the provided script file, excluding `.log` files.
The script dynamically loads the Pode module and sets up basic process monitoring using the Pode Watchdog.

.EXAMPLE
To run the sample: ./Watchdog-Sample.ps1

.LINK
https://github.com/Badgerati/Pode/blob/develop/examples/Waatchdog/Watchdog-SingleInstance.ps1

.NOTES
Author: Pode Team
License: MIT License
#>


try {
# Determine paths for the Pode module
$watchdogPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Path
$podePath = Split-Path -Parent -Path (Split-Path -Parent -Path $watchdogPath)

# Import the Pode module from the source path if it exists, otherwise from installed modules
if (Test-Path -Path "$($podePath)/src/Pode.psm1" -PathType Leaf) {
Import-Module "$($podePath)/src/Pode.psm1" -Force -ErrorAction Stop
}
else {
Import-Module -Name 'Pode' -MaximumVersion 2.99 -ErrorAction Stop
}
}
catch { throw }

Start-PodeServer {
# Define a simple HTTP endpoint on localhost:8082
Add-PodeEndpoint -Address localhost -Port 8082 -Protocol Http

# Path to the monitored script
$filePath = "$($watchdogPath)/monitored.ps1"

# Enable the Pode Watchdog to monitor the script file, excluding .log files
Enable-PodeWatchdog -FilePath $filePath -Parameters @{Port = 8081 } -FileMonitoring -FileExclude '*.log' -Name 'watch01'

}
Loading