Class: ScoutApm::BackgroundWorker
- Inherits:
-
Object
- Object
- ScoutApm::BackgroundWorker
- Defined in:
- lib/scout_apm/background_worker.rb
Constant Summary collapse
- DEFAULT_PERIOD =
in seconds, time between when the worker thread wakes up and runs.
60
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#period ⇒ Object
readonly
Returns the value of attribute period.
Instance Method Summary collapse
-
#initialize(context, period = DEFAULT_PERIOD) ⇒ BackgroundWorker
constructor
A new instance of BackgroundWorker.
- #logger ⇒ Object
-
#run_once ⇒ Object
Runs the task passed to
start
once. - #running? ⇒ Boolean
-
#start(&block) ⇒ Object
Starts running the passed block every 60 seconds (starting now).
- #stop ⇒ Object
Constructor Details
#initialize(context, period = DEFAULT_PERIOD) ⇒ BackgroundWorker
Returns a new instance of BackgroundWorker.
11 12 13 14 15 |
# File 'lib/scout_apm/background_worker.rb', line 11 def initialize(context, period=DEFAULT_PERIOD) @context = context @period = period @keep_running = true end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
9 10 11 |
# File 'lib/scout_apm/background_worker.rb', line 9 def context @context end |
#period ⇒ Object (readonly)
Returns the value of attribute period.
7 8 9 |
# File 'lib/scout_apm/background_worker.rb', line 7 def period @period end |
Instance Method Details
#logger ⇒ Object
17 18 19 |
# File 'lib/scout_apm/background_worker.rb', line 17 def logger context.logger end |
#run_once ⇒ Object
Runs the task passed to start
once.
31 32 33 |
# File 'lib/scout_apm/background_worker.rb', line 31 def run_once @task.call if @task end |
#running? ⇒ Boolean
21 22 23 |
# File 'lib/scout_apm/background_worker.rb', line 21 def running? @keep_running end |
#start(&block) ⇒ Object
Starts running the passed block every 60 seconds (starting now).
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/scout_apm/background_worker.rb', line 36 def start(&block) @task = block logger.debug "Background Worker: starting to run every #{period} seconds" # The first run should be 1 period of time from now next_time = Time.now + period loop do begin now = Time.now # Sleep the correct amount of time to reach next_time while now < next_time && @keep_running sleep_time = next_time - now sleep(sleep_time) if sleep_time > 0 now = Time.now end # Bail out if @keep_running is false unless @keep_running logger.debug "Background Worker: breaking from loop" break end @task.call # Adjust the next time to run forward by @periods until it is in the future while next_time <= now next_time += period end rescue logger.debug "Background Worker Exception!" logger.debug $!. logger.debug $!.backtrace end end end |
#stop ⇒ Object
25 26 27 28 |
# File 'lib/scout_apm/background_worker.rb', line 25 def stop logger.debug "Background Worker: stop requested" @keep_running = false end |