ProcessLock 0.1.2
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 +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/ProcessLock.gemspec +23 -0
- data/README.md +41 -0
- data/Rakefile +1 -0
- data/lib/ProcessLock/locked_error.rb +5 -0
- data/lib/ProcessLock/nix_locker.rb +43 -0
- data/lib/ProcessLock/version.rb +3 -0
- data/lib/ProcessLock.rb +22 -0
- data/spec/process_lock_spec.rb +35 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d61970b09ca1da8457f493e866c8b05b8dca9ee9
|
4
|
+
data.tar.gz: 2ed9372c86217c075f6a0ecb3837fe00007e1a0f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 575034da90d7591e6b8dbcb8a06528efb7a7fefdd264865b72e240525a3b7a73a9237fa701a4c54c12ca3ec14a7f6613e8c105673b29dc54f1dee97636e0a791
|
7
|
+
data.tar.gz: 35c71219c3e686ac924e7d7c4aaa8f5faf1e2f58db208763e71ee542d54e94b032a1a3842e35f22cc9e09a25a5b5df267c73daddd8d55f0e904a7223b4cf908b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Courtney de Lautour
|
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/ProcessLock.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ProcessLock/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ProcessLock"
|
8
|
+
spec.version = ProcessLock::VERSION
|
9
|
+
spec.authors = ["Courtney de Lautour"]
|
10
|
+
spec.email = ["clautour@thefrontiergroup.com.au"]
|
11
|
+
spec.summary = %q{Locks a method to a single process}
|
12
|
+
spec.homepage = "https://github.com/Amerdrix/ruby-ProcessLock"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
21
|
+
spec.add_development_dependency "rake", "~> 10"
|
22
|
+
spec.add_development_dependency "rspec", "~> 2.3"
|
23
|
+
end
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# ProcessLock
|
2
|
+
|
3
|
+
Provides a process level synchronization context, allowing only a single process to execute a section of code any given
|
4
|
+
time.
|
5
|
+
|
6
|
+
Windows is currently not supported.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'ProcessLock'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install ProcessLock
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
|
25
|
+
Wrap any code you want to run in a synchronized context with
|
26
|
+
|
27
|
+
ProcessLock.ensure_only_process do
|
28
|
+
# Your code here
|
29
|
+
end
|
30
|
+
|
31
|
+
Process lock will automatically infer a key based on the current file and method which is running. Optionally you can
|
32
|
+
specify a your self `ensure_only_process(:your_key_here)`
|
33
|
+
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it ( http://github.com/<Amerdrix>/ProcessLock/fork )
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'SecureRandom'
|
2
|
+
require_relative 'locked_error'
|
3
|
+
|
4
|
+
module ProcessLock
|
5
|
+
class NixLocker
|
6
|
+
|
7
|
+
EXISTS = Object.new
|
8
|
+
|
9
|
+
def ensure_only_process(name)
|
10
|
+
path = File.join(tmpdir, "#{name}.pid")
|
11
|
+
result = run(path) { yield }
|
12
|
+
raise LockedError if result == EXISTS
|
13
|
+
result
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def tmpdir
|
19
|
+
'/tmp'
|
20
|
+
end
|
21
|
+
|
22
|
+
def run(path)
|
23
|
+
thrown = false
|
24
|
+
catch(EXISTS) do
|
25
|
+
begin
|
26
|
+
begin
|
27
|
+
File.open(path, File::WRONLY|File::CREAT|File::EXCL) do |file|
|
28
|
+
file << Process.pid
|
29
|
+
end
|
30
|
+
rescue
|
31
|
+
thrown = true
|
32
|
+
throw(EXISTS, EXISTS)
|
33
|
+
end
|
34
|
+
|
35
|
+
yield
|
36
|
+
|
37
|
+
ensure
|
38
|
+
File.delete(path) unless thrown
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/ProcessLock.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "ProcessLock/version"
|
2
|
+
require "ProcessLock/locked_error"
|
3
|
+
require "ProcessLock/nix_locker"
|
4
|
+
|
5
|
+
module ProcessLock
|
6
|
+
class << self
|
7
|
+
def ensure_only_process(name = nil, &block)
|
8
|
+
raise ArgumentError, "No block given" unless block_given?
|
9
|
+
name ||= infer_name(block)
|
10
|
+
locker = NixLocker.new
|
11
|
+
locker.ensure_only_process(name, &block)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def infer_name(block)
|
17
|
+
method = block.binding.eval('__method__')
|
18
|
+
file = File.basename(block.binding.eval('__FILE__'), '.*')
|
19
|
+
"#{file}-#{method}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require_relative '../lib/process_lock'
|
2
|
+
require_relative '../lib/ProcessLock/locked_error'
|
3
|
+
|
4
|
+
describe ProcessLock do
|
5
|
+
describe '.ensure_only_process' do
|
6
|
+
|
7
|
+
specify 'raises an error when a block is not passed' do
|
8
|
+
expect { ProcessLock.ensure_only_process }.to raise_error(ArgumentError)
|
9
|
+
end
|
10
|
+
|
11
|
+
specify 'raises an error when run simultaneously' do
|
12
|
+
expect { ProcessLock.ensure_only_process { ProcessLock.ensure_only_process {} } }.to raise_error(ProcessLock::LockedError)
|
13
|
+
end
|
14
|
+
|
15
|
+
specify 'raises an error raised by the block' do
|
16
|
+
exp = Exception.new
|
17
|
+
expect { ProcessLock.ensure_only_process { raise exp } }.to raise_error(exp)
|
18
|
+
end
|
19
|
+
|
20
|
+
specify 'does not raise an error when running when a block is passed' do
|
21
|
+
expect { ProcessLock.ensure_only_process {} }.to_not raise_error
|
22
|
+
end
|
23
|
+
|
24
|
+
specify 'does not raise an error when run twice in a row' do
|
25
|
+
ProcessLock.ensure_only_process {}
|
26
|
+
expect { ProcessLock.ensure_only_process {} }.to_not raise_error
|
27
|
+
end
|
28
|
+
|
29
|
+
specify 'return the result of the block' do
|
30
|
+
result = Object.new
|
31
|
+
expect(ProcessLock.ensure_only_process { result }).to be result
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ProcessLock
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Courtney de Lautour
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.3'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- clautour@thefrontiergroup.com.au
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- ProcessLock.gemspec
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/ProcessLock.rb
|
69
|
+
- lib/ProcessLock/locked_error.rb
|
70
|
+
- lib/ProcessLock/nix_locker.rb
|
71
|
+
- lib/ProcessLock/version.rb
|
72
|
+
- spec/process_lock_spec.rb
|
73
|
+
homepage: https://github.com/Amerdrix/ruby-ProcessLock
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.2.0
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Locks a method to a single process
|
97
|
+
test_files:
|
98
|
+
- spec/process_lock_spec.rb
|