Class: NewRelic::Agent::WorkerLoop
- 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
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
Instance Method Summary collapse
-
#add_task(call_period, desc = "", &task_proc) ⇒ Object
add a task to the worker loop.
-
#initialize(log = Logger.new(STDERR)) ⇒ WorkerLoop
constructor
A new instance of WorkerLoop.
- #keep_running ⇒ Object
-
#run ⇒ Object
Run infinitely, calling the registered tasks at their specified call periods.
- #stop ⇒ Object
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
#log ⇒ Object (readonly)
Returns the value of attribute log.
7 8 9 |
# File 'lib/new_relic/agent/worker_loop.rb', line 7 def log @log end |
#pid ⇒ Object (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_running ⇒ Object
26 27 28 |
# File 'lib/new_relic/agent/worker_loop.rb', line 26 def keep_running @should_run && (@pid == $$) end |
#run ⇒ 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
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 |
#stop ⇒ Object
30 31 32 |
# File 'lib/new_relic/agent/worker_loop.rb', line 30 def stop @should_run = false end |