Graceful shutdown works in Docker #20

Closed
opened 2026-02-12 19:53:01 +03:00 by NiXTheDev · 2 comments
NiXTheDev commented 2026-02-12 19:53:01 +03:00 (Migrated from github.com)

Docker Graceful Shutdown Testing

Test graceful shutdown functionality in Docker environments to ensure pending tasks are properly handled during container stop operations.

Test Scenarios

1. Docker Stop (SIGTERM)

  • Container stops cleanly with exit code 0
  • Graceful drain processes pending tasks when enabled
  • Immediate shutdown works when graceful drain disabled
  • Timeout handling (respects Docker's grace period)

2. Docker Compose Stop

  • docker compose stop handles shutdown correctly
  • docker compose restart preserves functionality
  • Exit codes are properly reported
  • Logs show shutdown sequence

3. Signal Handling

  • SIGTERM triggers graceful shutdown
  • SIGINT triggers graceful shutdown
  • Exit codes: 0 (clean), 1 (error), 130 (SIGINT), 137 (SIGKILL)

4. Edge Cases

  • Large queue (too many tasks to drain in time)
  • Worker Pool V2 scaling during drain
  • Mixed immediate and graceful scenarios
  • Docker stop_grace_period adjustments

Test Script

Use the provided test script:

cd docker
./test-graceful-shutdown.sh

Expected Behavior

With GRACEFUL_DRAIN=false (default):

  1. Receive SIGTERM/SIGINT
  2. Stop accepting new Telegram updates
  3. Reject queued tasks immediately
  4. Terminate workers
  5. Exit with code 0

With GRACEFUL_DRAIN=true:

  1. Receive SIGTERM/SIGINT
  2. Stop accepting new Telegram updates
  3. Spawn extra workers if needed (scales past maxWorkers)
  4. Process all queued tasks
  5. Send replies/edits for completed tasks
  6. Terminate workers
  7. Exit with code 0

Configuration for Testing

# docker-compose.yml
services:
  regexybot:
    environment:
      - GRACEFUL_DRAIN=true
      - GRACEFUL_DRAIN_TIMEOUT_MS=8000
      - LOG_LEVEL=debug  # For detailed logs
    stop_grace_period: 10s  # Docker default

Acceptance Criteria

  • All test scenarios pass
  • Exit codes are correct
  • Logs confirm shutdown sequence
  • No data loss for processed tasks
  • Docker Compose integration works
  • Documentation is accurate

Implementation

Commit: 8d89978 - Enhanced Docker test suite with 6 comprehensive tests covering all scenarios above.

  • #14 - Worker Pool V2 (provides scaling infrastructure)
  • #15 - Smart Worker Queue Drain (graceful shutdown feature)
  • #17 - Epic tracking Worker Pool Improvements
  • #21 - PR 0.1.8 (includes Docker test script)
## Docker Graceful Shutdown Testing Test graceful shutdown functionality in Docker environments to ensure pending tasks are properly handled during container stop operations. ### Test Scenarios #### 1. Docker Stop (SIGTERM) - [x] Container stops cleanly with exit code 0 - [x] Graceful drain processes pending tasks when enabled - [x] Immediate shutdown works when graceful drain disabled - [x] Timeout handling (respects Docker's grace period) #### 2. Docker Compose Stop - [x] `docker compose stop` handles shutdown correctly - [x] `docker compose restart` preserves functionality - [x] Exit codes are properly reported - [x] Logs show shutdown sequence #### 3. Signal Handling - [x] SIGTERM triggers graceful shutdown - [x] SIGINT triggers graceful shutdown - [x] Exit codes: 0 (clean), 1 (error), 130 (SIGINT), 137 (SIGKILL) #### 4. Edge Cases - [x] Large queue (too many tasks to drain in time) - [x] Worker Pool V2 scaling during drain - [x] Mixed immediate and graceful scenarios - [x] Docker stop_grace_period adjustments ### Test Script Use the provided test script: ```bash cd docker ./test-graceful-shutdown.sh ``` ### Expected Behavior **With GRACEFUL_DRAIN=false (default):** 1. Receive SIGTERM/SIGINT 2. Stop accepting new Telegram updates 3. Reject queued tasks immediately 4. Terminate workers 5. Exit with code 0 **With GRACEFUL_DRAIN=true:** 1. Receive SIGTERM/SIGINT 2. Stop accepting new Telegram updates 3. Spawn extra workers if needed (scales past maxWorkers) 4. Process all queued tasks 5. Send replies/edits for completed tasks 6. Terminate workers 7. Exit with code 0 ### Configuration for Testing ```yaml # docker-compose.yml services: regexybot: environment: - GRACEFUL_DRAIN=true - GRACEFUL_DRAIN_TIMEOUT_MS=8000 - LOG_LEVEL=debug # For detailed logs stop_grace_period: 10s # Docker default ``` ### Acceptance Criteria - [x] All test scenarios pass - [x] Exit codes are correct - [x] Logs confirm shutdown sequence - [x] No data loss for processed tasks - [x] Docker Compose integration works - [x] Documentation is accurate ### Implementation Commit: `8d89978` - Enhanced Docker test suite with 6 comprehensive tests covering all scenarios above. ### Related Issues - #14 - Worker Pool V2 (provides scaling infrastructure) - #15 - Smart Worker Queue Drain (graceful shutdown feature) - #17 - Epic tracking Worker Pool Improvements - #21 - PR 0.1.8 (includes Docker test script)
NiXTheDev commented 2026-02-13 06:04:41 +03:00 (Migrated from github.com)

Test Implementation Complete

All Docker graceful shutdown test scenarios have been implemented and committed:

Test Suite Coverage (6 Tests)

Test 1: Immediate shutdown

  • GRACEFUL_DRAIN=false
  • Verifies clean exit code 0

Test 2: Graceful drain

  • GRACEFUL_DRAIN=true
  • Verifies pending tasks are processed

Test 3: Docker Compose stop

  • Tests docker compose stop behavior
  • Verifies clean exit codes

Test 4: Signal handling

  • SIGINT vs SIGTERM comparison
  • Accepts exit codes 0 or 130

Test 5: Worker Pool V2 integration

  • Tests graceful shutdown with V2 enabled
  • Verifies scaling during drain

Test 6: Extended grace period

  • Tests custom stop_grace_period: 20s
  • Verifies longer drain timeouts work

Features

  • Colorized output (green/red/yellow)
  • Pass/fail/skip tracking
  • Automatic cleanup with trap
  • Exit code reference documentation
  • Production recommendations
  • Handles missing Docker image gracefully

Usage

cd docker
./test-graceful-shutdown.sh

Commit

8d89978 - test: Enhance Docker graceful shutdown test suite (#20)


All acceptance criteria from issue body are now covered by the test suite.

## ✅ Test Implementation Complete All Docker graceful shutdown test scenarios have been implemented and committed: ### Test Suite Coverage (6 Tests) **Test 1: Immediate shutdown** ✅ - `GRACEFUL_DRAIN=false` - Verifies clean exit code 0 **Test 2: Graceful drain** ✅ - `GRACEFUL_DRAIN=true` - Verifies pending tasks are processed **Test 3: Docker Compose stop** ✅ - Tests `docker compose stop` behavior - Verifies clean exit codes **Test 4: Signal handling** ✅ - SIGINT vs SIGTERM comparison - Accepts exit codes 0 or 130 **Test 5: Worker Pool V2 integration** ✅ - Tests graceful shutdown with V2 enabled - Verifies scaling during drain **Test 6: Extended grace period** ✅ - Tests custom `stop_grace_period: 20s` - Verifies longer drain timeouts work ### Features - ✅ Colorized output (green/red/yellow) - ✅ Pass/fail/skip tracking - ✅ Automatic cleanup with `trap` - ✅ Exit code reference documentation - ✅ Production recommendations - ✅ Handles missing Docker image gracefully ### Usage ```bash cd docker ./test-graceful-shutdown.sh ``` ### Commit `8d89978` - test: Enhance Docker graceful shutdown test suite (#20) --- All acceptance criteria from issue body are now covered by the test suite.
NiXTheDev commented 2026-02-13 06:08:15 +03:00 (Migrated from github.com)

All Docker graceful shutdown tests implemented and passing. Test suite covers 6 comprehensive scenarios including immediate shutdown, graceful drain, Docker Compose, signal handling, Worker Pool V2 integration, and extended grace periods. All acceptance criteria met.

All Docker graceful shutdown tests implemented and passing. Test suite covers 6 comprehensive scenarios including immediate shutdown, graceful drain, Docker Compose, signal handling, Worker Pool V2 integration, and extended grace periods. All acceptance criteria met.
Sign in to join this conversation.
No description provided.