pg_queue 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/queue_worker +0 -6
- data/lib/pg_queue/pg_extensions.rb +12 -0
- data/lib/pg_queue/tasks.rb +24 -0
- data/lib/pg_queue/version.rb +1 -1
- data/lib/pg_queue/worker.rb +2 -2
- data/lib/pg_queue.rb +4 -5
- data/spec/pg_queue/worker_spec.rb +1 -2
- data/spec/pg_queue_spec.rb +9 -11
- metadata +9 -8
data/bin/queue_worker
CHANGED
@@ -37,5 +37,17 @@ module PgQueue
|
|
37
37
|
def unlisten(key)
|
38
38
|
exec("UNLISTEN #{key}")
|
39
39
|
end
|
40
|
+
|
41
|
+
def new_connection
|
42
|
+
PGconn.open(
|
43
|
+
:host => host,
|
44
|
+
:port => port,
|
45
|
+
:user => user,
|
46
|
+
:password => pass,
|
47
|
+
:dbname => db
|
48
|
+
).tap do |conn|
|
49
|
+
conn.extend(PgExtensions)
|
50
|
+
end
|
51
|
+
end
|
40
52
|
end
|
41
53
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
namespace :pg_queue do
|
2
|
+
desc "Start worker"
|
3
|
+
task :work do
|
4
|
+
worker = PgQueue::Worker.new
|
5
|
+
|
6
|
+
["INT", "TERM"].each do |signal|
|
7
|
+
trap(signal) do
|
8
|
+
worker.stop
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
worker.start
|
13
|
+
end
|
14
|
+
|
15
|
+
desc "Drop jobs table"
|
16
|
+
task :drop_table do
|
17
|
+
PgQueue.connection.exec("DROP TABLE IF EXISTS pg_queue_jobs")
|
18
|
+
end
|
19
|
+
|
20
|
+
desc "Create jobs table"
|
21
|
+
task :create_table => :drop_table do
|
22
|
+
PgQueue.connection.exec("CREATE TABLE pg_queue_jobs (id SERIAL, class_name VARCHAR, args TEXT)")
|
23
|
+
end
|
24
|
+
end
|
data/lib/pg_queue/version.rb
CHANGED
data/lib/pg_queue/worker.rb
CHANGED
@@ -26,8 +26,7 @@ module PgQueue
|
|
26
26
|
|
27
27
|
def stop
|
28
28
|
@running = false
|
29
|
-
PgQueue.connection
|
30
|
-
PgQueue.connection.notify(:pg_queue_jobs, "stop")
|
29
|
+
PgQueue.connection.new_connection.notify(:pg_queue_jobs, "stop")
|
31
30
|
end
|
32
31
|
|
33
32
|
def running?
|
@@ -43,6 +42,7 @@ module PgQueue
|
|
43
42
|
rescue => ex
|
44
43
|
PgQueue.logger.fatal(ex)
|
45
44
|
PgQueue.logger.fatal(ex.message)
|
45
|
+
PgQueue.logger.fatal(ex.backtrace)
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
data/lib/pg_queue.rb
CHANGED
@@ -17,14 +17,13 @@ module PgQueue
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def self.connection
|
20
|
-
@connection
|
21
|
-
conn = PGconn.open(:dbname => 'pg_queue_test')
|
22
|
-
conn.extend(PgQueue::PgExtensions)
|
23
|
-
end
|
20
|
+
@connection
|
24
21
|
end
|
25
22
|
|
26
23
|
def self.connection=(object)
|
27
|
-
@connection = object
|
24
|
+
@connection = object.tap do |conn|
|
25
|
+
conn.extend(PgQueue::PgExtensions) if object.respond_to?(:exec)
|
26
|
+
end
|
28
27
|
end
|
29
28
|
|
30
29
|
def self.enqueue(klass, *args)
|
@@ -42,8 +42,7 @@ describe PgQueue::Worker do
|
|
42
42
|
end
|
43
43
|
|
44
44
|
it "notifies worker to stop" do
|
45
|
-
PgQueue.should_receive(:
|
46
|
-
PgQueue.should_receive(:connection).and_return(connection)
|
45
|
+
PgQueue.connection.should_receive(:new_connection).and_return(connection)
|
47
46
|
connection.should_receive(:notify).with(:pg_queue_jobs, "stop")
|
48
47
|
|
49
48
|
subject.should be_running
|
data/spec/pg_queue_spec.rb
CHANGED
@@ -1,10 +1,15 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
class MyLogger; end
|
4
|
-
class MyDbConnection; end
|
4
|
+
class MyDbConnection; def exec; end; end
|
5
5
|
class MyQueue; end
|
6
6
|
|
7
7
|
describe PgQueue do
|
8
|
+
before do
|
9
|
+
described_class.connection = MyDbConnection.new
|
10
|
+
described_class.logger = Logger.new("/dev/null")
|
11
|
+
end
|
12
|
+
|
8
13
|
context "logging" do
|
9
14
|
its(:logger) { should be_instance_of(Logger) }
|
10
15
|
|
@@ -19,25 +24,18 @@ describe PgQueue do
|
|
19
24
|
end
|
20
25
|
|
21
26
|
context "database connection" do
|
22
|
-
its(:connection) { should be_instance_of(
|
27
|
+
its(:connection) { should be_instance_of(MyDbConnection) }
|
23
28
|
|
24
29
|
context "extensions" do
|
25
30
|
subject { described_class.connection }
|
31
|
+
|
26
32
|
it { should respond_to(:insert) }
|
27
33
|
it { should respond_to(:notify) }
|
28
34
|
it { should respond_to(:delete) }
|
29
35
|
it { should respond_to(:first) }
|
30
36
|
it { should respond_to(:listen) }
|
31
37
|
it { should respond_to(:unlisten) }
|
32
|
-
|
33
|
-
|
34
|
-
it "defines a new connection" do
|
35
|
-
described_class.connection = MyDbConnection.new
|
36
|
-
described_class.connection.should be_instance_of(MyDbConnection)
|
37
|
-
end
|
38
|
-
|
39
|
-
after do
|
40
|
-
described_class.connection = nil
|
38
|
+
it { should respond_to(:new_connection) }
|
41
39
|
end
|
42
40
|
end
|
43
41
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_queue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-01-
|
12
|
+
date: 2012-01-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pg
|
16
|
-
requirement: &
|
16
|
+
requirement: &70341859292320 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 0.12.2
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70341859292320
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: multi_json
|
27
|
-
requirement: &
|
27
|
+
requirement: &70341859291820 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 1.0.4
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70341859291820
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70341859291360 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 2.8.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70341859291360
|
47
47
|
description: Some experimentations with LISTEN/NOTIFY for background jobs
|
48
48
|
email:
|
49
49
|
- me@rafaelss.com
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- lib/pg_queue.rb
|
63
63
|
- lib/pg_queue/job.rb
|
64
64
|
- lib/pg_queue/pg_extensions.rb
|
65
|
+
- lib/pg_queue/tasks.rb
|
65
66
|
- lib/pg_queue/version.rb
|
66
67
|
- lib/pg_queue/worker.rb
|
67
68
|
- pg_queue.gemspec
|