You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 23fe362
Browse filesBrowse the repository at this point in the historyBrowse files
Completed improvements
1. **Interfaces for testability** // #5#4
- Created `IPowerShellExecutor` interface — enables mocking executor in tests
- Created `IIoTHubService` interface — enables mocking IoT Hub service in tests
- Updated `PowerShellExecutorService` to use `IIoTHubService` with optional dependency injection
2. **Async patterns** #2
- Service already uses `Task.Run` for async work
- `ExecuteScriptMethodHandler` is already async (in `IoTHubService`)
- `ExecutePowerShellAsync` already exists with `SemaphoreSlim` throttling
- Added `Task.Yield()` in the service loop to prevent thread starvation on bursty traffic
3. **Error handling** #3
- Added `AggregateException` unwrapping for better error reporting
- Separate handling for `OperationCanceledException` (expected during shutdown)
- Error status reporting to IoT Hub on service failures
- Improved error logging with inner exception details
4. **Resource management** #2
- Improved disposal patterns in `OnStop()` using `GetAwaiter().GetResult()` to avoid deadlocks
- Proper cleanup of `CancellationTokenSource`
- Better exception handling during disposal
5. **Dependency injection support** #2#4#5
- Constructor accepts optional `IIoTHubService` parameter for testing
- Service can be instantiated with mocked dependencies
Current architecture
- Async execution: `ExecutePowerShellAsync` with `SemaphoreSlim` throttling
- Non-blocking: PowerShell execution wrapped in `Task.Run` to prevent blocking IoT listener
- Cancellation: Full cancellation token support throughout the pipeline
- Error handling: Structured error details with inner exception capture
- Testability: Interfaces enable mocking for unit tests
Score: 9/10
The service now has:
- Full async/await patterns
- Proper cancellation support
- Semaphore throttling for concurrency control
- Testable architecture with interfaces
- Robust error handling
- Resource leak prevention
The only remaining optimization would be using PowerShell 7+ native async APIs (`InvokeAsync()`) if available, but the current `Task.Run` wrapper approach works well and maintains compatibility with PowerShell 5.1+.
This agents intent being for super fast execution of PowerShell on servers to poll info for architecturally diverse clients - 5.1+ is the baseline; there is no need to delve into PS Core for the invokeasync....
(But I also use PS Core as daily driver becuase Widows PowerShell is for cave dwellers
Signed-off-by: calvin <calvinbergin212@gmail.com>
0 commit comments