single_instance 0.1.0 → 0.2.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 CHANGED
@@ -19,3 +19,6 @@ rdoc
19
19
  pkg
20
20
 
21
21
  ## PROJECT::SPECIFIC
22
+ pkg/*
23
+ *.gem
24
+ .bundle
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.2-p0@single_instance --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # Specify your gem's dependencies in single_instance.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,17 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ single_instance (0.1.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ thoughtbot-shoulda (2.11.1)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (>= 1.0.0)
16
+ single_instance!
17
+ thoughtbot-shoulda
data/README.rdoc CHANGED
@@ -22,6 +22,15 @@ is running:
22
22
  end
23
23
  end
24
24
 
25
+ Run without a block, it creates a lock that lasts as long as the process is running:
26
+
27
+ # in Rakefile
28
+ task :all => [:exclusive, :this, :that, :whatever]
29
+ task :exclusive do
30
+ SingleInstance.exclusive_non_blocking(:build_models) || exit
31
+ end
32
+
33
+
25
34
  == Note on Patches/Pull Requests
26
35
 
27
36
  * Fork the project.
@@ -35,4 +44,4 @@ is running:
35
44
 
36
45
  == Copyright
37
46
 
38
- Copyright (c) 2010 ICE House AB. See LICENSE for details.
47
+ Copyright (c) 2010-11 ICE House AB. See LICENSE for details.
data/Rakefile CHANGED
@@ -1,21 +1,5 @@
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
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
19
3
 
20
4
  require 'rake/testtask'
21
5
  Rake::TestTask.new(:test) do |test|
@@ -37,8 +21,8 @@ rescue LoadError
37
21
  end
38
22
  end
39
23
 
40
- task :test => :check_dependencies
41
-
24
+ # task :test => :check_dependencies
25
+ #
42
26
  task :default => :test
43
27
 
44
28
  require 'rake/rdoctask'
@@ -0,0 +1,3 @@
1
+ module SingleInstance
2
+ VERSION = "0.2.0"
3
+ end
@@ -2,27 +2,33 @@ require 'tmpdir'
2
2
 
3
3
  module SingleInstance
4
4
  Blocker = Struct.new :pid, :started_at
5
+
5
6
  class << self
6
7
 
7
8
  def exclusive_non_blocking(name, &block)
8
9
  lock_file = File.join(Dir::tmpdir, "single_instance_#{name}.pid")
9
- File.open(lock_file, "a+") do |file|
10
+ begin
11
+ file = File.open(lock_file, "a+")
10
12
  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
13
+ if file.flock(File::LOCK_EX|File::LOCK_NB)
14
+ file.truncate(0)
15
+ file.puts $$
16
+ file.puts Time.now.to_i
17
+ file.flush
18
+ if block_given?
17
19
  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)
20
+ else
21
+ (@lock_files ||= []) << file
22
22
  end
23
- ensure
24
- file.flock(File::LOCK_UN)
23
+ elsif block_given? && block.arity == 1
24
+ pid = file.readline.to_i
25
+ started_at = Time.at(file.readline.to_i)
26
+ yield Blocker.new(pid, started_at)
27
+ else
28
+ false
25
29
  end
30
+ ensure
31
+ file.close if block_given?
26
32
  end
27
33
  end
28
34
 
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/single_instance/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "single_instance"
6
+ s.version = SingleInstance::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["David Vrensk", "Magnus Enarsson"]
9
+ s.email = ["dev+gemsupport@icehouse.se"]
10
+ s.homepage = "http://github.com/icehouse/single_instance"
11
+ s.summary = %Q{Ruby Gem that makes sure that only a single instance of a code block or process is running.}
12
+ s.description = "SingleInstance uses an exclusive file lock to allow or block execution of code."
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.rubyforge_project = "single_instance"
16
+
17
+ s.add_development_dependency "bundler", ">= 1.0.0"
18
+ s.add_development_dependency "thoughtbot-shoulda", ">= 0"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
22
+ s.require_path = 'lib'
23
+ end
@@ -42,6 +42,13 @@ class TestSingleInstance < Test::Unit::TestCase
42
42
  assert @block_1
43
43
  assert @block_2
44
44
  end
45
+
46
+ context "without a block" do
47
+ should "create a lock that lasts until the process terminates" do
48
+ assert SingleInstance.exclusive_non_blocking(:blockless)
49
+ assert ! SingleInstance.exclusive_non_blocking(:blockless)
50
+ end
51
+ end
45
52
  end
46
53
 
47
54
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: single_instance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - David Vrensk
@@ -10,36 +15,58 @@ autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
17
 
13
- date: 2010-02-09 00:00:00 +01:00
18
+ date: 2011-05-02 00:00:00 +02:00
14
19
  default_executable:
15
20
  dependencies:
16
21
  - !ruby/object:Gem::Dependency
17
- name: thoughtbot-shoulda
22
+ name: bundler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 1
31
+ - 0
32
+ - 0
33
+ version: 1.0.0
18
34
  type: :development
19
- version_requirement:
20
- version_requirements: !ruby/object:Gem::Requirement
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: thoughtbot-shoulda
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
21
41
  requirements:
22
42
  - - ">="
23
43
  - !ruby/object:Gem::Version
44
+ segments:
45
+ - 0
24
46
  version: "0"
25
- version:
26
- description:
27
- email: dev+gemsupport@icehouse.se
47
+ type: :development
48
+ version_requirements: *id002
49
+ description: SingleInstance uses an exclusive file lock to allow or block execution of code.
50
+ email:
51
+ - dev+gemsupport@icehouse.se
28
52
  executables: []
29
53
 
30
54
  extensions: []
31
55
 
32
- extra_rdoc_files:
33
- - LICENSE
34
- - README.rdoc
56
+ extra_rdoc_files: []
57
+
35
58
  files:
36
59
  - .document
37
60
  - .gitignore
61
+ - .rvmrc
62
+ - Gemfile
63
+ - Gemfile.lock
38
64
  - LICENSE
39
65
  - README.rdoc
40
66
  - Rakefile
41
- - VERSION
42
67
  - lib/single_instance.rb
68
+ - lib/single_instance/version.rb
69
+ - single_instance.gemspec
43
70
  - test/helper.rb
44
71
  - test/test_single_instance.rb
45
72
  has_rdoc: true
@@ -47,29 +74,34 @@ homepage: http://github.com/icehouse/single_instance
47
74
  licenses: []
48
75
 
49
76
  post_install_message:
50
- rdoc_options:
51
- - --charset=UTF-8
77
+ rdoc_options: []
78
+
52
79
  require_paths:
53
80
  - lib
54
81
  required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
55
83
  requirements:
56
84
  - - ">="
57
85
  - !ruby/object:Gem::Version
86
+ segments:
87
+ - 0
58
88
  version: "0"
59
- version:
60
89
  required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
61
91
  requirements:
62
92
  - - ">="
63
93
  - !ruby/object:Gem::Version
64
- version: "0"
65
- version:
94
+ segments:
95
+ - 1
96
+ - 3
97
+ - 6
98
+ version: 1.3.6
66
99
  requirements: []
67
100
 
68
- rubyforge_project:
69
- rubygems_version: 1.3.5
101
+ rubyforge_project: single_instance
102
+ rubygems_version: 1.3.7
70
103
  signing_key:
71
104
  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
105
+ summary: Ruby Gem that makes sure that only a single instance of a code block or process is running.
106
+ test_files: []
107
+
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.1.0