Class: Honeybadger::Agent::Worker

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logging::Helper
Defined in:
lib/honeybadger/agent/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, feature) ⇒ Worker

Returns a new instance of Worker.


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/honeybadger/agent/worker.rb', line 34

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

Instance Method Details

#flushObject

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

Returns nothing.


95
96
97
98
99
100
101
102
# File 'lib/honeybadger/agent/worker.rb', line 95

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

#push(obj) ⇒ Object


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

def push(obj)
  return false unless start
  queue.push(obj)
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
# File 'lib/honeybadger/agent/worker.rb', line 54

def shutdown
  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


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

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

  d { sprintf('killing worker thread feature=%s', feature) }

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

  true
end

#startObject


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

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