single_instance 0.1.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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 ICE House AB
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,38 @@
1
+ = SingleInstance
2
+
3
+ SingleInstance creates a system-wide lock that prevents multiple copies of the
4
+ same code to run at the same time. We use it to make sure that `cron` jobs
5
+ do not start if an earlier invocation is still running.
6
+
7
+ # in Rakefile
8
+ task :build_models do |t, args|
9
+ SingleInstance.exclusive_non_blocking(:build_models) do
10
+ # ...
11
+ end
12
+ end
13
+
14
+ Optionally, the block can be allowed to react to the fact that another process
15
+ is running:
16
+
17
+ SingleInstance.exclusive_non_blocking(:build_models) do |blocker|
18
+ if blocker
19
+ puts "Cannot run; pid #{blocker.pid} is running since #{blocker.started_at}"
20
+ else
21
+ # ...
22
+ end
23
+ end
24
+
25
+ == Note on Patches/Pull Requests
26
+
27
+ * Fork the project.
28
+ * Make your feature addition or bug fix.
29
+ * Add tests for it. This is important so I don't break it in a
30
+ future version unintentionally.
31
+ * Commit, do not mess with rakefile, version, or history.
32
+ (if you want to have your own version, that is fine but
33
+ bump version in a commit by itself I can ignore when I pull)
34
+ * Send me a pull request. Bonus points for topic branches.
35
+
36
+ == Copyright
37
+
38
+ Copyright (c) 2010 ICE House AB. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "single_instance"
8
+ gem.summary = %Q{Ruby Gem that makes sure that only a single instance of a code block is running.}
9
+ gem.email = "dev+gemsupport@icehouse.se"
10
+ gem.homepage = "http://github.com/icehouse/single_instance"
11
+ gem.authors = ["David Vrensk", "Magnus Enarsson"]
12
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "single_instance #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,30 @@
1
+ require 'tmpdir'
2
+
3
+ module SingleInstance
4
+ Blocker = Struct.new :pid, :started_at
5
+ class << self
6
+
7
+ def exclusive_non_blocking(name, &block)
8
+ lock_file = File.join(Dir::tmpdir, "single_instance_#{name}.pid")
9
+ File.open(lock_file, "a+") do |file|
10
+ file.rewind
11
+ begin
12
+ if file.flock(File::LOCK_EX|File::LOCK_NB)
13
+ file.truncate(0)
14
+ file.puts $$
15
+ file.puts Time.now.to_i
16
+ file.flush
17
+ yield nil
18
+ elsif block.arity == 1
19
+ pid = file.readline.to_i
20
+ started_at = Time.at(file.readline.to_i)
21
+ yield Blocker.new(pid, started_at)
22
+ end
23
+ ensure
24
+ file.flock(File::LOCK_UN)
25
+ end
26
+ end
27
+ end
28
+
29
+ end
30
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'single_instance'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,47 @@
1
+ require 'helper'
2
+
3
+ class TestSingleInstance < Test::Unit::TestCase
4
+
5
+ context "SingleInstance" do
6
+ should "only allow one block for each name" do
7
+ SingleInstance.exclusive_non_blocking(:test) do
8
+ @block_1 = true
9
+ SingleInstance.exclusive_non_blocking(:test) do
10
+ @block_2 = true
11
+ end
12
+ end
13
+ assert @block_1
14
+ assert @block_2.nil?
15
+ end
16
+
17
+ should "be able to do something intelligent if it didn't get the lock" do
18
+ SingleInstance.exclusive_non_blocking(:test) do
19
+ SingleInstance.exclusive_non_blocking(:test) do |blocker|
20
+ if blocker
21
+ @no_lock = true
22
+ @blocker = blocker
23
+ else
24
+ @block_2 = true
25
+ end
26
+ end
27
+ end
28
+ assert @block_2.nil?
29
+ assert @no_lock
30
+ assert_equal $$, @blocker.pid
31
+ assert @blocker.started_at < Time.now
32
+ assert @blocker.started_at > (Time.now() - 60)
33
+ end
34
+
35
+ should "allow nested blocks with different names" do
36
+ SingleInstance.exclusive_non_blocking(:test1) do
37
+ @block_1 = true
38
+ SingleInstance.exclusive_non_blocking(:test2) do
39
+ @block_2 = true
40
+ end
41
+ end
42
+ assert @block_1
43
+ assert @block_2
44
+ end
45
+ end
46
+
47
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: single_instance
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Vrensk
8
+ - Magnus Enarsson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2010-02-09 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: thoughtbot-shoulda
18
+ type: :development
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ version:
26
+ description:
27
+ email: dev+gemsupport@icehouse.se
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - LICENSE
34
+ - README.rdoc
35
+ files:
36
+ - .document
37
+ - .gitignore
38
+ - LICENSE
39
+ - README.rdoc
40
+ - Rakefile
41
+ - VERSION
42
+ - lib/single_instance.rb
43
+ - test/helper.rb
44
+ - test/test_single_instance.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/icehouse/single_instance
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.5
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Ruby Gem that makes sure that only a single instance of a code block is running.
73
+ test_files:
74
+ - test/helper.rb
75
+ - test/test_single_instance.rb