class Sinatra::RunLater::Worker

Attributes

logger[RW]
thread[RW]

Public Class Methods

cleanup() click to toggle source
# File lib/sinatra/run-later.rb, line 68
def self.cleanup
  begin
    Timeout::timeout 10 do
      loop do
        break unless instance.thread[:running]

        # When run in Passenger, explicitly pass control to another thread
        # which will in return hand over control to the worker thread.
        # However, it doesn't work in Passenger 2.1.0, since it removes
        # all its classes before handing the request over to Rails.

        # Thread.pass if defined?(::Passenger)
      end
    end
  rescue Timeout::Error
    # logger.warn("Worker thread takes too long and will be killed.")
    # logger.flush
    instance.thread.kill
    @worker = RunLater::Worker.new
  end
end
instance() click to toggle source
# File lib/sinatra/run-later.rb, line 52
def self.instance
  @worker ||= RunLater::Worker.new
end
new() click to toggle source
# File lib/sinatra/run-later.rb, line 37
def initialize
  @thread = Thread.new {

    # removing this seems to allow it shut down nicely
    # trap :INT do
    #  RunLater::Worker.shutdown
    #  exit
    # end

    loop {
      process_queue
    }
  }
end
shutdown() click to toggle source
# File lib/sinatra/run-later.rb, line 56
def self.shutdown
  begin
    Timeout::timeout 10 do
      loop { break unless instance.thread[:running] }
    end
  rescue Timeout::Error
    # logger.error("Worker thread timed out. Forcing shutdown.")
  ensure
    instance.thread.kill
  end
end

Public Instance Methods

process_queue() click to toggle source
# File lib/sinatra/run-later.rb, line 90
def process_queue
  begin
    while block = RunLater.queue.pop
      Thread.pass
      Thread.current[:running] = true
      block.call
      Thread.current[:running] = false
      # logger.flush
    end
  rescue Exception => e
    # logger.error("Worker thread crashed, retrying. Error was: #{e}")
    # logger.flush
    Thread.current[:running] = false
    retry
  end
end