Badgerati / Pode

Pode is a Cross-Platform PowerShell web framework for creating REST APIs, Web Sites, and TCP/SMTP servers
https://badgerati.github.io/Pode
MIT License
830 stars 92 forks source link

Naming Runspaces #1363

Open HeyItsGilbert opened 1 month ago

HeyItsGilbert commented 1 month ago

Describe the Change

When inspecting a Pode server we often see several runspaces in different states. I know there are several runspaces for schedules and route listenings. It would be useful if the runspaces where named.

Currently

  1. Find the PODE Process ID
  2. Enter-PSHostProcess -Id <pid>
  3. Get-Runspace
[Process:9048]: PS > Get-Runspace

 Id Name            ComputerName    Type          State         Availability
 -- ----            ------------    ----          -----         ------------
  1 Runspace1       localhost       Local         Opened        Available
  2 Runspace2       localhost       Local         Opened        Busy
  4 Runspace4       localhost       Local         Opened        Busy
  5 Runspace5       localhost       Local         Opened        Busy
  6 Runspace6       localhost       Local         Opened        Busy
  8 Runspace8       localhost       Local         Opened        Busy
 10 Runspace10      localhost       Local         Opened        Busy
 11 Runspace11      localhost       Local         Opened        Busy
 12 Runspace12      localhost       Local         Opened        Available
 13 Runspace13      localhost       Local         Opened        Busy
 15 Runspace15      localhost       Local         Opened        Busy
 16 Runspace16      localhost       Local         Opened        Busy
 17 Runspace17      localhost       Local         Opened        Busy
 18 Runspace18      localhost       Local         Opened        Busy
 19 RemoteHost      localhost       Local         Opened        Busy
 25 Runspace25      localhost       Local         Opened        Available
 32 Runspace32      localhost       Local         Opened        Available
 37 Runspace37      localhost       Local         Opened        Available

If we had runspaces that were labeled for schedule tasks that may help troubleshooting, etc.

HeyItsGilbert commented 1 month ago

You beat me to it @mdaneri! lol

mdaneri commented 1 month ago

I think it's done. The only one that are not named are the user tasks and schedule

HeyItsGilbert commented 1 month ago

I was actually hoping those would be part of them, but the helper should suffice.

mdaneri commented 1 month ago

you can assign a name. I wrote this on the documentation

Customizing and Managing Runspace Names

Distinguishing Runspace Names in Pode

In Pode, internal runspaces are automatically assigned distinct names. This naming convention helps in identifying and managing these runspaces efficiently during debugging and monitoring. However, this is not the case for runspaces created by user tasks and schedules (excluding AsyncTask). These user-created runspaces typically have names in the Runspace<number> format, which can make it challenging to distinguish between different runspaces.

Customizing Runspace Names

To provide clarity and better manageability, you can set custom names for runspaces within your Pode tasks or schedules. This can be achieved using the Set-PodeCurrentRunspaceName cmdlet. By assigning meaningful names to your runspaces, you can easily identify and work with them, especially during debugging or performance monitoring.

Another useful cmdlet is Get-PodeCurrentRunspaceName, which retrieves the current runspace's name. This can be helpful if you need to log or display the runspace name dynamically.

Example

Here is an example demonstrating how to set a custom name for a runspace in a Pode task:

Add-PodeTask -Name 'Test2' -ScriptBlock {
    param($value)
    # Set a custom name for the current runspace
    Set-PodeCurrentRunspaceName -Name 'Test2'
    Start-Sleep -Seconds 10
    "A $($value) is never late, it arrives exactly when it means to" | Out-Default
}

In this example, the Set-PodeCurrentRunspaceName cmdlet is used to assign the name 'Test2' to the runspace executing the task. This makes it easier to identify the runspace in logs or during debugging sessions.