Class: Honeybadger::Worker

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

Overview

Internal: A concurrent queue to notify the backend.

Defined Under Namespace

Classes: Queue, Thread

Constant Summary collapse

SHUTDOWN =
:__hb_worker_shutdown!

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Worker

Returns a new instance of Worker.


32
33
34
35
36
37
38
39
40
# File 'lib/honeybadger/worker.rb', line 32

def initialize(config)
  @config = config
  @throttles = []
  @mutex = Mutex.new
  @marker = ConditionVariable.new
  @queue = Queue.new(config.max_queue_size)
  @shutdown = false
  @start_at = nil
end

Instance Method Details

#flushObject

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

Returns nothing.


97
98
99
100
101
102
103
104
# File 'lib/honeybadger/worker.rb', line 97

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

#push(msg) ⇒ Object


42
43
44
45
# File 'lib/honeybadger/worker.rb', line 42

def push(msg)
  return false unless start
  queue.push(msg)
end

#send_now(msg) ⇒ Object


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

def send_now(msg)
  handle_response(msg, notify_backend(msg))
end

#shutdownObject

Internal: Shutdown the worker after sending remaining data.

Returns true.


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

def shutdown
  d { 'shutting down worker' }

  mutex.synchronize do
    @shutdown = true
    @pid = nil
    queue.push(SHUTDOWN)
  end

  return true unless thread

  r = true
  unless Thread.current.eql?(thread)
    begin
      r = !!thread.join
    ensure
      shutdown! unless r
    end
  end

  r
end

#shutdown!Object


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/honeybadger/worker.rb', line 77

def shutdown!
  mutex.synchronize do
    @shutdown = true
    @pid = nil
    queue.clear
  end

  d { 'killing worker thread' }

  if thread
    Thread.kill(thread)
    thread.join # Allow ensure blocks to execute.
  end

  true
end

#startObject


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/honeybadger/worker.rb', line 106

def start
  return false unless can_start?

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

    return true if thread && thread.alive?

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

  true
end