Module: NatsWork::WorkerHealth

Defined in:
lib/natswork/health_check.rb

Overview

Worker-specific health checks

Class Method Summary collapse

Class Method Details

.setup_worker_checks(worker, health_checker = HealthChecker.global) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/natswork/health_check.rb', line 200

def self.setup_worker_checks(worker, health_checker = HealthChecker.global)
  # Worker status check
  health_checker.add_check('worker_status') do
    if worker.running?
      if worker.paused?
        { status: :degraded, message: 'Worker is paused' }
      elsif worker.stopping?
        { status: :degraded, message: 'Worker is stopping' }
      else
        { status: :healthy, message: 'Worker running normally' }
      end
    else
      { status: :unhealthy, message: 'Worker is stopped' }
    end
  end

  # Active jobs check
  health_checker.add_check('active_jobs') do
    stats = worker.stats
    active = stats[:active_jobs]
    concurrency = stats[:concurrency]

    if active >= concurrency
      { status: :degraded, message: "All #{concurrency} job slots occupied" }
    elsif active > concurrency * 0.8
      { status: :degraded, message: "#{active}/#{concurrency} job slots occupied" }
    else
      { status: :healthy, message: "#{active}/#{concurrency} job slots occupied" }
    end
  end

  # Job processing rate check
  health_checker.add_check('job_processing') do
    stats = worker.stats
    processed = stats[:jobs_processed]
    failed = stats[:jobs_failed]

    if processed.zero?
      { status: :unknown, message: 'No jobs processed yet' }
    else
      error_rate = failed.to_f / processed
      if error_rate > 0.5
        { status: :unhealthy, message: "High error rate: #{(error_rate * 100).round(1)}%" }
      elsif error_rate > 0.1
        { status: :degraded, message: "Elevated error rate: #{(error_rate * 100).round(1)}%" }
      else
        { status: :healthy, message: "Error rate: #{(error_rate * 100).round(1)}%" }
      end
    end
  end
end