pidlock 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2471de94b25e794aa754db7b6ce0f31b6600b1f0
4
+ data.tar.gz: a3c7c1d857ea226924f16ac18075b2b8551de2e8
5
+ SHA512:
6
+ metadata.gz: 88dd41c285f7734d26bd7b9a0e8dfffce7416a7335fd6cb187bac1f104416e319e9d5dd37ae822a64c4a69925d227b7e091a01a0c00f0a7fd090e9739f034d7d
7
+ data.tar.gz: 951f0bcf0c69b2aaeabef0077adafb794dfe0bce94eb2a0f3711e1cedfdf7b5f372a0da9a6b23fd77345d2e868671e11fd0b66ad71a58cb01d319b7034f2fe3a
data/README.mkd CHANGED
@@ -1,6 +1,6 @@
1
1
  # Pidlock
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/abangratz/pidlock.png)](http://travis-ci.org/abangratz/pidlock)
3
+ [![Build Status](https://travis-ci.org/abangratz/pidlock.png?branch=master)](https://travis-ci.org/abangratz/pidlock)
4
4
 
5
5
  This is a ruby library for using pid-file based locking of programs/daemons to prevent multiple parallel running tasks.
6
6
 
@@ -13,12 +13,24 @@ The gem ``sys/proctable`` is required.
13
13
  ```ruby
14
14
  require 'sys/proctable'
15
15
 
16
- Pidlock.new('my.pid').lock # tries to create a lock file '/var/run/my.pid';
17
- # raises an error if the file is locked or a program exists that
18
- # has the same basename and pid (here: 'my')
16
+ pidlock = Pidlock.new('my.pid') # Instantiates a lock
17
+ pidlock.lock # tries to create a lock file '/var/run/my.pid';
18
+ # raises an error if the file is locked or a program exists that
19
+ # has the same basename and pid (here: 'my')
19
20
  ...
21
+ pidlock.unlock # closes and removes the file
20
22
  ```
21
23
 
22
24
  ## TODO
23
25
 
24
- Lots still. First release.
26
+ * test unlocking in projects.
27
+
28
+
29
+ ## LICENSE
30
+
31
+ [ISC License](LICENSE)
32
+
33
+
34
+ ## CONTRIBUTORS
35
+
36
+ - Greg Whiteley (https:/github/com/whitty)
data/lib/pidlock.rb CHANGED
@@ -13,6 +13,7 @@ class Pidlock
13
13
  @name = File.basename(name)
14
14
  @filename = File.expand_path(File.join('/', 'var', 'run', dir, @name))
15
15
  @logger = Logger.new(STDERR)
16
+ @file = nil
16
17
  end
17
18
 
18
19
 
@@ -28,10 +29,11 @@ class Pidlock
28
29
  raise ProcessRunning if old_process.comm == File.basename(@name, File.extname(@name))
29
30
  else
30
31
  @logger.warn "WARNING: resetting stale lockfile"
31
- @file.rewind
32
32
  end
33
33
  end
34
34
  @file.flock(File::LOCK_EX | File::LOCK_NB) or raise FileLockedException
35
+ @file.rewind
36
+ @file.truncate(0)
35
37
  @file.write Process.pid
36
38
  @file.flush
37
39
  end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Pidlock", :filesystem => true do
4
+ let(:logger) { mock(Logger) }
5
+
6
+ context "filesystem testing" do
7
+ before(:each) do
8
+ Logger.stub!(:new => logger)
9
+
10
+ path = Pathname(File.writable?('/var/run') ? '/var/run' : '/tmp')
11
+ @pid_name = 'my.pid'
12
+ @pid_file = path + @pid_name
13
+ File.unlink(@pid_file) if @pid_file.exist?
14
+
15
+ Process.stub(:pid).and_return(666)
16
+ logger.stub!(:warn)
17
+ end
18
+
19
+ after(:each) do
20
+ File.unlink(@pid_file) if @pid_file.exist?
21
+ end
22
+
23
+ it "enters the correct PID in the pidfile" do
24
+
25
+ Pidlock.new(@pid_name).lock
26
+ @pid_file.should exist
27
+
28
+ File.read(@pid_file).should eq('666')
29
+ end
30
+
31
+ it "cleans up the PID in the pidfile" do
32
+
33
+ pidlock = Pidlock.new(@pid_name)
34
+ pidlock.lock
35
+ @pid_file.should exist
36
+
37
+ File.read(@pid_file).should eq('666')
38
+ pidlock.unlock
39
+
40
+ @pid_file.should_not exist
41
+ end
42
+
43
+ it "enters the correct PID in the pidfile when the pidfile is stale" do
44
+ # start with a stale PID in the file
45
+ open(@pid_file, "w") {|f| f.write('1111') }
46
+ File.read(@pid_file).should eq('1111')
47
+ ::Sys::ProcTable.should_receive(:ps).with(1111).and_return(nil)
48
+ logger.should_receive(:warn).with('WARNING: resetting stale lockfile')
49
+
50
+ Pidlock.new(@pid_name).lock
51
+ @pid_file.should exist
52
+
53
+ File.read(@pid_file).should eq('666')
54
+ end
55
+
56
+ it "enters the correct PID in the pidfile when the pid matches a different process" do
57
+ # start with a stale PID in the file
58
+ open(@pid_file, "w") {|f| f.write('1111') }
59
+ File.read(@pid_file).should eq('1111')
60
+ ps = stub("ProcTableStruct", :comm => 'other')
61
+ ::Sys::ProcTable.should_receive(:ps).with(1111).and_return(ps)
62
+
63
+ Pidlock.new(@pid_name).lock
64
+ @pid_file.should exist
65
+
66
+ File.read(@pid_file).should eq('666')
67
+ end
68
+
69
+ end
70
+
71
+ end
@@ -9,6 +9,8 @@ describe Pidlock do
9
9
  @file.stub(:flock => 0)
10
10
  @file.stub(:flush)
11
11
  @file.stub(:gets)
12
+ @file.stub(:rewind)
13
+ @file.stub(:truncate)
12
14
  File.stub(:open).with('/var/run/my.pid', File::RDWR|File::CREAT, 0600).and_return(@file)
13
15
  File.stub(:writable?).with('/var/run').and_return(true)
14
16
  Logger.stub!(:new => logger)
@@ -66,21 +68,21 @@ describe Pidlock do
66
68
  end
67
69
  it "warns but continue if the file exists but the process name does not" do
68
70
  @file.should_receive(:gets).and_return('667')
69
- ps = stub("ProcTableStruct", :comm => 'test')
70
71
  ::Sys::ProcTable.should_receive(:ps).with(667).and_return(nil)
71
72
  logger.should_receive(:warn).with('WARNING: resetting stale lockfile')
72
73
  @file.should_receive(:rewind)
74
+ @file.should_receive(:truncate).with(0)
73
75
  @file.should_receive(:write).with(666)
74
76
  Pidlock.new('my.pid').lock
75
77
  end
76
78
  it "should use a logger if injected" do
77
79
  logger2 = mock(Logger)
78
80
  @file.should_receive(:gets).and_return('667')
79
- ps = stub("ProcTableStruct", :comm => 'test')
80
81
  ::Sys::ProcTable.should_receive(:ps).with(667).and_return(nil)
81
82
  logger.should_not_receive(:warn).with('WARNING: resetting stale lockfile')
82
83
  logger2.should_receive(:warn).with('WARNING: resetting stale lockfile')
83
84
  @file.should_receive(:rewind)
85
+ @file.should_receive(:truncate).with(0)
84
86
  @file.should_receive(:write).with(666)
85
87
  pidlock = Pidlock.new('my.pid')
86
88
  pidlock.logger = logger2
@@ -88,10 +90,10 @@ describe Pidlock do
88
90
  end
89
91
  it "should close and remove the file if unlocked" do
90
92
  @file.should_receive(:gets).and_return('667')
91
- ps = stub("ProcTableStruct", :comm => 'test')
92
93
  ::Sys::ProcTable.should_receive(:ps).with(667).and_return(nil)
93
94
  logger.should_receive(:warn).with('WARNING: resetting stale lockfile')
94
95
  @file.should_receive(:rewind)
96
+ @file.should_receive(:truncate).with(0)
95
97
  @file.should_receive(:write).with(666)
96
98
  lock = Pidlock.new('my.pid')
97
99
  lock.lock
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pidlock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
5
- prerelease:
4
+ version: 0.2.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Anton Bangratz
8
+ - Greg Whiteley
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
@@ -14,52 +14,51 @@ dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sys-proctable
16
16
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
17
  requirements:
19
- - - ~>
18
+ - - "~>"
20
19
  - !ruby/object:Gem::Version
21
20
  version: 0.9.2
22
21
  type: :runtime
23
22
  prerelease: false
24
23
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
24
  requirements:
27
- - - ~>
25
+ - - "~>"
28
26
  - !ruby/object:Gem::Version
29
27
  version: 0.9.2
30
28
  description: Used for locking processes via PID and file (daemon style).
31
- email: anton.bangratz@gmail.com
29
+ email:
30
+ - anton.bangratz@gmail.com
32
31
  executables: []
33
32
  extensions: []
34
33
  extra_rdoc_files:
35
34
  - README.mkd
36
35
  files:
36
+ - README.mkd
37
37
  - lib/pidlock.rb
38
+ - spec/lib/pidlock_filesystem_spec.rb
38
39
  - spec/lib/pidlock_spec.rb
39
40
  - spec/spec_helper.rb
40
- - README.mkd
41
41
  homepage: https://github.com/abangratz/pidlock
42
42
  licenses: []
43
+ metadata: {}
43
44
  post_install_message:
44
45
  rdoc_options: []
45
46
  require_paths:
46
47
  - lib
47
48
  required_ruby_version: !ruby/object:Gem::Requirement
48
- none: false
49
49
  requirements:
50
- - - ! '>='
50
+ - - ">="
51
51
  - !ruby/object:Gem::Version
52
52
  version: '0'
53
53
  required_rubygems_version: !ruby/object:Gem::Requirement
54
- none: false
55
54
  requirements:
56
- - - ! '>='
55
+ - - ">="
57
56
  - !ruby/object:Gem::Version
58
57
  version: '0'
59
58
  requirements: []
60
59
  rubyforge_project:
61
- rubygems_version: 1.8.25
60
+ rubygems_version: 2.2.2
62
61
  signing_key:
63
- specification_version: 3
62
+ specification_version: 4
64
63
  summary: Using PID/file locking for daemons and long running tasks made easy.
65
64
  test_files: []