sequel-pg_advisory_lock 0.1.1
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.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +38 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +105 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/gemfiles/sequel-4.gemfile +5 -0
- data/gemfiles/sequel-5.gemfile +5 -0
- data/lib/sequel/extensions/pg_advisory_lock.rb +89 -0
- data/sequel-pg_advisory_lock.gemspec +35 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b48cb4023fcc88c838d6de22714b92c61437f02f
|
4
|
+
data.tar.gz: f0ec5d03ce435061e6a995e999b79f34c67f0dcc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8adac28854b26f69ecdd2324b0ed1c76028b07b1975d5317ae46e5914044bf04d8fea57f560e247c67f0dc2ecd432364c187e08293e2af99b4d2f8c228eaa523
|
7
|
+
data.tar.gz: 6dd7a64a7049aeb7fba91de618224cf6a9dc6909c605228e80ba771f0f9ca8cbf95047aecf5cdd353c062ae196ee3ede5c4d433be2badbb790b41779f5efeeff
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.9.3
|
4
|
+
- 2.0.0
|
5
|
+
- 2.1
|
6
|
+
- 2.2
|
7
|
+
- 2.3
|
8
|
+
- 2.4
|
9
|
+
- jruby-19mode
|
10
|
+
- jruby-9.0.5.0
|
11
|
+
- jruby-9.1.12.0
|
12
|
+
- jruby-head
|
13
|
+
- ruby-head
|
14
|
+
|
15
|
+
matrix:
|
16
|
+
allow_failures:
|
17
|
+
- rvm: jruby-head
|
18
|
+
- rvm: ruby-head
|
19
|
+
- rvm: jruby-19mode
|
20
|
+
|
21
|
+
gemfile:
|
22
|
+
- gemfiles/sequel-4.gemfile
|
23
|
+
- gemfiles/sequel-5.gemfile
|
24
|
+
|
25
|
+
before_install:
|
26
|
+
- gem install bundler
|
27
|
+
|
28
|
+
before_script:
|
29
|
+
- psql -c 'create database sequel_test;' -U postgres
|
30
|
+
|
31
|
+
env:
|
32
|
+
- TEST_DB_NAME=sequel_test
|
33
|
+
|
34
|
+
addons:
|
35
|
+
postgresql: "9.6"
|
36
|
+
|
37
|
+
services:
|
38
|
+
- postgresql
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Yury Shchyhlinski
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
# sequel-pg_advisory_lock [](https://travis-ci.org/yuryroot/sequel-pg_advisory_lock)
|
2
|
+
|
3
|
+
Gem `sequel-pg_advisory_lock` is an extension for ruby [Sequel](https://github.com/jeremyevans/sequel) library
|
4
|
+
that helps using [PostgreSQL advisory locks](https://www.postgresql.org/docs/9.6/static/explicit-locking.html#ADVISORY-LOCKS)
|
5
|
+
in your application.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'sequel-pg_advisory_lock'
|
13
|
+
```
|
14
|
+
|
15
|
+
and then execute:
|
16
|
+
|
17
|
+
```
|
18
|
+
$ bundle
|
19
|
+
```
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
```
|
24
|
+
$ gem install sequel-pg_advisory_lock
|
25
|
+
```
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
First, you should load an extension for `Sequel::Database` instance:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
DB.extension :pg_advisory_lock
|
33
|
+
```
|
34
|
+
|
35
|
+
Then, you should register new lock by specifying unique name:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
DB.register_advisory_lock(:my_lock)
|
39
|
+
|
40
|
+
```
|
41
|
+
|
42
|
+
By default, `pg_advisory_lock` *PostgreSQL* function will be associated with registered lock.
|
43
|
+
|
44
|
+
It's also possible to specify different function in second parameter of `register_advisory_lock` method, for example:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
DB.register_advisory_lock(:my_lock, :pg_try_advisory_lock)
|
48
|
+
````
|
49
|
+
|
50
|
+
All supported lock functions are described in [here](#available-types-of-locks).
|
51
|
+
|
52
|
+
Finally, you can use registered lock:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
DB.with_advisory_lock(:my_lock) do
|
56
|
+
# do something
|
57
|
+
end
|
58
|
+
|
59
|
+
```
|
60
|
+
|
61
|
+
An optional *4-bytes integer* parameter can be passed to `with_advisory_lock` method call:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
DB.with_advisory_lock(:my_lock, 1) do
|
65
|
+
# do something
|
66
|
+
end
|
67
|
+
|
68
|
+
```
|
69
|
+
|
70
|
+
## Available types of locks
|
71
|
+
|
72
|
+
There are 4 supported *PostgreSQL* lock functions which can be used in `register_advisory_lock`:
|
73
|
+
|
74
|
+
* `pg_advisory_lock` (default)
|
75
|
+
|
76
|
+
Waits of lock releasing if someone already owns requested lock.
|
77
|
+
|
78
|
+
* `pg_try_advisory_lock`
|
79
|
+
|
80
|
+
Doesn't wait of lock releasing, returns nil if someone already owns requested lock.
|
81
|
+
|
82
|
+
* `pg_advisory_xact_lock`
|
83
|
+
|
84
|
+
Waits of lock releasing if someone already owns requested lock.
|
85
|
+
Releases lock immediately after database transaction ends.
|
86
|
+
Requires manually opened transaction before using this lock.
|
87
|
+
|
88
|
+
* `pg_try_advisory_xact_lock`
|
89
|
+
|
90
|
+
Doesn't wait of lock releasing, returns nil if someone already owns requested lock.
|
91
|
+
Releases lock immediately after database transaction ends.
|
92
|
+
Requires manually opened transaction before using this lock.
|
93
|
+
|
94
|
+
For more information see [PostgreSQL documentation](https://www.postgresql.org/docs/9.6/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS).
|
95
|
+
|
96
|
+
## Contributing
|
97
|
+
|
98
|
+
1. Fork the project (https://github.com/yuryroot/sequel-pg_advisory_lock).
|
99
|
+
2. Create your feature branch (`git checkout -b my-new-feature`).
|
100
|
+
3. Implement your feature or bug fix.
|
101
|
+
4. Add tests for your feature or bug fix.
|
102
|
+
5. Run `rake` to make sure all tests pass.
|
103
|
+
6. Commit your changes (`git commit -am 'Add new feature'`).
|
104
|
+
7. Push to the branch (`git push origin my-new-feature`).
|
105
|
+
8. Create new pull request.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'sequel/extensions/pg_advisory_lock'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
require 'zlib'
|
3
|
+
|
4
|
+
module Sequel
|
5
|
+
module Postgres
|
6
|
+
module PgAdvisoryLock
|
7
|
+
|
8
|
+
SESSION_LEVEL_LOCKS = [
|
9
|
+
:pg_advisory_lock,
|
10
|
+
:pg_try_advisory_lock
|
11
|
+
].freeze
|
12
|
+
|
13
|
+
TRANSACTION_LEVEL_LOCKS = [
|
14
|
+
:pg_advisory_xact_lock,
|
15
|
+
:pg_try_advisory_xact_lock
|
16
|
+
].freeze
|
17
|
+
|
18
|
+
LOCK_FUNCTIONS = (SESSION_LEVEL_LOCKS + TRANSACTION_LEVEL_LOCKS).freeze
|
19
|
+
|
20
|
+
DEFAULT_LOCK_FUNCTION = :pg_advisory_lock
|
21
|
+
UNLOCK_FUNCTION = :pg_advisory_unlock
|
22
|
+
|
23
|
+
def registered_advisory_locks
|
24
|
+
@registered_advisory_locks ||= {}
|
25
|
+
end
|
26
|
+
|
27
|
+
def with_advisory_lock(name, id = nil, &block)
|
28
|
+
options = registered_advisory_locks.fetch(name.to_sym)
|
29
|
+
|
30
|
+
lock_key = options.fetch(:key)
|
31
|
+
function_params = [lock_key, id].compact
|
32
|
+
|
33
|
+
lock_function = options.fetch(:lock_function)
|
34
|
+
transaction_level_lock = TRANSACTION_LEVEL_LOCKS.include?(lock_function)
|
35
|
+
|
36
|
+
if transaction_level_lock
|
37
|
+
# TODO: It's allowed to specify additional options (in particular, :server)
|
38
|
+
# while opening database transaction.
|
39
|
+
# That's why this check must be smarter.
|
40
|
+
unless in_transaction?
|
41
|
+
raise Error, "Transaction must be manually opened before using transaction level lock '#{lock_function}'"
|
42
|
+
end
|
43
|
+
|
44
|
+
if get(Sequel.function(lock_function, *function_params))
|
45
|
+
yield
|
46
|
+
end
|
47
|
+
else
|
48
|
+
synchronize do
|
49
|
+
if get(Sequel.function(lock_function, *function_params))
|
50
|
+
begin
|
51
|
+
result = yield
|
52
|
+
ensure
|
53
|
+
get(Sequel.function(UNLOCK_FUNCTION, *function_params))
|
54
|
+
result
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def register_advisory_lock(name, lock_function = DEFAULT_LOCK_FUNCTION)
|
62
|
+
name = name.to_sym
|
63
|
+
|
64
|
+
if registered_advisory_locks.key?(name)
|
65
|
+
raise Error, "Lock with name :#{name} is already registered"
|
66
|
+
end
|
67
|
+
|
68
|
+
key = advisory_lock_key_for(name)
|
69
|
+
if registered_advisory_locks.values.any? { |opts| opts.fetch(:key) == key }
|
70
|
+
raise Error, "Lock key #{key} is already taken"
|
71
|
+
end
|
72
|
+
|
73
|
+
function = lock_function.to_sym
|
74
|
+
unless LOCK_FUNCTIONS.include?(function)
|
75
|
+
raise Error, "Invalid lock function :#{function}"
|
76
|
+
end
|
77
|
+
|
78
|
+
registered_advisory_locks[name] = { key: key, lock_function: function }
|
79
|
+
end
|
80
|
+
|
81
|
+
def advisory_lock_key_for(lock_name)
|
82
|
+
Zlib.crc32(lock_name.to_s) % 2 ** 31
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
Database.register_extension(:pg_advisory_lock, Postgres::PgAdvisoryLock)
|
89
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'sequel-pg_advisory_lock'
|
7
|
+
spec.version = '0.1.1'
|
8
|
+
spec.authors = ['Yury Shchyhlinski']
|
9
|
+
spec.email = ['Shchyhlinski.YL@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = "#{spec.name} is an extension for ruby Sequel library that helps using PostgreSQL advisory locks in your application"
|
12
|
+
spec.homepage = "https://github.com/yuryroot/#{spec.name}"
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = 'bin'
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_dependency 'sequel'
|
23
|
+
|
24
|
+
if RUBY_PLATFORM =~ /java/
|
25
|
+
spec.add_development_dependency 'jdbc-postgres', '9.4.1200'
|
26
|
+
elsif RUBY_VERSION < '2.0.0'
|
27
|
+
spec.add_development_dependency 'pg', '<0.19.0'
|
28
|
+
else
|
29
|
+
spec.add_development_dependency 'pg'
|
30
|
+
end
|
31
|
+
|
32
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
33
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
34
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel-pg_advisory_lock
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yury Shchyhlinski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sequel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pg
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.15'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.15'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '5.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '5.0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- Shchyhlinski.YL@gmail.com
|
86
|
+
executables:
|
87
|
+
- console
|
88
|
+
- setup
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- ".gitignore"
|
93
|
+
- ".travis.yml"
|
94
|
+
- CHANGELOG.md
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- gemfiles/sequel-4.gemfile
|
102
|
+
- gemfiles/sequel-5.gemfile
|
103
|
+
- lib/sequel/extensions/pg_advisory_lock.rb
|
104
|
+
- sequel-pg_advisory_lock.gemspec
|
105
|
+
homepage: https://github.com/yuryroot/sequel-pg_advisory_lock
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.5.1
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: sequel-pg_advisory_lock is an extension for ruby Sequel library that helps
|
129
|
+
using PostgreSQL advisory locks in your application
|
130
|
+
test_files: []
|