cleverua-lockfile 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
- This ruby gem allows you create a lock file on disk right before it yields given code block. It would not allow to yield the block again while the first one finished (ie while lock file exists). This gem currently works on UNIX operational systems.
7
+ This ruby gem allows you create a lock file on disk right before it yields given code block. It would not allow to yield the block again while the first one finished (ie while lock file exists). This gem currently works on UNIX operating systems. This gem should not be used on NFS (http://lwn.net/Articles/251004/)
8
8
 
9
9
  == USAGE EXAMPLE
10
10
 
@@ -19,15 +19,15 @@ This ruby gem allows you create a lock file on disk right before it yields given
19
19
  === TO FORCE STOP
20
20
  include Lockfile
21
21
 
22
- Lockfile.force_stop(LOCK_FILE_PATH)
22
+ Lockfile.release_lock(LOCK_FILE_PATH)
23
23
 
24
24
  == REQUIREMENTS:
25
25
 
26
- * a UNIX operational system
26
+ * a UNIX operating system
27
27
 
28
28
  == INSTALL:
29
29
 
30
- sudo gem install lockfile
30
+ sudo gem install cleverua-lockfile
31
31
 
32
32
  == LICENSE:
33
33
 
data/Rakefile CHANGED
@@ -10,6 +10,7 @@ begin
10
10
  gem.email = "pavlo@cleverua.com"
11
11
  gem.homepage = "http://github.com/cleverua/lockfile"
12
12
  gem.authors = ["pavlo"]
13
+ gem.version = "1.1.0"
13
14
  #gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
16
  end
@@ -7,10 +7,8 @@ module Lockfile
7
7
  end
8
8
 
9
9
  def with_lock_file(lock_file_path, write_pid = true)
10
- return false if has_lock?(lock_file_path)
11
-
10
+ return false unless obtain_lock(lock_file_path, write_pid)
12
11
  begin
13
- obtain_lock(lock_file_path, write_pid)
14
12
  yield
15
13
  ensure
16
14
  release_lock(lock_file_path)
@@ -19,9 +17,12 @@ module Lockfile
19
17
 
20
18
  private
21
19
  def obtain_lock(lf, write_pid)
22
- open(lf, "w+") do |o|
20
+ File.open(lf, File::CREAT | File::EXCL | File::WRONLY) do |o|
23
21
  o.write(Process.pid) if write_pid
24
22
  end
23
+ return true
24
+ rescue
25
+ return false
25
26
  end
26
27
 
27
28
  def release_lock(lf)
@@ -22,7 +22,21 @@ class TestLockfile < Test::Unit::TestCase
22
22
  end
23
23
  end
24
24
 
25
+ class Lockfile::File < ::File
26
+ #slow open down once to demonstrate race condition
27
+ def self.open(*args)
28
+ unless @no_sleep
29
+ @no_sleep = true
30
+ sleep(1)
31
+ end
32
+ super
33
+ end
34
+ end
35
+
36
+
25
37
  should "not allow to launch other processes while the previous one has lock" do
38
+ Lockfile.ensure_no_lock_file_exists(LOCK_FILE)
39
+
26
40
  p1 = TestProcess.new
27
41
  p2 = TestProcess.new
28
42
  p3 = TestProcess.new
@@ -35,8 +49,7 @@ class TestLockfile < Test::Unit::TestCase
35
49
  t2.join
36
50
  t3.join
37
51
 
38
- assert p1.launched?
39
- assert !p2.launched?
40
- assert !p3.launched?
52
+ #only one should launch
53
+ assert_equal 1, [p1, p2, p3].select{|i| i.launched? }.size
41
54
  end
42
55
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cleverua-lockfile
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 1.0.0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - pavlo
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-06 00:00:00 +03:00
18
+ date: 2010-11-29 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -30,7 +30,6 @@ extra_rdoc_files:
30
30
  - README.rdoc
31
31
  files:
32
32
  - .document
33
- - .gitignore
34
33
  - LICENSE
35
34
  - README.rdoc
36
35
  - Rakefile
@@ -43,8 +42,8 @@ homepage: http://github.com/cleverua/lockfile
43
42
  licenses: []
44
43
 
45
44
  post_install_message:
46
- rdoc_options:
47
- - --charset=UTF-8
45
+ rdoc_options: []
46
+
48
47
  require_paths:
49
48
  - lib
50
49
  required_ruby_version: !ruby/object:Gem::Requirement
data/.gitignore DELETED
@@ -1,21 +0,0 @@
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