Class: TjScaleRuby::JobMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/tj_scale_ruby/models/tj_scale_ruby.rb

Overview

Posts metrics to your TJ Scale (or compatible) ingest URL. Scaling decisions happen on the server.

Class Method Summary collapse

Class Method Details

.backendObject

Active queue backend (Delayed Job or Sidekiq) per TJ_SCALE_JOB_BACKEND.



14
15
16
# File 'lib/tj_scale_ruby/models/tj_scale_ruby.rb', line 14

def backend
  JobBackends.current
end

.oldest_pending_queue_seconds(now = Time.zone.now) ⇒ Object

Age in seconds of the oldest waiting job (DJ: run_at vs now; Sidekiq: queue latency).



33
34
35
36
37
38
# File 'lib/tj_scale_ruby/models/tj_scale_ruby.rb', line 33

def oldest_pending_queue_seconds(now = Time.zone.now)
  backend.oldest_pending_queue_seconds(now)
rescue StandardError => e
  Rails.logger.error("TjScaleRuby: Error computing queue_time_s: #{e.message}")
  0
end

.pending_job_count(now = Time.zone.now) ⇒ Object

Counts jobs waiting to run on the active backend (DJ: runnable rows with priority filters; Sidekiq: enqueued jobs across monitored queues).



25
26
27
28
29
30
# File 'lib/tj_scale_ruby/models/tj_scale_ruby.rb', line 25

def pending_job_count(now = Time.zone.now)
  backend.pending_job_count(now)
rescue StandardError => e
  Rails.logger.error("TjScaleRuby: Error counting pending jobs: #{e.message}")
  0
end

.pending_jobs_relation(now = Time.zone.now) ⇒ Object

Relation for runnable Delayed::Job rows (Delayed Job backend only; kept for manual use).



19
20
21
# File 'lib/tj_scale_ruby/models/tj_scale_ruby.rb', line 19

def pending_jobs_relation(now = Time.zone.now)
  JobBackends::DelayedJob.pending_jobs_relation(now)
end

.send_logObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tj_scale_ruby/models/tj_scale_ruby.rb', line 40

def send_log
  token = ENV["TJ_SCALE_API_TOKEN"]
  unless token
    Rails.logger.warn("TjScaleRuby: TJ_SCALE_API_TOKEN not configured")
    return false
  end

  api_url = ENV["TJ_SCALE_API_URL"].to_s.strip
  if api_url.empty?
    Rails.logger.warn("TjScaleRuby: TJ_SCALE_API_URL not configured")
    return false
  end

  now = Time.zone.now
  cfg = TjScaleRuby.configuration
  payload = build_log_payload(now)

  response = http_post(URI.parse(api_url), body: payload.compact.to_json, token: token)

  if response.is_a?(Net::HTTPSuccess)
    if cfg.monitor_process == "web"
      Rails.logger.debug do
        "TjScaleRuby: Sent web metrics queue_time_ms=#{payload[:queue_time_ms].inspect}"
      end
    else
      Rails.logger.debug do
        "TjScaleRuby: Sent worker metrics job_count=#{payload[:job_count]} " \
          "queue_time_s=#{payload[:queue_time_s]}"
      end
    end
  else
    Rails.logger.warn("TjScaleRuby: API returned #{response.code}: #{response.message}")
  end

  response
rescue StandardError => e
  Rails.logger.error("TjScaleRuby: Error sending log: #{e.message}")
  false
end