Class: Honeybadger::MetricsWorker Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logging::Helper
Defined in:
lib/honeybadger/metrics_worker.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A concurrent queue to execute plugin collect blocks and registry.

Defined Under Namespace

Classes: Thread

Constant Summary collapse

SHUTDOWN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Used to signal the worker to shutdown.

:__hb_worker_shutdown!

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ MetricsWorker

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of MetricsWorker.



17
18
19
20
21
22
23
24
25
26
# File 'lib/honeybadger/metrics_worker.rb', line 17

def initialize(config)
  @config = config
  @interval_seconds = 1
  @mutex = Mutex.new
  @marker = ConditionVariable.new
  @queue = Queue.new
  @shutdown = false
  @start_at = nil
  @pid = Process.pid
end

Instance Method Details

#flushObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Blocks until queue is processed up to this point in time.



60
61
62
63
64
65
66
67
# File 'lib/honeybadger/metrics_worker.rb', line 60

def flush
  mutex.synchronize do
    if thread && thread.alive?
      queue.push(marker)
      marker.wait(mutex)
    end
  end
end

#push(msg) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



28
29
30
31
32
33
# File 'lib/honeybadger/metrics_worker.rb', line 28

def push(msg)
  return false unless config.insights_enabled?
  return false unless start

  queue.push(msg)
end

#send_now(msg) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



35
36
37
38
39
40
# File 'lib/honeybadger/metrics_worker.rb', line 35

def send_now(msg)
  return if msg.tick > 0

  msg.call
  msg.reset
end

#shutdown(force = false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/honeybadger/metrics_worker.rb', line 42

def shutdown(force = false)
  d { 'shutting down worker' }

  mutex.synchronize do
    @shutdown = true
  end

  return true if force
  return true unless thread&.alive?

  queue.push(SHUTDOWN)
  !!thread.join
ensure
  queue.clear
  kill!
end

#startObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/honeybadger/metrics_worker.rb', line 69

def start
  return false unless can_start?

  mutex.synchronize do
    @shutdown = false
    @start_at = nil

    return true if thread&.alive?

    @pid = Process.pid
    @thread = Thread.new { run }
  end

  true
end