pg_queue 0.0.0a
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/bin/queue_worker +18 -0
- data/lib/pg_queue/job.rb +18 -0
- data/lib/pg_queue/queue.rb +26 -0
- data/lib/pg_queue/version.rb +3 -0
- data/lib/pg_queue/worker.rb +67 -0
- data/lib/pg_queue.rb +8 -0
- data/pg_queue.gemspec +19 -0
- metadata +69 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Rafael Souza
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# PgQueue
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'pg_queue'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install pg_queue
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/queue_worker
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "pg_queue"
|
3
|
+
|
4
|
+
class MailQueue
|
5
|
+
def self.perform(email)
|
6
|
+
puts email
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
worker = PgQueue::Worker.new
|
11
|
+
|
12
|
+
["INT", "TERM"].each do |signal|
|
13
|
+
trap(signal) do
|
14
|
+
worker.stop
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
worker.start
|
data/lib/pg_queue/job.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module PgQueue
|
2
|
+
class Job
|
3
|
+
attr_reader :id, :klass, :args
|
4
|
+
|
5
|
+
def initialize(attributes)
|
6
|
+
@id = attributes["id"]
|
7
|
+
puts "new job #{@id}"
|
8
|
+
@klass = Object.const_get(attributes["klass"])
|
9
|
+
@args = JSON.load(attributes["args"])
|
10
|
+
end
|
11
|
+
|
12
|
+
def perform
|
13
|
+
puts "performing"
|
14
|
+
klass.perform(*args)
|
15
|
+
puts "performed"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "json"
|
2
|
+
|
3
|
+
module PgQueue
|
4
|
+
class Queue
|
5
|
+
attr_reader :connection
|
6
|
+
|
7
|
+
def initialize(connection)
|
8
|
+
@connection = connection
|
9
|
+
end
|
10
|
+
|
11
|
+
def enqueue(klass, *args)
|
12
|
+
sql = "INSERT INTO pg_queue_jobs (klass, args) VALUES ($1, $2) RETURNING id"
|
13
|
+
id = connection.exec(sql, [klass.name, JSON.dump(args)]).getvalue(0, 0)
|
14
|
+
puts "enqueued #{id}"
|
15
|
+
connection.exec("NOTIFY pg_queue_jobs")
|
16
|
+
end
|
17
|
+
|
18
|
+
def dequeue
|
19
|
+
result = connection.exec("SELECT id, klass, args FROM pg_queue_jobs LIMIT 1")
|
20
|
+
return nil unless result.count == 1
|
21
|
+
PgQueue::Job.new(result[0]).tap do |job|
|
22
|
+
connection.exec("DELETE FROM pg_queue_jobs WHERE id = $1", [job.id])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module PgQueue
|
2
|
+
class Worker
|
3
|
+
attr_reader :connection, :interval
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@connection = new_connection
|
7
|
+
@queue = PgQueue::Queue.new(@connection)
|
8
|
+
@interval = 5
|
9
|
+
end
|
10
|
+
|
11
|
+
def start
|
12
|
+
@running = true
|
13
|
+
|
14
|
+
listen
|
15
|
+
while running?
|
16
|
+
job = @queue.dequeue
|
17
|
+
if job
|
18
|
+
perform(job)
|
19
|
+
next
|
20
|
+
end
|
21
|
+
|
22
|
+
puts "waiting for jobs"
|
23
|
+
connection.wait_for_notify do |event, pid, payload|
|
24
|
+
if payload == "stop"
|
25
|
+
puts "stop notify received"
|
26
|
+
stop
|
27
|
+
else
|
28
|
+
puts "let's perform some jobs"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def listen
|
35
|
+
connection.exec("LISTEN pg_queue_jobs")
|
36
|
+
end
|
37
|
+
|
38
|
+
def unlisten
|
39
|
+
connection.exec("UNLISTEN pg_queue_jobs")
|
40
|
+
end
|
41
|
+
|
42
|
+
def stop
|
43
|
+
@running = false
|
44
|
+
new_connection.exec("NOTIFY pg_queue_jobs, 'stop'")
|
45
|
+
end
|
46
|
+
|
47
|
+
def running?
|
48
|
+
@running
|
49
|
+
end
|
50
|
+
|
51
|
+
protected
|
52
|
+
|
53
|
+
def new_connection
|
54
|
+
PGconn.open(:dbname => 'pg_queue_test')
|
55
|
+
end
|
56
|
+
|
57
|
+
def perform(job)
|
58
|
+
begin
|
59
|
+
puts job.inspect
|
60
|
+
job.perform
|
61
|
+
rescue => ex
|
62
|
+
puts ex
|
63
|
+
puts ex.message
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/pg_queue.rb
ADDED
data/pg_queue.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/pg_queue/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Rafael Souza"]
|
6
|
+
gem.email = ["me@rafaelss.com"]
|
7
|
+
gem.description = %q{Some experimentations with LISTEN/NOTIFY for background jobs}
|
8
|
+
gem.summary = %q{Background jobs using PostgreSQL's LISTEN/NOTIFY}
|
9
|
+
gem.homepage = "http://github.com/rafaelss/pg_queue"
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "pg_queue"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = PgQueue::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "pg", "~> 0.12.2"
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pg_queue
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0a
|
5
|
+
prerelease: 5
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rafael Souza
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: pg
|
16
|
+
requirement: &70283076765840 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.12.2
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70283076765840
|
25
|
+
description: Some experimentations with LISTEN/NOTIFY for background jobs
|
26
|
+
email:
|
27
|
+
- me@rafaelss.com
|
28
|
+
executables:
|
29
|
+
- queue_worker
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- Gemfile
|
35
|
+
- LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- bin/queue_worker
|
39
|
+
- lib/pg_queue.rb
|
40
|
+
- lib/pg_queue/job.rb
|
41
|
+
- lib/pg_queue/queue.rb
|
42
|
+
- lib/pg_queue/version.rb
|
43
|
+
- lib/pg_queue/worker.rb
|
44
|
+
- pg_queue.gemspec
|
45
|
+
homepage: http://github.com/rafaelss/pg_queue
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>'
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 1.3.1
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.10
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Background jobs using PostgreSQL's LISTEN/NOTIFY
|
69
|
+
test_files: []
|