Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 122 lines (105 sloc) 3.296 kb
13c3671 @senny extract configuration constants in a backwards compatible fashion.
senny authored
1 require_relative "queue_classic/config"
2
08370ab add support for QC methods
Ryan R. Smith authored
3 module QC
13c3671 @senny extract configuration constants in a backwards compatible fashion.
senny authored
4 extend QC::Config
5
6 # Assign constants for backwards compatibility.
7 # They should no longer be used. Prefer the corresponding methods.
8 # See +QC::Config+ for more details.
5ee3365 @senny deprecate QC::XYZ configuration constants.
senny authored
9 DEPRECATED_CONSTANTS = {
10 :APP_NAME => :app_name,
11 :WAIT_TIME => :wait_time,
12 :TABLE_NAME => :table_name,
13 :QUEUE => :queue,
14 :QUEUES => :queues,
15 :TOP_BOUND => :top_bound,
16 :FORK_WORKER => :fork_worker?,
17 }
18
19 def self.const_missing(const_name)
20 if DEPRECATED_CONSTANTS.key? const_name
21 config_method = DEPRECATED_CONSTANTS[const_name]
22 $stderr.puts <<-MSG
23 The constant QC::#{const_name} is deprecated and will be removed in the future.
24 Please use the method QC.#{config_method} instead.
25 MSG
26 QC.public_send config_method
ae9524f @senny only forward messages the `default_queue` understands.
senny authored
27 else
28 super
5ee3365 @senny deprecate QC::XYZ configuration constants.
senny authored
29 end
30 end
7630272 revert the removal of forking worker
Ryan Smith authored
31
7f3bf41 describe config options in the code
Ryan R. Smith (ace hacker) authored
32 # Defer method calls on the QC module to the
33 # default queue. This facilitates QC.enqueue()
08370ab add support for QC methods
Ryan R. Smith authored
34 def self.method_missing(sym, *args, &block)
ae9524f @senny only forward messages the `default_queue` understands.
senny authored
35 if default_queue.respond_to? sym
d21df49 @senny use `public_send` to delegate method calls to `QC.default_queue`.
senny authored
36 default_queue.public_send(sym, *args, &block)
ae9524f @senny only forward messages the `default_queue` understands.
senny authored
37 else
38 super
39 end
08370ab add support for QC methods
Ryan R. Smith authored
40 end
ea35571 first pass at removing some code
Ryan R. Smith (ace hacker) authored
41
e7c60fe @keithpitt Correctly handle respond_to on the QC object.
keithpitt authored
42 # Ensure QC.respond_to?(:enqueue) equals true (ruby 1.9 only)
43 def self.respond_to_missing?(method_name, include_private = false)
44 default_queue.respond_to?(method_name)
45 end
46
7f45c38 @jipiboily enable connection sharing for workers
jipiboily authored
47 def self.has_connection?
4686e0f @senny tests, don't fail when running `queue_classic_test.rb` in isolation.
senny authored
48 !default_conn_adapter.nil?
7f45c38 @jipiboily enable connection sharing for workers
jipiboily authored
49 end
50
2fefa5f @ryandotsmith Feature: Connection Adapter
ryandotsmith authored
51 def self.default_conn_adapter
8e208e2 @smathieu Make the connection adapter thread safe.
smathieu authored
52 t = Thread.current
53 return t[:qc_conn_adapter] if t[:qc_conn_adapter]
54 adapter = if rails_connection_sharing_enabled?
55 ConnAdapter.new(ActiveRecord::Base.connection.raw_connection)
04391ab @jipiboily add detection of ActiveRecord connection and use it
jipiboily authored
56 else
8e208e2 @smathieu Make the connection adapter thread safe.
smathieu authored
57 ConnAdapter.new
04391ab @jipiboily add detection of ActiveRecord connection and use it
jipiboily authored
58 end
8e208e2 @smathieu Make the connection adapter thread safe.
smathieu authored
59
60 t[:qc_conn_adapter] = adapter
2fefa5f @ryandotsmith Feature: Connection Adapter
ryandotsmith authored
61 end
62
65b8b87 @jipiboily fix coding style error
jipiboily authored
63 def self.default_conn_adapter=(conn)
8e208e2 @smathieu Make the connection adapter thread safe.
smathieu authored
64 Thread.current[:qc_conn_adapter] = conn
65a212b @jipiboily fix the connection sharing so it works as specified in README
jipiboily authored
65 end
66
6057420 Log data.
Ryan Smith (ace hacker) authored
67 def self.log_yield(data)
68 begin
69 t0 = Time.now
70 yield
71 rescue => e
bfb0e05 fix typo
♠ ace hacker authored
72 log({:at => "error", :error => e.inspect}.merge(data))
6057420 Log data.
Ryan Smith (ace hacker) authored
73 raise
74 ensure
75 t = Integer((Time.now - t0)*1000)
76 log(data.merge(:elapsed => t)) unless e
77 end
78 end
79
80 def self.log(data)
2c7ce26 remove scrolls dep
♠ ace hacker authored
81 result = nil
82 data = {:lib => "queue-classic"}.merge(data)
83 if block_given?
84 result = yield
20c0caa @gregburek Change log to report time elapsedin milliseconds
gregburek authored
85 data.merge(:elapsed => Integer((Time.now - t0)*1000))
2c7ce26 remove scrolls dep
♠ ace hacker authored
86 end
87 data.reduce(out=String.new) do |s, tup|
88 s << [tup.first, tup.last].join("=") << " "
89 end
90 puts(out) if ENV["DEBUG"]
91 return result
6057420 Log data.
Ryan Smith (ace hacker) authored
92 end
49d3600 @jipiboily move unlock_jobs_of_dead_workers
jipiboily authored
93
d00cf7f @jipiboily add conditional measure output
jipiboily authored
94 def self.measure(data)
95 if ENV['QC_MEASURE']
96 $stdout.puts("measure#qc.#{data}")
97 end
98 end
49d3600 @jipiboily move unlock_jobs_of_dead_workers
jipiboily authored
99
100 # This will unlock all jobs any postgres' PID that is not existing anymore
101 # to prevent any infinitely locked jobs
102 def self.unlock_jobs_of_dead_workers
5ee3365 @senny deprecate QC::XYZ configuration constants.
senny authored
103 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);")
49d3600 @jipiboily move unlock_jobs_of_dead_workers
jipiboily authored
104 end
e2408d3 @jipiboily add QC_RAILS_DATABASE to disable conn detection
jipiboily authored
105
dbb7c21 @senny `private` keyword has no effect on class methods.
senny authored
106 # private class methods
107 class << self
108 private
109
110 def rails_connection_sharing_enabled?
111 enabled = ENV.fetch('QC_RAILS_DATABASE', 'true') != 'false'
112 return false unless enabled
113 return Object.const_defined?("ActiveRecord") && ActiveRecord::Base.respond_to?("connection")
114 end
e2408d3 @jipiboily add QC_RAILS_DATABASE to disable conn detection
jipiboily authored
115 end
08370ab add support for QC methods
Ryan R. Smith authored
116 end
79c3e31 @ryandotsmith explicit requires
ryandotsmith authored
117
1178f7e @wurde Switch require to require_relative
wurde authored
118 require_relative "queue_classic/queue"
119 require_relative "queue_classic/worker"
120 require_relative "queue_classic/setup"
121 require_relative "queue_classic/railtie" if defined?(Rails)
Something went wrong with that request. Please try again.