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.

Defined Under Namespace

Classes: LoopTask

Constant Summary collapse

MIN_CALL_PERIOD =
0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log = Logger.new(STDERR)) ⇒ WorkerLoop

Returns a new instance of WorkerLoop.



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

def initialize(log = Logger.new(STDERR))
  @tasks = []
  @log = log
  @should_run = true
  @pid = $$
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



7
8
9
# File 'lib/new_relic/agent/worker_loop.rb', line 7

def log
  @log
end

#pidObject (readonly)

Returns the value of attribute pid.



8
9
10
# File 'lib/new_relic/agent/worker_loop.rb', line 8

def pid
  @pid
end

Instance Method Details

#add_task(call_period, desc = "", &task_proc) ⇒ Object

add a task to the worker loop. The task will be called approximately once every call_period seconds. The task is passed as a block



38
39
40
41
42
43
# File 'lib/new_relic/agent/worker_loop.rb', line 38

def add_task(call_period, desc="", &task_proc)
  if call_period < MIN_CALL_PERIOD
    raise ArgumentError, "Invalid Call Period (must be > #{MIN_CALL_PERIOD}): #{call_period}" 
  end
  @tasks << LoopTask.new(call_period, desc, &task_proc)
end

#keep_runningObject



26
27
28
# File 'lib/new_relic/agent/worker_loop.rb', line 26

def keep_running
  @should_run && (@pid == $$)
end

#runObject

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



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

def run
  while keep_running do
    run_next_task
  end
end

#stopObject



30
31
32
# File 'lib/new_relic/agent/worker_loop.rb', line 30

def stop
  @should_run = false
end