Class: Tasker::Orchestration::ConnectionPoolIntelligence

Inherits:
Object
  • Object
show all
Defined in:
lib/tasker/orchestration/connection_pool_intelligence.rb

Overview

Rails-Framework-Aligned Connection Management

Provides intelligent assessment of database connection pool health and recommends safe concurrency levels that work WITH Rails connection pool rather than around it.

Key Design Principles:

  • CONSTANTS: Ruby/Rails optimization characteristics (safety thresholds, precision)
  • CONFIGURABLE: Environment-dependent pressure response factors
  • Conservative safety margins to prevent connection exhaustion
  • Comprehensive structured logging for observability

Examples:

Basic usage

health = ConnectionPoolIntelligence.assess_connection_health
concurrency = ConnectionPoolIntelligence.intelligent_concurrency_for_step_executor

With custom configuration

config = Tasker::Configuration.configuration.execution
config.connection_pressure_factors = { high: 0.3, critical: 0.1 }
concurrency = ConnectionPoolIntelligence.intelligent_concurrency_for_step_executor

Constant Summary collapse

CONNECTION_UTILIZATION_PRECISION =

Decimal places for utilization calculation (based on Rails pool stat precision)

3
PRESSURE_ASSESSMENT_THRESHOLDS =

Pressure assessment thresholds (based on Rails connection pool characteristics)

{
  low: 0.0..0.5,
  moderate: 0.5..0.7,
  high: 0.7..0.85,
  critical: 0.85..Float::INFINITY
}.freeze
MAX_SAFE_CONNECTION_PERCENTAGE =

Conservative safety patterns (based on Rails connection pool behavior) Never use more than 60% of pool to prevent connection exhaustion

0.6
EMERGENCY_FALLBACK_CONCURRENCY =

Absolute minimum for system stability (based on Tasker orchestration needs)

3

Class Method Summary collapse

Class Method Details

.assess_connection_healthHash

Assess current database connection pool health

Provides comprehensive analysis of Rails connection pool state including utilization metrics, pressure assessment, and concurrency recommendations.

Returns:

  • (Hash)

    Connection health assessment with structured metrics @option return [Float] :pool_utilization Connection pool utilization ratio (0.0-1.0+) @option return [Symbol] :connection_pressure Pressure level (:low, :moderate, :high, :critical) @option return [Integer] :recommended_concurrency Safe concurrency level @option return [Hash] :rails_pool_stats Raw Rails pool statistics @option return [Symbol] :health_status Overall health status @option return [Time] :assessment_timestamp When assessment was performed



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/tasker/orchestration/connection_pool_intelligence.rb', line 59

def self.assess_connection_health
  pool = ActiveRecord::Base.connection_pool
  pool_stat = pool.stat

  {
    pool_utilization: calculate_utilization(pool_stat),
    connection_pressure: assess_pressure(pool_stat),
    recommended_concurrency: recommend_concurrency(pool_stat),
    rails_pool_stats: pool_stat,
    health_status: determine_health_status(pool_stat),
    assessment_timestamp: Time.current
  }
rescue StandardError => e
  Rails.logger.error("ConnectionPoolIntelligence: Health assessment failed - #{e.class.name}: #{e.message}")

  # Return safe fallback assessment
  {
    pool_utilization: 0.0,
    connection_pressure: :unknown,
    recommended_concurrency: EMERGENCY_FALLBACK_CONCURRENCY,
    rails_pool_stats: {},
    health_status: :unknown,
    assessment_timestamp: Time.current,
    assessment_error: e.message
  }
end

.intelligent_concurrency_for_step_executorInteger

Calculate intelligent concurrency for StepExecutor integration

Provides Rails-aware concurrency calculation that respects connection pool limits while applying configurable safety margins and pressure factors.

Returns:

  • (Integer)

    Safe concurrency level for step execution



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/tasker/orchestration/connection_pool_intelligence.rb', line 92

def self.intelligent_concurrency_for_step_executor
  health_data = assess_connection_health
  config = Tasker::Configuration.configuration.execution

  # Get base recommendation from Rails pool analysis
  base_recommendation = health_data[:recommended_concurrency]
  safe_concurrency = apply_tasker_safety_margins(base_recommendation, health_data, config)

  Rails.logger.debug do
    "ConnectionPoolIntelligence: Dynamic concurrency=#{safe_concurrency}, " \
      "pressure=#{health_data[:connection_pressure]}, " \
      "pool_size=#{ActiveRecord::Base.connection_pool.size}, " \
      "available=#{health_data[:rails_pool_stats][:available]}"
  end

  safe_concurrency
rescue StandardError => e
  Rails.logger.error(
    "ConnectionPoolIntelligence: Concurrency calculation failed - #{e.class.name}: #{e.message}, " \
    "using fallback=#{EMERGENCY_FALLBACK_CONCURRENCY}"
  )

  EMERGENCY_FALLBACK_CONCURRENCY
end