module QC

Constants

DEPRECATED_CONSTANTS

Assign constants for backwards compatibility. They should no longer be used. Prefer the corresponding methods. See QC::Config for more details.

VERSION

Public Class Methods

const_missing(const_name) click to toggle source
Calls superclass method
# File lib/queue_classic.rb, line 19
  def self.const_missing(const_name)
    if DEPRECATED_CONSTANTS.key? const_name
      config_method = DEPRECATED_CONSTANTS[const_name]
      $stderr.puts <<-MSG
The constant QC::#{const_name} is deprecated and will be removed in the future.
Please use the method QC.#{config_method} instead.
      MSG
      QC.public_send config_method
    else
      super
    end
  end
default_conn_adapter() click to toggle source
# File lib/queue_classic.rb, line 51
def self.default_conn_adapter
  t = Thread.current
  return t[:qc_conn_adapter] if t[:qc_conn_adapter]
  adapter = if rails_connection_sharing_enabled?
    ConnAdapter.new(ActiveRecord::Base.connection.raw_connection)
  else
    ConnAdapter.new
  end

  t[:qc_conn_adapter] = adapter
end
default_conn_adapter=(conn) click to toggle source
# File lib/queue_classic.rb, line 63
def self.default_conn_adapter=(conn)
  Thread.current[:qc_conn_adapter] = conn
end
has_connection?() click to toggle source
# File lib/queue_classic.rb, line 47
def self.has_connection?
  !default_conn_adapter.nil?
end
log(data) { || ... } click to toggle source
# File lib/queue_classic.rb, line 80
def self.log(data)
  result = nil
  data = {:lib => "queue-classic"}.merge(data)
  if block_given?
    result = yield
    data.merge(:elapsed => Integer((Time.now - t0)*1000))
  end
  data.reduce(out=String.new) do |s, tup|
    s << [tup.first, tup.last].join("=") << " "
  end
  puts(out) if ENV["DEBUG"]
  return result
end
log_yield(data) { || ... } click to toggle source
# File lib/queue_classic.rb, line 67
def self.log_yield(data)
  t0 = Time.now
  begin
    yield
  rescue => e
    log({:at => "error", :error => e.inspect}.merge(data))
    raise
  ensure
    t = Integer((Time.now - t0)*1000)
    log(data.merge(:elapsed => t)) unless e
  end
end
measure(data) click to toggle source
# File lib/queue_classic.rb, line 94
def self.measure(data)
  if ENV['QC_MEASURE']
    $stdout.puts("measure#qc.#{data}")
  end
end
method_missing(sym, *args, &block) click to toggle source

Defer method calls on the QC module to the default queue. This facilitates QC.enqueue()

Calls superclass method
# File lib/queue_classic.rb, line 34
def self.method_missing(sym, *args, &block)
  if default_queue.respond_to? sym
    default_queue.public_send(sym, *args, &block)
  else
    super
  end
end
respond_to_missing?(method_name, include_private = false) click to toggle source

Ensure QC.respond_to?(:enqueue) equals true (ruby 1.9 only)

# File lib/queue_classic.rb, line 43
def self.respond_to_missing?(method_name, include_private = false)
  default_queue.respond_to?(method_name)
end
unlock_jobs_of_dead_workers() click to toggle source

This will unlock all jobs any postgres' PID that is not existing anymore to prevent any infinitely locked jobs

# File lib/queue_classic.rb, line 102
def self.unlock_jobs_of_dead_workers
  pid_column = default_conn_adapter.server_version < 90200 ? "procpid" : "pid"
  default_conn_adapter.execute("UPDATE #{QC.table_name} SET locked_at = NULL, locked_by = NULL WHERE locked_by NOT IN (SELECT #{pid_column} FROM pg_stat_activity);")
end

Private Class Methods

rails_connection_sharing_enabled?() click to toggle source
# File lib/queue_classic.rb, line 111
def rails_connection_sharing_enabled?
  enabled = ENV.fetch('QC_RAILS_DATABASE', 'true') != 'false'
  return false unless enabled
  return Object.const_defined?("ActiveRecord") && ActiveRecord::Base.respond_to?("connection")
end