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
-
#period ⇒ Object
readonly
Returns the value of attribute period.
Instance Method Summary collapse
-
#initialize(period = DEFAULT_PERIOD) ⇒ BackgroundWorker
constructor
A new instance of BackgroundWorker.
-
#run_once ⇒ Object
Runs the task passed to
start
once. -
#start(&block) ⇒ Object
Starts running the passed block every 60 seconds (starting now).
- #stop ⇒ Object
Constructor Details
#initialize(period = DEFAULT_PERIOD) ⇒ BackgroundWorker
Returns a new instance of BackgroundWorker.
9 10 11 12 |
# File 'lib/scout_apm/background_worker.rb', line 9 def initialize(period=DEFAULT_PERIOD) @period = period @keep_running = true end |
Instance Attribute Details
#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
#run_once ⇒ Object
Runs the task passed to start
once.
19 20 21 |
# File 'lib/scout_apm/background_worker.rb', line 19 def run_once @task.call if @task end |
#start(&block) ⇒ Object
Starts running the passed block every 60 seconds (starting now).
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/scout_apm/background_worker.rb', line 24 def start(&block) @task = block begin ScoutApm::Agent.instance.logger.debug "Starting Background Worker, running every #{period} seconds" # The first run should be 1 period of time from now next_time = Time.now + period loop do now = Time.now # Sleep the correct amount of time to reach next_time while now < next_time sleep_time = next_time - now sleep(sleep_time) if sleep_time > 0 now = Time.now end # Bail out if @keep_running is false break unless @keep_running @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 end rescue ScoutApm::Agent.instance.logger.debug "Background Worker Exception!" ScoutApm::Agent.instance.logger.debug $!. ScoutApm::Agent.instance.logger.debug $!.backtrace end end |
#stop ⇒ Object
14 15 16 |
# File 'lib/scout_apm/background_worker.rb', line 14 def stop @keep_running = false end |