Class: Honeybadger::Worker Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logging::Helper
Defined in:
lib/honeybadger/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 notify the backend.

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!
BASE_THROTTLE =

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.

The base number for the exponential backoff formula when calculating the throttle interval. ‘1.05 ** throttle` will reach an interval of 2 minutes after around 100 429 responses from the server.

1.05

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Worker

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 Worker.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/honeybadger/worker.rb', line 25

def initialize(config)
  @config = config
  @throttle = 0
  @throttle_interval = 0
  @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.



77
78
79
80
81
82
83
84
# File 'lib/honeybadger/worker.rb', line 77

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.



37
38
39
40
41
42
43
44
45
46
# File 'lib/honeybadger/worker.rb', line 37

def push(msg)
  return false unless start

  if queue.size >= config.max_queue_size
    warn { sprintf('Unable to report error; reached max queue size of %s. id=%s', queue.size, msg.id) }
    return false
  end

  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.



48
49
50
# File 'lib/honeybadger/worker.rb', line 48

def send_now(msg)
  handle_response(msg, notify_backend(msg))
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.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/honeybadger/worker.rb', line 52

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

  mutex.synchronize do
    @shutdown = true
  end

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

  if throttled?
    warn { sprintf('Unable to report %s error(s) to Honeybadger (currently throttled)', queue.size) } unless queue.empty?
    return true
  end

  info { sprintf('Waiting to report %s error(s) to Honeybadger', queue.size) } unless queue.empty?

  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.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/honeybadger/worker.rb', line 86

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