with_locking 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rvmrc +48 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +44 -0
- data/Rakefile +2 -0
- data/lib/with_locking/version.rb +3 -0
- data/lib/with_locking.rb +38 -0
- data/spec/with_locking_spec.rb +54 -0
- data/with_locking.gemspec +17 -0
- metadata +60 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use 1.9.3" > .rvmrc
|
9
|
+
environment_id="ruby-1.9.3-p125@with_locking"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.10.3" # 1.10.1 seams as a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
|
18
|
+
# First we attempt to load the desired environment directly from the environment
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
20
|
+
# CLI and selector. If you want feedback on which environment was used then
|
21
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
25
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
26
|
+
[[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
|
27
|
+
\. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
|
28
|
+
else
|
29
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
30
|
+
rvm --create "$environment_id" || {
|
31
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
32
|
+
return 1
|
33
|
+
}
|
34
|
+
fi
|
35
|
+
|
36
|
+
# If you use bundler, this might be useful to you:
|
37
|
+
# if [[ -s Gemfile ]] && {
|
38
|
+
# ! builtin command -v bundle >/dev/null ||
|
39
|
+
# builtin command -v bundle | grep $rvm_path/bin/bundle >/dev/null
|
40
|
+
# }
|
41
|
+
# then
|
42
|
+
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
|
43
|
+
# gem install bundler
|
44
|
+
# fi
|
45
|
+
# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
|
46
|
+
# then
|
47
|
+
# bundle install | grep -vE '^Using|Your bundle is complete'
|
48
|
+
# fi
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Marty Kraft
|
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,44 @@
|
|
1
|
+
# WithLocking
|
2
|
+
|
3
|
+
A gem to execute a block of code and ensure that one does not execute other code until the block has completed executing.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'with_locking'
|
10
|
+
|
11
|
+
Or install it yourself as:
|
12
|
+
|
13
|
+
$ gem install with_locking
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
A block of code can be executed like so:
|
18
|
+
|
19
|
+
WithLocking.do { puts "While I'm running other code run through WithLocking cannot run!" }
|
20
|
+
|
21
|
+
Alternatively, run the block of code with an optional name (recommended), that way multiple WithLocking blocks with different names can be invoked without conflicting:
|
22
|
+
|
23
|
+
WithLocking.do(:name => "sleeper") { sleep 60 }
|
24
|
+
WithLocking.do(:name => "sleeper") { puts "I won't execute and will return false because 'sleeper' is still running the first block." }
|
25
|
+
WithLocking.do(:name => "other_name") { puts "But I will run because 'other_name' isn't running!" }
|
26
|
+
# => "But I will run because 'other_name' isn't running!"
|
27
|
+
|
28
|
+
To simply test if a named block is still running without executing a block use the `locked?` method:
|
29
|
+
|
30
|
+
WithLocking.locked?("sleeper")
|
31
|
+
# => true or false
|
32
|
+
|
33
|
+
To raise an exception when the block isn't run (rather than simply returning false), use the `do!` method:
|
34
|
+
|
35
|
+
WithLocking.do!(:name => "sleeper") { puts "Blah" }
|
36
|
+
# => raises an error if 'sleeper' block is still running from before
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
1. Fork it
|
41
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
42
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
43
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
44
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/with_locking.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "with_locking/version"
|
2
|
+
|
3
|
+
module WithLocking
|
4
|
+
|
5
|
+
def self.do(options = {}, &block)
|
6
|
+
raise "No block given" unless block_given?
|
7
|
+
|
8
|
+
name = options[:name] || "locking_service_task"
|
9
|
+
pid_file = "tmp/pids/#{name}.pid"
|
10
|
+
|
11
|
+
return false if File.exists? pid_file
|
12
|
+
|
13
|
+
begin
|
14
|
+
Dir.mkdir("tmp")
|
15
|
+
Dir.mkdir("tmp/pids")
|
16
|
+
rescue Errno::EEXIST => e
|
17
|
+
# ignore this error
|
18
|
+
end
|
19
|
+
|
20
|
+
File.open(pid_file, 'w') { |f| f.puts Process.pid }
|
21
|
+
|
22
|
+
begin
|
23
|
+
block.call
|
24
|
+
ensure
|
25
|
+
File.delete pid_file
|
26
|
+
end
|
27
|
+
return true
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.do!(options = {}, &block)
|
31
|
+
raise "locked process still running" unless self.do(options, &block)
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.locked?(name)
|
35
|
+
File.exists?("tmp/pids/#{name}.pid")
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require_relative '../lib/with_locking.rb'
|
2
|
+
|
3
|
+
describe WithLocking do
|
4
|
+
let(:path) { 'tmp/pids/rspec_test.pid' }
|
5
|
+
let(:file_name) { 'rspec_test' }
|
6
|
+
|
7
|
+
before :each do
|
8
|
+
File.stub(:open)
|
9
|
+
File.stub(:delete)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#do" do
|
13
|
+
|
14
|
+
it "writes a pid file" do
|
15
|
+
File.should_receive(:open).with(path, "w")
|
16
|
+
WithLocking.do(:name => file_name) { }
|
17
|
+
end
|
18
|
+
|
19
|
+
it "executes the given block" do
|
20
|
+
my_test_array = []
|
21
|
+
WithLocking.do { my_test_array << 1 }
|
22
|
+
my_test_array.count.should == 1
|
23
|
+
end
|
24
|
+
|
25
|
+
it "deletes the pid file" do
|
26
|
+
File.should_receive(:delete).with(path)
|
27
|
+
WithLocking.do(:name => file_name) {}
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should not execute the block if the pid file already exists" do
|
31
|
+
File.stub(:exists?).and_return(true)
|
32
|
+
WithLocking.do {}.should == false
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#do!" do
|
38
|
+
|
39
|
+
it "raises an exception if the pid file already exists" do
|
40
|
+
File.stub(:exists?).and_return(true)
|
41
|
+
lambda { WithLocking.do! {} }.should raise_error(RuntimeError, 'locked process still running')
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#locked?" do
|
47
|
+
|
48
|
+
it "checks if the file exists" do
|
49
|
+
File.should_receive(:exists?).with(path)
|
50
|
+
WithLocking.locked?(file_name)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/with_locking/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Marty Kraft"]
|
6
|
+
gem.email = ["marty.kraft@gmail.com"]
|
7
|
+
gem.description = %q{A gem to execute a block of code and ensure that one does not execute other code until the block has completed executing.}
|
8
|
+
gem.summary = %q{Writes a file before executing a given block and then deletes the file when the block has executed. If the file is still there then next time a block is invoked through WithLocking then it is assumed that the first block is still running and the new block isn't executed.}
|
9
|
+
gem.homepage = "https://github.com/mkraft/with_locking"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "with_locking"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = WithLocking::VERSION
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: with_locking
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Marty Kraft
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-13 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A gem to execute a block of code and ensure that one does not execute
|
15
|
+
other code until the block has completed executing.
|
16
|
+
email:
|
17
|
+
- marty.kraft@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- .rvmrc
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- lib/with_locking.rb
|
29
|
+
- lib/with_locking/version.rb
|
30
|
+
- spec/with_locking_spec.rb
|
31
|
+
- with_locking.gemspec
|
32
|
+
homepage: https://github.com/mkraft/with_locking
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.21
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Writes a file before executing a given block and then deletes the file when
|
56
|
+
the block has executed. If the file is still there then next time a block is invoked
|
57
|
+
through WithLocking then it is assumed that the first block is still running and
|
58
|
+
the new block isn't executed.
|
59
|
+
test_files:
|
60
|
+
- spec/with_locking_spec.rb
|