Class: Async::Background::Web::MetricsReader

Inherits:
Object
  • Object
show all
Includes:
Clock
Defined in:
lib/async/background/web/metrics_reader.rb

Constant Summary collapse

DEFAULT_TTL =
1.0
EMPTY_WORKERS =
[].freeze
SUMMED_FIELDS =
%i[
  total_runs
  total_successes
  total_failures
  total_timeouts
  total_skips
  active_jobs
].freeze
LATEST_FIELD =
:last_run_at
LATEST_COMPANION =
:last_duration_ms
EMPTY_TOTALS =
(
  SUMMED_FIELDS.to_h { |field| [field, 0] }
    .merge(LATEST_FIELD => 0, LATEST_COMPANION => nil)
).freeze
UNAVAILABLE =
{available: false, workers: EMPTY_WORKERS, totals: EMPTY_TOTALS}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(path:, total_workers:, ttl: DEFAULT_TTL) ⇒ MetricsReader

Returns a new instance of MetricsReader.



33
34
35
36
37
38
39
40
# File 'lib/async/background/web/metrics_reader.rb', line 33

def initialize(path:, total_workers:, ttl: DEFAULT_TTL)
  @path = path
  @total_workers = total_workers
  @ttl = ttl
  @mutex = Mutex.new
  @cache = nil
  @cached_at = nil
end

Instance Method Details

#aggregatedObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/async/background/web/metrics_reader.rb', line 42

def aggregated
  @mutex.synchronize do
    now = monotonic_now
    return @cache if cache_current?(now)

    @cache = read_metrics.freeze
    @cached_at = now
    @cache
  end
end