queue_classic 0.3.5.pre → 0.3.7.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/queue_classic/database.rb +14 -3
- data/lib/queue_classic/worker.rb +22 -2
- data/test/database_test.rb +28 -0
- metadata +5 -3
@@ -27,6 +27,11 @@ module QC
|
|
27
27
|
execute("SET application_name = 'queue_classic'")
|
28
28
|
end
|
29
29
|
|
30
|
+
def notify
|
31
|
+
log("NOTIFY")
|
32
|
+
execute("NOTIFY queue_classic_jobs")
|
33
|
+
end
|
34
|
+
|
30
35
|
def listen
|
31
36
|
log("LISTEN")
|
32
37
|
execute("LISTEN queue_classic_jobs")
|
@@ -37,9 +42,15 @@ module QC
|
|
37
42
|
execute("UNLISTEN queue_classic_jobs")
|
38
43
|
end
|
39
44
|
|
40
|
-
def
|
41
|
-
|
42
|
-
|
45
|
+
def drain_notify
|
46
|
+
until connection.notifies.nil?
|
47
|
+
log("draining notifications")
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def wait_for_notify(t)
|
52
|
+
log("waiting for notify timeout=#{t}")
|
53
|
+
connection.wait_for_notify(t)
|
43
54
|
log("done waiting for notify")
|
44
55
|
end
|
45
56
|
|
data/lib/queue_classic/worker.rb
CHANGED
@@ -14,6 +14,9 @@ module QC
|
|
14
14
|
@fork_worker = ENV["QC_FORK_WORKER"] == "true"
|
15
15
|
log("worker fork=#{@fork_worker}")
|
16
16
|
|
17
|
+
@listening_worker = ENV["QC_LISTENING_WORKER"] == "true"
|
18
|
+
log("worker listen=#{@listening_worker}")
|
19
|
+
|
17
20
|
handle_signals
|
18
21
|
end
|
19
22
|
|
@@ -25,6 +28,10 @@ module QC
|
|
25
28
|
@fork_worker
|
26
29
|
end
|
27
30
|
|
31
|
+
def can_listen?
|
32
|
+
@listening_worker
|
33
|
+
end
|
34
|
+
|
28
35
|
def handle_signals
|
29
36
|
%W(INT TERM).each do |sig|
|
30
37
|
trap(sig) do
|
@@ -88,8 +95,7 @@ module QC
|
|
88
95
|
attempts += 1
|
89
96
|
if attempts < MAX_LOCK_ATTEMPTS
|
90
97
|
seconds = 2**attempts
|
91
|
-
|
92
|
-
sleep(seconds)
|
98
|
+
wait(seconds)
|
93
99
|
log("worker tries again")
|
94
100
|
next
|
95
101
|
else
|
@@ -103,6 +109,20 @@ module QC
|
|
103
109
|
job
|
104
110
|
end
|
105
111
|
|
112
|
+
def wait(t)
|
113
|
+
if can_listen?
|
114
|
+
log("worker waiting on LISTEN")
|
115
|
+
@queue.database.listen
|
116
|
+
@queue.database.wait_for_notify(t) { log("message received") }
|
117
|
+
@queue.database.unlisten
|
118
|
+
@queue.database.drain_notify
|
119
|
+
log("worker finished LISTEN")
|
120
|
+
else
|
121
|
+
log("worker sleeps seconds=#{t}")
|
122
|
+
Kernel.sleep(t)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
106
126
|
#override this method to do whatever you want
|
107
127
|
def handle_failure(job,e)
|
108
128
|
puts "!"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require File.expand_path("../helper.rb", __FILE__)
|
2
|
+
|
3
|
+
context "DatabaseTest" do
|
4
|
+
|
5
|
+
setup do
|
6
|
+
@database = init_db
|
7
|
+
end
|
8
|
+
|
9
|
+
teardown do
|
10
|
+
@database.disconnect
|
11
|
+
end
|
12
|
+
|
13
|
+
test "drain_notify clears all of the notifications" do
|
14
|
+
@database.listen
|
15
|
+
@database.execute("NOTIFY queue_classic_jobs, 'hello'")
|
16
|
+
|
17
|
+
assert ! @database.connection.notifies.nil?
|
18
|
+
assert @database.connection.notifies.nil?
|
19
|
+
|
20
|
+
@database.execute("NOTIFY queue_classic_jobs, 'hello'")
|
21
|
+
@database.execute("NOTIFY queue_classic_jobs, 'hello'")
|
22
|
+
@database.execute("NOTIFY queue_classic_jobs, 'hello'")
|
23
|
+
|
24
|
+
@database.drain_notify
|
25
|
+
assert @database.connection.notifies.nil?
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: queue_classic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.7.pre
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-08-22 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pg
|
16
|
-
requirement: &
|
16
|
+
requirement: &2152255440 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: 0.11.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2152255440
|
25
25
|
description: Queue Classic (beta) is a queueing library for Ruby apps (Rails, Sinatra,
|
26
26
|
Etc...) Queue Classic features asynchronous job polling, database maintained locks
|
27
27
|
and no ridiculous dependencies. As a matter of fact, Queue Classic only requires
|
@@ -41,6 +41,7 @@ files:
|
|
41
41
|
- lib/queue_classic/worker.rb
|
42
42
|
- lib/queue_classic.rb
|
43
43
|
- test/database_helpers.rb
|
44
|
+
- test/database_test.rb
|
44
45
|
- test/durable_array_test.rb
|
45
46
|
- test/helper.rb
|
46
47
|
- test/job_test.rb
|
@@ -74,6 +75,7 @@ summary: Queue Classic (beta) is a queueing library for Ruby apps (Rails, Sinatr
|
|
74
75
|
and no ridiculous dependencies. As a matter of fact, Queue Classic only requires
|
75
76
|
the pg and json.(simple)
|
76
77
|
test_files:
|
78
|
+
- test/database_test.rb
|
77
79
|
- test/durable_array_test.rb
|
78
80
|
- test/job_test.rb
|
79
81
|
- test/queue_test.rb
|