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

a simple accessor for @should_run



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

def keep_running
  @should_run
end

#lockObject

returns a class-level memoized mutex to make sure we don’t run overlapping



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

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

#logObject

a helper to access the NewRelic::Control.instance.log



22
23
24
# File 'lib/new_relic/agent/worker_loop.rb', line 22

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.



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

def run(period=nil, &block)
  @period = period if period
  @next_invocation_time = (Time.now + @period)
  @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

Executes the block given to the worker loop, and handles many possible errors. Also updates the execution time so that the next run occurs on schedule, even if we execute at some odd time



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/new_relic/agent/worker_loop.rb', line 58

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

Sets @should_run to false. Returns false



51
52
53
# File 'lib/new_relic/agent/worker_loop.rb', line 51

def stop
  @should_run = false
end