Class: NewRelic::Agent::PipeChannelManager::Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/pipe_channel_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeListener

Returns a new instance of Listener.



101
102
103
104
105
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 101

def initialize
  @pipes = {}
  @timeout = 360
  @select_timeout = 60
end

Instance Attribute Details

#pipesObject

Returns the value of attribute pipes.



99
100
101
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 99

def pipes
  @pipes
end

#select_timeoutObject

Returns the value of attribute select_timeout.



99
100
101
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 99

def select_timeout
  @select_timeout
end

#threadObject (readonly)

Returns the value of attribute thread.



98
99
100
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 98

def thread
  @thread
end

#timeoutObject

Returns the value of attribute timeout.



99
100
101
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 99

def timeout
  @timeout
end

Instance Method Details

#close_all_pipesObject



159
160
161
162
163
164
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 159

def close_all_pipes
  @pipes.each do |id, pipe|
    pipe.close if pipe
  end
  @pipes = {}
end

#register_pipe(id) ⇒ Object



111
112
113
114
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 111

def register_pipe(id)
  @pipes[id] = Pipe.new
  wakeup
end

#startObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 116

def start
  return if @started == true
  @started = true
  @thread = NewRelic::Agent::AgentThread.new('Pipe Channel Manager') do
    now = nil
    loop do
      clean_up_pipes
      pipes_to_listen_to = @pipes.values.map{|pipe| pipe.out} + [wake.out]

      NewRelic::Agent.record_metric('Supportability/Listeners',
        (Time.now - now).to_f) if now

      if ready = IO.select(pipes_to_listen_to, [], [], @select_timeout)
        now = Time.now

        ready_pipes = ready[0]
        ready_pipes.each do |pipe|
          merge_data_from_pipe(pipe) unless pipe == wake.out
        end

        wake.out.read(1) if ready_pipes.include?(wake.out)
      end

      break unless should_keep_listening?
    end
  end
  sleep 0.001 # give time for the thread to spawn
end

#started?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 170

def started?
  @started
end

#stopObject



151
152
153
154
155
156
157
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 151

def stop
  return unless @started == true
  stop_listener_thread
  close_all_pipes
  @wake.close
  @wake = nil
end

#stop_listener_threadObject



145
146
147
148
149
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 145

def stop_listener_thread
  @started = false
  wakeup
  @thread.join
end

#wakeObject



166
167
168
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 166

def wake
  @wake ||= Pipe.new
end

#wakeupObject



107
108
109
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 107

def wakeup
  wake.in << '.'
end