Class: NewRelic::Agent::WorkerLoop

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

Overview

A worker loop executes a set of registered tasks on a single thread. A task is a proc or block with a specified call period in seconds.

Instance Method Summary collapse

Constructor Details

#initializeWorkerLoop

Returns a new instance of WorkerLoop.



9
10
11
12
13
14
# File 'lib/new_relic/agent/worker_loop.rb', line 9

def initialize
  @log = log
  @should_run = true
  @next_invocation_time = Time.now
  @period = 60.0
end

Instance Method Details

#keep_runningObject



42
43
44
# File 'lib/new_relic/agent/worker_loop.rb', line 42

def keep_running
  @should_run
end

#lockObject



16
17
18
# File 'lib/new_relic/agent/worker_loop.rb', line 16

def lock
  @@lock ||= Mutex.new
end

#logObject



20
21
22
# File 'lib/new_relic/agent/worker_loop.rb', line 20

def log
  NewRelic::Control.instance.log
end

#run(period = nil, &block) ⇒ Object

Run infinitely, calling the registered tasks at their specified call periods. The caller is responsible for creating the thread that runs this worker loop. This will run the task immediately.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/new_relic/agent/worker_loop.rb', line 26

def run(period=nil, &block)
  @period = period if period
  @next_invocation_time = Time.now
  @task = block
  while keep_running do
    now = Time.now
    while now < @next_invocation_time
      # sleep until this next task's scheduled invocation time
      sleep_time = @next_invocation_time - now
      sleep sleep_time if sleep_time > 0
      now = Time.now
    end
    run_task if keep_running
  end
end

#run_taskObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/new_relic/agent/worker_loop.rb', line 50

def run_task
  begin
    lock.synchronize do
      @task.call
    end
  rescue ServerError => e
    log.debug "Server Error: #{e}"
  rescue NewRelic::Agent::ForceRestartException, NewRelic::Agent::ForceDisconnectException
    # blow out the loop
    raise
  rescue RuntimeError => e
    # This is probably a server error which has been logged in the server along
    # with your account name.
    log.error "Error running task in worker loop, likely a server error (#{e})"
    log.debug e.backtrace.join("\n")
  rescue Timeout::Error, NewRelic::Agent::ServerConnectionException
    # Want to ignore these because they are handled already
  rescue SystemExit, NoMemoryError, SignalException
    raise
  rescue Exception => e
    # Don't blow out the stack for anything that hasn't already propagated
    log.error "Error running task in Agent Worker Loop '#{e}': #{e.backtrace.first}"
    log.debug e.backtrace.join("\n")
  end
  now = Time.now
  while @next_invocation_time <= now && @period > 0
    @next_invocation_time += @period
  end
end

#stopObject



46
47
48
# File 'lib/new_relic/agent/worker_loop.rb', line 46

def stop
  @should_run = false
end