/
queue_classic.rb
91 lines (75 loc) · 2.61 KB
/
queue_classic.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require "scrolls"
require "pg"
require "uri"
$: << File.expand_path(__FILE__, "lib")
require "queue_classic/okjson"
require "queue_classic/conn"
require "queue_classic/queries"
require "queue_classic/queue"
require "queue_classic/worker"
module QC
# ENV["LOG_LEVEL"] is used in Scrolls
Scrolls::Log.start
Root = File.expand_path("..", File.dirname(__FILE__))
SqlFunctions = File.join(QC::Root, "/sql/ddl.sql")
DropSqlFunctions = File.join(QC::Root, "/sql/drop_ddl.sql")
# You can use the APP_NAME to query for
# postgres related process information in the
# pg_stat_activity table. Don't set this unless
# you are using PostgreSQL > 9.0
if APP_NAME = ENV["QC_APP_NAME"]
Conn.execute("SET application_name = '#{APP_NAME}'")
end
# Why do you want to change the table name?
# Just deal with the default OK?
# If you do want to change this, you will
# need to update the PL/pgSQL lock_head() function.
# Come on. Don't do it.... Just stick with the default.
TABLE_NAME = "queue_classic_jobs"
# Each row in the table will have a column that
# notes the queue. You can point your workers
# at different queues but only one at a time.
QUEUE = ENV["QUEUE"] || "default"
# Set this to 1 for strict FIFO.
# There is nothing special about 9....
TOP_BOUND = (ENV["QC_TOP_BOUND"] || 9).to_i
# If you are using PostgreSQL > 9
# then you will have access to listen/notify with payload.
# Set this value if you wish to make your worker more efficient.
LISTENING_WORKER = !ENV["QC_LISTENING_WORKER"].nil?
# Set this variable if you wish for
# the worker to fork a UNIX process for
# each locked job. Remember to re-establish
# any database connections. See the worker
# for more details.
FORK_WORKER = !ENV["QC_FORK_WORKER"].nil?
# The worker uses an exponential back-off
# algorithm to lock a job. This value will be used
# as the max exponent.
MAX_LOCK_ATTEMPTS = (ENV["QC_MAX_LOCK_ATTEMPTS"] || 5).to_i
# Defer method calls on the QC module to the
# default queue. This facilitates QC.enqueue()
def self.method_missing(sym, *args, &block)
default_queue.send(sym, *args, &block)
end
def self.default_queue
@default_queue ||= begin
Queue.new(QUEUE, LISTENING_WORKER)
end
end
def self.log_yield(data)
begin
t0 = Time.now
yield
rescue => e
log({:level => :error, :error => e.class, :message => e.message.strip}.merge(data))
raise
ensure
t = Integer((Time.now - t0)*1000)
log(data.merge(:elapsed => t)) unless e
end
end
def self.log(data)
Scrolls.log({:lib => :queue_classic}.merge(data))
end
end