pg_queue 0.0.0a

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pg_queue.gemspec
4
+ gemspec
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
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
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
@@ -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,3 @@
1
+ module PgQueue
2
+ VERSION = "0.0.0a"
3
+ 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
@@ -0,0 +1,8 @@
1
+ require "pg_queue/version"
2
+ require "pg"
3
+
4
+ module PgQueue
5
+ autoload :Worker, "pg_queue/worker"
6
+ autoload :Queue, "pg_queue/queue"
7
+ autoload :Job, "pg_queue/job"
8
+ end
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: []