Class: OneApm::Support::WorkerLoop

Inherits:
Object
  • Object
show all
Defined in:
lib/one_apm/support/event/worker_loop.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ WorkerLoop

Returns a new instance of WorkerLoop.



13
14
15
16
17
18
19
20
21
# File 'lib/one_apm/support/event/worker_loop.rb', line 13

def initialize(opts = {})
  @should_run = true
  @next_invocation_time = Time.now
  @period = 60.0
  @duration = opts[:duration] if opts[:duration]
  @limit = opts[:limit] if opts[:limit]
  @iterations = 0
  @propagate_errors = opts.fetch(:propagate_errors, false)
end

Instance Attribute Details

#iterationsObject (readonly)

Returns the value of attribute iterations.



11
12
13
# File 'lib/one_apm/support/event/worker_loop.rb', line 11

def iterations
  @iterations
end

#periodObject

Returns the value of attribute period.



10
11
12
# File 'lib/one_apm/support/event/worker_loop.rb', line 10

def period
  @period
end

#propagate_errorsObject

Returns the value of attribute propagate_errors.



10
11
12
# File 'lib/one_apm/support/event/worker_loop.rb', line 10

def propagate_errors
  @propagate_errors
end

Instance Method Details

#keep_running?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/one_apm/support/event/worker_loop.rb', line 52

def keep_running?
  @should_run && under_duration? && under_limit?
end

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



34
35
36
37
38
39
40
41
42
# File 'lib/one_apm/support/event/worker_loop.rb', line 34

def run(period = nil, &block)
  setup(period, block)
  while keep_running? do
    sleep_time = schedule_next_invocation
    sleep(sleep_time) if sleep_time > 0
    run_task if keep_running?
    @iterations += 1
  end
end

#run_taskObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/one_apm/support/event/worker_loop.rb', line 68

def run_task
  if @propagate_errors
    @task.call
  else
    begin
      @task.call
    rescue OneApm::ForceRestartException, OneApm::ForceDisconnectException
      raise
    rescue => e
      OneApm::Manager.logger.error "Error running task in Agent Worker Loop:", e
    end
  end
end

#schedule_next_invocationObject



44
45
46
47
48
49
50
# File 'lib/one_apm/support/event/worker_loop.rb', line 44

def schedule_next_invocation
  now = Time.now
  while @next_invocation_time <= now && @period > 0
    @next_invocation_time += @period
  end
  @next_invocation_time - Time.now
end

#setup(period, task) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/one_apm/support/event/worker_loop.rb', line 23

def setup(period, task)
  @task = task
  @period = period if period
  @should_run = true
  @iterations = 0

  now = Time.now
  @deadline = now + @duration if @duration
  @next_invocation_time = (now + @period)
end

#stopObject



64
65
66
# File 'lib/one_apm/support/event/worker_loop.rb', line 64

def stop
  @should_run = false
end

#under_duration?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/one_apm/support/event/worker_loop.rb', line 56

def under_duration?
  !@deadline || Time.now < @deadline
end

#under_limit?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/one_apm/support/event/worker_loop.rb', line 60

def under_limit?
  @limit.nil? || @iterations < @limit
end