Class: MysqlFramework::Stats::AwsMetricPublisher
- Inherits:
-
Object
- Object
- MysqlFramework::Stats::AwsMetricPublisher
- Defined in:
- lib/mysql_framework/stats/aws_metric_publisher.rb
Constant Summary collapse
- THREAD_NAME =
'mysql-connector-pool-stats'- JOIN_TIMEOUT =
seconds to wait for clean thread exit before force-killing
5- METRIC_UNIT =
'Count'- METRIC_NAME_MAP =
{ size: 'MysqlConnectionPoolSize', available: 'MysqlConnectionPoolAvailable', idle: 'MysqlConnectionPoolIdle' }.freeze
Instance Method Summary collapse
-
#initialize(connector: nil, dimension_map: nil, cloudwatch_client: nil, publish_interval: 300) ⇒ void
constructor
Initializes AWS metric publishing dependencies.
-
#running? ⇒ Boolean
Returns true when the reporter thread is alive.
-
#start ⇒ Thread?
Spawns the background sampling thread.
-
#stop ⇒ void
Cooperatively stops the background thread and waits up to JOIN_TIMEOUT seconds for it to exit before force-killing it.
Constructor Details
#initialize(connector: nil, dimension_map: nil, cloudwatch_client: nil, publish_interval: 300) ⇒ void
Initializes AWS metric publishing dependencies.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/mysql_framework/stats/aws_metric_publisher.rb', line 25 def initialize( connector: nil, dimension_map: nil, cloudwatch_client: nil, publish_interval: 300 ) @thread = nil @connector = connector @cloudwatch_client = cloudwatch_client @dimension_map = dimension_map || MysqlFramework::Stats::DimensionMap.new @publish_interval = publish_interval end |
Instance Method Details
#running? ⇒ Boolean
Returns true when the reporter thread is alive.
74 75 76 |
# File 'lib/mysql_framework/stats/aws_metric_publisher.rb', line 74 def running? @thread&.alive? || false end |
#start ⇒ Thread?
Spawns the background sampling thread. Safe to call more than once –subsequent calls are no-ops while the thread is already running.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mysql_framework/stats/aws_metric_publisher.rb', line 42 def start return if running? thread_name = "#{THREAD_NAME}-#{object_id}" thread = Thread.new do Thread.current.name = thread_name loop do sleep @publish_interval break unless Thread.current == @thread sample end end thread.abort_on_exception = false @thread = thread end |
#stop ⇒ void
This method returns an undefined value.
Cooperatively stops the background thread and waits up to JOIN_TIMEOUT seconds for it to exit before force-killing it.
64 65 66 67 68 69 |
# File 'lib/mysql_framework/stats/aws_metric_publisher.rb', line 64 def stop thread = @thread @thread = nil # cooperative stop signal: loop checks this after each sleep thread&.join(JOIN_TIMEOUT) thread&.kill # force-kill only if still alive after timeout end |