pg_advisory_locker 0.9.0

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,24 @@
1
+ # See http://help.github.com/ignore-files/ for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile ~/.gitignore_global
6
+
7
+ # Ignore bundler config
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+
13
+ # Ignore all logfiles and tempfiles.
14
+ /log/*.log
15
+ /tmp
16
+ .idea/*
17
+ database.yml
18
+ /log/*.pid
19
+ spec/dummy/db/*.sqlite3
20
+ spec/dummy/log/*.log
21
+ spec/dummy/tmp/
22
+ spec/dummy/.sass-cache
23
+ spec/dummy/config/database.yml
24
+ spec/dummy/db/schema.rb
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Declare your gem's dependencies in partitioned.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ # To use debugger
14
+ # gem 'ruby-debug'
data/Gemfile.lock ADDED
@@ -0,0 +1,108 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pg_application_name (0.8.0)
5
+ pg
6
+ rails (>= 3.0.0)
7
+ rspec-rails
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ actionmailer (3.2.8)
13
+ actionpack (= 3.2.8)
14
+ mail (~> 2.4.4)
15
+ actionpack (3.2.8)
16
+ activemodel (= 3.2.8)
17
+ activesupport (= 3.2.8)
18
+ builder (~> 3.0.0)
19
+ erubis (~> 2.7.0)
20
+ journey (~> 1.0.4)
21
+ rack (~> 1.4.0)
22
+ rack-cache (~> 1.2)
23
+ rack-test (~> 0.6.1)
24
+ sprockets (~> 2.1.3)
25
+ activemodel (3.2.8)
26
+ activesupport (= 3.2.8)
27
+ builder (~> 3.0.0)
28
+ activerecord (3.2.8)
29
+ activemodel (= 3.2.8)
30
+ activesupport (= 3.2.8)
31
+ arel (~> 3.0.2)
32
+ tzinfo (~> 0.3.29)
33
+ activeresource (3.2.8)
34
+ activemodel (= 3.2.8)
35
+ activesupport (= 3.2.8)
36
+ activesupport (3.2.8)
37
+ i18n (~> 0.6)
38
+ multi_json (~> 1.0)
39
+ arel (3.0.2)
40
+ builder (3.0.3)
41
+ diff-lcs (1.1.3)
42
+ erubis (2.7.0)
43
+ hike (1.2.1)
44
+ i18n (0.6.1)
45
+ journey (1.0.4)
46
+ json (1.7.5)
47
+ mail (2.4.4)
48
+ i18n (>= 0.4.0)
49
+ mime-types (~> 1.16)
50
+ treetop (~> 1.4.8)
51
+ mime-types (1.19)
52
+ multi_json (1.3.6)
53
+ pg (0.14.1)
54
+ polyglot (0.3.3)
55
+ rack (1.4.1)
56
+ rack-cache (1.2)
57
+ rack (>= 0.4)
58
+ rack-ssl (1.3.2)
59
+ rack
60
+ rack-test (0.6.1)
61
+ rack (>= 1.0)
62
+ rails (3.2.8)
63
+ actionmailer (= 3.2.8)
64
+ actionpack (= 3.2.8)
65
+ activerecord (= 3.2.8)
66
+ activeresource (= 3.2.8)
67
+ activesupport (= 3.2.8)
68
+ bundler (~> 1.0)
69
+ railties (= 3.2.8)
70
+ railties (3.2.8)
71
+ actionpack (= 3.2.8)
72
+ activesupport (= 3.2.8)
73
+ rack-ssl (~> 1.3.2)
74
+ rake (>= 0.8.7)
75
+ rdoc (~> 3.4)
76
+ thor (>= 0.14.6, < 2.0)
77
+ rake (0.9.2.2)
78
+ rdoc (3.12)
79
+ json (~> 1.4)
80
+ rspec (2.8.0)
81
+ rspec-core (~> 2.8.0)
82
+ rspec-expectations (~> 2.8.0)
83
+ rspec-mocks (~> 2.8.0)
84
+ rspec-core (2.8.0)
85
+ rspec-expectations (2.8.0)
86
+ diff-lcs (~> 1.1.2)
87
+ rspec-mocks (2.8.0)
88
+ rspec-rails (2.8.1)
89
+ actionpack (>= 3.0)
90
+ activesupport (>= 3.0)
91
+ railties (>= 3.0)
92
+ rspec (~> 2.8.0)
93
+ sprockets (2.1.3)
94
+ hike (~> 1.2)
95
+ rack (~> 1.0)
96
+ tilt (~> 1.1, != 1.3.0)
97
+ thor (0.16.0)
98
+ tilt (1.3.3)
99
+ treetop (1.4.10)
100
+ polyglot
101
+ polyglot (>= 0.3.1)
102
+ tzinfo (0.3.33)
103
+
104
+ PLATFORMS
105
+ ruby
106
+
107
+ DEPENDENCIES
108
+ pg_application_name!
data/LICENSE ADDED
@@ -0,0 +1,30 @@
1
+ Copyright (c) 2010-2012, Fiksu, Inc.
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are
6
+ met:
7
+
8
+ o Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+
11
+ o Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the
14
+ distribution.
15
+
16
+ o Fiksu, Inc. nor the names of its contributors may be used to
17
+ endorse or promote products derived from this software without
18
+ specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README ADDED
@@ -0,0 +1,47 @@
1
+ pg_advisory_locker
2
+ ==================
3
+
4
+ Helper for calling PostgreSQL functions: pg_advisory_lock,
5
+ pg_advisory_try_lock, and pg_advisory_unlock.
6
+
7
+ Examples
8
+ ========
9
+
10
+ Basic example of passing a block to the locker
11
+
12
+ class Foo < ActiveRecord::Base
13
+ include PgAdvisoryLocker
14
+
15
+ def self.lock_for_whatever(&block)
16
+ return lock_record(0, &block)
17
+ end
18
+
19
+ def self.do_something
20
+ lock_for_whatever do
21
+ # do something here
22
+ end
23
+ end
24
+ end
25
+
26
+ Advisory lock on id:
27
+
28
+ class Foo < ActiveRecord::Base
29
+ include PgAdvisoryLocker
30
+
31
+ def lock_for_whatever
32
+ return advisory_lock
33
+ end
34
+
35
+ def unlock_for_whatever
36
+ return advisory_unlock
37
+ end
38
+
39
+ def do_something
40
+ lock_for_whatever
41
+ begin
42
+ # do something
43
+ ensure
44
+ unlock_for_whatever
45
+ end
46
+ end
47
+ end
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ task :default => :spec
9
+
10
+ begin
11
+ require 'rdoc/task'
12
+ rescue LoadError
13
+ require 'rdoc/rdoc'
14
+ require 'rake/rdoctask'
15
+ RDoc::Task = Rake::RDocTask
16
+ end
17
+
18
+ RDoc::Task.new(:rdoc) do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'pg_advisory_locker'
21
+ rdoc.options << '--line-numbers'
22
+ rdoc.rdoc_files.include('README')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
25
+
26
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,75 @@
1
+ module PgAdvisoryLocker
2
+ def self.included(base)
3
+ base.extend(ClassMethods)
4
+ end
5
+
6
+ def advisory_lock(&block)
7
+ return self.class.lock_record(id, &block)
8
+ end
9
+
10
+ def advisory_try_lock(&block)
11
+ return self.class.try_lock_record(id, &block)
12
+ end
13
+
14
+ def advisory_unlock
15
+ self.class.unlock_record(id)
16
+ end
17
+
18
+ module ClassMethods
19
+ def table_oid
20
+ if @table_oid.nil?
21
+ sql_table_components = table_name.split('.')
22
+ if sql_table_components.length == 1
23
+ sql_table_components.prepend('public')
24
+ end
25
+ sql = <<-SQL
26
+ SELECT
27
+ pg_class.oid
28
+ FROM
29
+ pg_class,pg_namespace
30
+ WHERE
31
+ pg_namespace.nspname = ? AND
32
+ pg_class.relnamespace = pg_namespace.oid AND
33
+ pg_class.relname = ?
34
+ SQL
35
+ @table_oid = find_by_sql([sql, *sql_table_components]).first.oid.to_i
36
+ end
37
+ return @table_oid
38
+ end
39
+
40
+ def lock_record(id, &block)
41
+ locked = uncached do
42
+ find_by_sql(["select pg_advisory_lock(?, ?)", table_oid, id])[0].pg_advisory_lock == "t"
43
+ end
44
+ if block.present?
45
+ begin
46
+ return block.call
47
+ ensure
48
+ unlock_record(id)
49
+ end
50
+ end
51
+ return locked
52
+ end
53
+
54
+ def try_lock_record(id, &block)
55
+ locked = uncached do
56
+ find_by_sql(["select pg_try_advisory_lock(?, ?)", table_oid, id])[0].pg_try_advisory_lock == "t"
57
+ end
58
+ if block.present?
59
+ begin
60
+ block.call
61
+ ensure
62
+ unlock_record(id)
63
+ end
64
+ end
65
+ return locked
66
+ end
67
+
68
+ def unlock_record(id)
69
+ unlocked = uncached do
70
+ find_by_sql(["select pg_advisory_unlock(?, ?)", table_oid, id])[0].pg_advisory_unlock == "t"
71
+ end
72
+ return unlocked
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,4 @@
1
+ module PgAdvisoryLocker
2
+ # the current version of this gem
3
+ VERSION = "0.9.0"
4
+ end
@@ -0,0 +1,2 @@
1
+ require 'pg_advisory_locker/pg_advisory_locker'
2
+ require 'pg_advisory_locker/version'
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :desirable do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,22 @@
1
+ $LOAD_PATH.push File.expand_path("../lib", __FILE__)
2
+
3
+ # Maintain your gem's version:
4
+ require "pg_advisory_locker/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'pg_advisory_locker'
8
+ s.version = PgAdvisoryLocker::VERSION
9
+ s.license = 'New BSD License'
10
+ s.date = '2012-09-14'
11
+ s.summary = "Helper for calling PostgreSQL pg_advisory_lock, pg_advisory_try_lock, and pg_advisory_unlock."
12
+ s.description = "This gem provides a module that, when included in your ActiveRecord model, provides methods to acquire and release advisory locks for PostgreSQL connections."
13
+ s.authors = ["Keith Gabryelski"]
14
+ s.email = 'keith@fiksu.com'
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.require_path = 'lib'
18
+ s.homepage = 'http://github.com/fiksu/pg_advisory_locker'
19
+ s.add_dependency "pg"
20
+ s.add_dependency "rails", '>= 3.0.0'
21
+ s.add_dependency 'rspec-rails'
22
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_advisory_locker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Keith Gabryelski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pg
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.0.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 3.0.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec-rails
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: This gem provides a module that, when included in your ActiveRecord model,
63
+ provides methods to acquire and release advisory locks for PostgreSQL connections.
64
+ email: keith@fiksu.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - Gemfile.lock
72
+ - LICENSE
73
+ - README
74
+ - Rakefile
75
+ - lib/pg_advisory_locker.rb
76
+ - lib/pg_advisory_locker/pg_advisory_locker.rb
77
+ - lib/pg_advisory_locker/version.rb
78
+ - lib/tasks/desirable_tasks.rake
79
+ - pg_advisory_locker.gemspec
80
+ homepage: http://github.com/fiksu/pg_advisory_locker
81
+ licenses:
82
+ - New BSD License
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.24
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Helper for calling PostgreSQL pg_advisory_lock, pg_advisory_try_lock, and
105
+ pg_advisory_unlock.
106
+ test_files: []