pg_queue 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # PgQueue
2
2
 
3
- TODO: Write a gem description
3
+ Background jobs using PostgreSQL's LISTEN/NOTIFY
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,25 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ ### Rails
22
+
23
+ Create an initializer with the configuration
24
+
25
+ PgQueue.connection = ActiveRecord::Base.connection.raw_connection
26
+ PgQueue.logger = Rails.logger
27
+ PgQueue.interval = 3 # the number of seconds you want the worker wait after process the job
28
+
29
+ Add this line in the Rakefile
30
+
31
+ require "pg_queue/tasks"
32
+
33
+ And then start the worker running
34
+
35
+ rake environment pg_queue:work
36
+
37
+ That's it! You're now able to enqueue your jobs. To do that, just call
38
+
39
+ PgQueue.enqueue(MyQueueClass, "string", 1, false) # parameters will be JSON encoded
22
40
 
23
41
  ## Contributing
24
42
 
@@ -1,3 +1,3 @@
1
1
  module PgQueue
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -8,6 +8,7 @@ module PgQueue
8
8
  job = PgQueue.dequeue
9
9
  if job
10
10
  perform(job)
11
+ sleep(PgQueue.interval) if PgQueue.interval > 0
11
12
  next
12
13
  end
13
14
 
data/lib/pg_queue.rb CHANGED
@@ -26,6 +26,14 @@ module PgQueue
26
26
  end
27
27
  end
28
28
 
29
+ def self.interval
30
+ @interval || 0
31
+ end
32
+
33
+ def self.interval=(seconds)
34
+ @interval = seconds
35
+ end
36
+
29
37
  def self.enqueue(klass, *args)
30
38
  id = connection.insert(:pg_queue_jobs, { :class_name => klass.name, :args => MultiJson.encode(args) }, "id")
31
39
  logger.debug("enqueued #{id}")
@@ -32,6 +32,32 @@ describe PgQueue::Worker do
32
32
  subject.start
33
33
  end
34
34
  end
35
+
36
+ context "with interval" do
37
+ let(:job) { double("job") }
38
+
39
+ before do
40
+ PgQueue.should_receive(:dequeue).and_return(job)
41
+ job.should_receive(:perform)
42
+ end
43
+
44
+ after do
45
+ PgQueue.interval = nil
46
+ end
47
+
48
+ it "should not sleep if an interval is not defined" do
49
+ subject.should_receive(:sleep).never
50
+
51
+ subject.start
52
+ end
53
+
54
+ it "sleeps if an interval is defined" do
55
+ PgQueue.interval = 5
56
+ subject.should_receive(:sleep).with(5)
57
+
58
+ subject.start
59
+ end
60
+ end
35
61
  end
36
62
 
37
63
  context "stoping" do
@@ -11,16 +11,19 @@ describe PgQueue do
11
11
  end
12
12
 
13
13
  context "logging" do
14
- its(:logger) { should be_instance_of(Logger) }
15
-
16
- it "defines a new logger" do
17
- described_class.logger = MyLogger.new
18
- described_class.logger.should be_instance_of(MyLogger)
14
+ before do
15
+ described_class.logger = nil
19
16
  end
20
17
 
21
18
  after do
22
19
  described_class.logger = nil
23
20
  end
21
+
22
+ it "defines a logger" do
23
+ described_class.logger.should be_instance_of(Logger)
24
+ described_class.logger = MyLogger.new
25
+ described_class.logger.should be_instance_of(MyLogger)
26
+ end
24
27
  end
25
28
 
26
29
  context "database connection" do
@@ -39,6 +42,18 @@ describe PgQueue do
39
42
  end
40
43
  end
41
44
 
45
+ context "interval" do
46
+ after do
47
+ described_class.interval = nil
48
+ end
49
+
50
+ it "defines the interval" do
51
+ described_class.interval.should == 0
52
+ described_class.interval = 5
53
+ described_class.interval.should == 5
54
+ end
55
+ end
56
+
42
57
  context "enqueuing/dequeuing" do
43
58
  let(:connection) { described_class.connection }
44
59
  let(:result) { double("result") }
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.3
4
+ version: 0.0.4
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-28 00:00:00.000000000 Z
12
+ date: 2012-01-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pg
16
- requirement: &70341859292320 !ruby/object:Gem::Requirement
16
+ requirement: &70363119914140 !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: *70341859292320
24
+ version_requirements: *70363119914140
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: multi_json
27
- requirement: &70341859291820 !ruby/object:Gem::Requirement
27
+ requirement: &70363119913640 !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: *70341859291820
35
+ version_requirements: *70363119913640
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70341859291360 !ruby/object:Gem::Requirement
38
+ requirement: &70363119913180 !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: *70341859291360
46
+ version_requirements: *70363119913180
47
47
  description: Some experimentations with LISTEN/NOTIFY for background jobs
48
48
  email:
49
49
  - me@rafaelss.com