Module: QC

Extended by:
Config
Defined in:
lib/queue_classic.rb,
lib/queue_classic/queue.rb,
lib/queue_classic/setup.rb,
lib/queue_classic/config.rb,
lib/queue_classic/worker.rb,
lib/queue_classic/railtie.rb,
lib/queue_classic/version.rb,
lib/queue_classic/conn_adapter.rb,
lib/generators/queue_classic/install_generator.rb

Defined Under Namespace

Modules: Config, Setup Classes: ConnAdapter, InstallGenerator, Queue, Railtie, Worker

Constant Summary collapse

DEPRECATED_CONSTANTS =

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

{
  :APP_NAME => :app_name,
  :WAIT_TIME => :wait_time,
  :TABLE_NAME => :table_name,
  :QUEUE => :queue,
  :QUEUES => :queues,
  :TOP_BOUND => :top_bound,
  :FORK_WORKER => :fork_worker?,
}
VERSION =
"4.0.0"

Class Method Summary collapse

Methods included from Config

app_name, default_queue, default_queue=, default_worker_class, default_worker_class=, fork_worker?, queue, queues, reset_config, table_name, top_bound, wait_time

Class Method Details

.const_missing(const_name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/queue_classic.rb', line 21

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_adapterObject



53
54
55
# File 'lib/queue_classic.rb', line 53

def self.default_conn_adapter
  Thread.current[:qc_conn_adapter] ||= ConnAdapter.new(active_record_connection_share: rails_connection_sharing_enabled?)
end

.default_conn_adapter=(conn) ⇒ Object



57
58
59
# File 'lib/queue_classic.rb', line 57

def self.default_conn_adapter=(conn)
  Thread.current[:qc_conn_adapter] = conn
end

.has_connection?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/queue_classic.rb', line 49

def self.has_connection?
  !default_conn_adapter.nil?
end

.log(data) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/queue_classic.rb', line 74

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) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/queue_classic.rb', line 61

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) ⇒ Object



88
89
90
91
92
# File 'lib/queue_classic.rb', line 88

def self.measure(data)
  if ENV['QC_MEASURE']
    $stdout.puts("measure#qc.#{data}")
  end
end

.method_missing(sym, *args, &block) ⇒ Object

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



36
37
38
39
40
41
42
# File 'lib/queue_classic.rb', line 36

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) ⇒ Boolean

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

Returns:

  • (Boolean)


45
46
47
# File 'lib/queue_classic.rb', line 45

def self.respond_to_missing?(method_name, include_private = false)
  default_queue.respond_to?(method_name)
end

.unlock_jobs_of_dead_workersObject

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



96
97
98
# File 'lib/queue_classic.rb', line 96

def self.unlock_jobs_of_dead_workers
  default_conn_adapter.execute("UPDATE #{QC.table_name} SET locked_at = NULL, locked_by = NULL WHERE locked_by NOT IN (SELECT pid FROM pg_stat_activity);")
end