Sha256: 2ef37b5972c8f5a9881ad00ba13908e2b7800c487c97c74a17a011962b526f4c

Contents?: true

Size: 1.11 KB

Versions: 14

Compression:

Stored size: 1.11 KB

Contents

# Used to run a given task every 60 seconds. 
class ScoutRails::BackgroundWorker
  # in seconds, time between when the worker thread wakes up and runs.
  PERIOD = 60
  
  def initialize
    @keep_running = true
  end
  
  def stop
    @keep_running = false
  end
  
  # Runs the task passed to +start+ once.
  def run_once
    @task.call if @task
  end
  
  # Starts running the passed block every 60 seconds (starting now).
  def start(&block)
    @task = block
    begin
      ScoutRails::Agent.instance.logger.debug "Starting Background Worker, running every #{PERIOD} seconds"
      next_time = Time.now
      while @keep_running do
        now = Time.now
        while now < next_time
          sleep_time = next_time - now
          sleep(sleep_time) if sleep_time > 0
          now = Time.now
        end
        @task.call
        while next_time <= now
          next_time += PERIOD
        end
      end
    rescue
      ScoutRails::Agent.instance.logger.debug "Background Worker Exception!!!!!!!"
      ScoutRails::Agent.instance.logger.debug $!.message
      ScoutRails::Agent.instance.logger.debug $!.backtrace
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
scout_rails-1.1.5.pre6 lib/scout_rails/background_worker.rb
scout_rails-1.1.5.pre5 lib/scout_rails/background_worker.rb
scout_rails-1.1.5.pre4 lib/scout_rails/background_worker.rb
scout_rails-1.1.5.pre3 lib/scout_rails/background_worker.rb
scout_rails-1.1.5.pre lib/scout_rails/background_worker.rb
scout_rails-1.1.4.pre lib/scout_rails/background_worker.rb
scout_rails-1.1.3 lib/scout_rails/background_worker.rb
scout_rails-1.1.2 lib/scout_rails/background_worker.rb
scout_rails-1.1.1 lib/scout_rails/background_worker.rb
scout_rails-1.1.1.pre lib/scout_rails/background_worker.rb
scout_rails-1.1.0 lib/scout_rails/background_worker.rb
scout_rails-1.0.9 lib/scout_rails/background_worker.rb
scout_rails-1.0.8 lib/scout_rails/background_worker.rb
scout_rails-1.0.8.pre.3 lib/scout_rails/background_worker.rb