feat: Add Graceful Shutdown Handling to ProcessWorker#143
Merged
Conversation
- Add ConcurrentDictionary to track active messages - Expose IsShuttingDown and ActiveMessageCount properties - Implement wait-for-completion logic with 30s timeout - Reject new messages during shutdown - Use fresh CancellationToken for error recording - Add comprehensive shutdown logging - Record cancellation for interrupted processes Related to #100
- Report Healthy during normal operation - Report Degraded when shutting down - Report Degraded when high number of active messages (>100) - Include activeMessages in response data Related to #100
- Set host shutdown timeout to 45 seconds - Add health checks with ProcessWorkerHealthCheck - Register ProcessWorker as singleton for health check injection Related to #100
- Test IsShuttingDown initial state - Test ActiveMessageCount initial state - Test shutdown properties exposure Related to #100
- Test Healthy status during normal operation - Test Degraded status when shutting down - Test Degraded status with high message count - Verify activeMessages data in response Related to #100
- Document graceful shutdown behavior - Explain two-timeout strategy - Provide testing instructions - Include Kubernetes integration example - Document fresh CancellationToken pattern Related to #100
- Use Services.Configure<HostOptions> instead of builder.Host - HostApplicationBuilder doesn't expose Host property - Maintain 45s shutdown timeout configuration Related to #100
- Remove BeDefined() which doesn't exist in FluentAssertions - Properties are always defined in C#, no need to test existence - Keep meaningful assertions on property values Related to #100
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📋 Description
Implements comprehensive graceful shutdown handling in ProcessWorker to ensure clean termination without message loss, proper resource cleanup, and coordinated shutdown with the host application.
This PR addresses issue #100 and implements Phase 7.1 requirements for graceful shutdown in the Process Engine.
🎯 Changes
Core Implementation
ConcurrentDictionary<string, Task>to track active messagesIsShuttingDownandActiveMessageCountpublic propertiesCancellationTokenfor error recording operationsConfiguration
Health Check
ProcessWorkerHealthCheckfor monitoringHealthyduring normal operationDegradedwhen shutting down or high message count (>100)activeMessagescount in response dataTesting
ProcessWorkerShutdownTestsfor shutdown scenariosProcessWorkerHealthCheckTestsfor health check validationDocumentation
GRACEFUL-SHUTDOWN.mdguide🔗 Related Issues
Closes #100
📝 Type of Change
✅ Testing Performed
Unit Tests
Build Verification
✅ dotnet build - Success ✅ dotnet test - All tests passingCode Quality
📚 Documentation
docs/GRACEFUL-SHUTDOWN.md🎨 Implementation Highlights
Two-Timeout Strategy
Worker Timeout (30s):
Host Timeout (45s):
Message Requeue Strategy
Messages cancelled during shutdown are:
PROCESS_CANCELLEDwithretryable: trueBenefits: Zero message loss, eventual consistency, clear audit trail
Fresh CancellationToken Pattern
During shutdown, critical operations (error recording) use a fresh
CancellationTokenSourcewith short timeout (5s) to ensure completion even when main token is cancelled.🚀 Deployment Notes
Kubernetes Integration
The health check enables clean pod termination:
Degradedduring shutdownMonitoring Recommendations
Key Metrics:
📋 Checklist
🔍 Review Focus Areas
📊 Impact Assessment
Risk Level: Low
Performance Impact: Negligible
🎓 References
Ready for review ✅