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.



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

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

Instance Attribute Details

#pipesObject

Returns the value of attribute pipes.



95
96
97
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 95

def pipes
  @pipes
end

#select_timeoutObject

Returns the value of attribute select_timeout.



95
96
97
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 95

def select_timeout
  @select_timeout
end

#threadObject (readonly)

Returns the value of attribute thread.



94
95
96
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 94

def thread
  @thread
end

#timeoutObject

Returns the value of attribute timeout.



95
96
97
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 95

def timeout
  @timeout
end

Instance Method Details

#close_all_pipesObject



154
155
156
157
158
159
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 154

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

#register_pipe(id) ⇒ Object



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

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

#startObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 112

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
        pipe = ready[0][0]
        if pipe == wake.out
          pipe.read(1)
        else
          merge_data_from_pipe(pipe)
        end
      end

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

#started?Boolean

Returns:

  • (Boolean)


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

def started?
  @started
end

#stopObject



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

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

#stop_listener_threadObject



140
141
142
143
144
# File 'lib/new_relic/agent/pipe_channel_manager.rb', line 140

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

#wakeObject



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

def wake
  @wake ||= Pipe.new
end

#wakeupObject



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

def wakeup
  wake.in << '.'
end