pidlock 0.0.2 → 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/lib/pidlock.rb +10 -2
- data/spec/lib/pidlock_spec.rb +31 -11
- metadata +12 -6
data/lib/pidlock.rb
CHANGED
@@ -1,13 +1,21 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
1
3
|
class Pidlock
|
2
4
|
|
5
|
+
attr_accessor :logger
|
3
6
|
|
4
7
|
class FileLockedException < Exception; end
|
5
8
|
class ProcessRunning < Exception; end
|
9
|
+
|
6
10
|
def initialize(name)
|
11
|
+
dir = File.dirname(name)
|
7
12
|
@name = File.basename(name)
|
8
|
-
@filename = File.join('/', 'var', 'run', @name)
|
13
|
+
@filename = File.expand_path(File.join('/', 'var', 'run', dir, @name))
|
14
|
+
@logger = Logger.new(STDERR)
|
9
15
|
end
|
10
16
|
|
17
|
+
|
18
|
+
|
11
19
|
def lock
|
12
20
|
unless @file
|
13
21
|
unless (File.writable?(File.dirname(@filename)))
|
@@ -18,7 +26,7 @@ class Pidlock
|
|
18
26
|
if (old_process = Sys::ProcTable.ps(old_pid.chomp.to_i))
|
19
27
|
raise ProcessRunning if old_process.comm == File.basename(@name, File.extname(@name))
|
20
28
|
else
|
21
|
-
|
29
|
+
@logger.warn "WARNING: resetting stale lockfile"
|
22
30
|
@file.rewind
|
23
31
|
end
|
24
32
|
end
|
data/spec/lib/pidlock_spec.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Pidlock do
|
4
|
+
let(:logger) { mock(Logger) }
|
4
5
|
before(:each) do
|
5
6
|
@file = stub(File)
|
6
7
|
@file.stub(:flock)
|
@@ -10,38 +11,46 @@ describe Pidlock do
|
|
10
11
|
@file.stub(:gets)
|
11
12
|
File.stub(:open).with('/var/run/my.pid', 'w+').and_return(@file)
|
12
13
|
File.stub(:writable?).with('/var/run').and_return(true)
|
14
|
+
Logger.stub!(:new => logger)
|
13
15
|
end
|
14
|
-
it "
|
16
|
+
it "creates a file with the given name in /var/run" do
|
15
17
|
Pidlock.new('my.pid').lock
|
16
18
|
end
|
17
19
|
context "filehandling" do
|
18
20
|
before(:each) do
|
19
21
|
Process.stub(:pid).and_return(666)
|
22
|
+
logger.stub!(:warn)
|
20
23
|
end
|
21
|
-
it "
|
24
|
+
it "writes the current pid to the file" do
|
22
25
|
@file.should_receive(:write).with(666)
|
23
26
|
Pidlock.new('my.pid').lock
|
24
27
|
end
|
28
|
+
it "uses a directory under /var/run if given" do
|
29
|
+
File.should_receive(:open).with("/var/run/my/my.pid", 'w+').and_return(@file)
|
30
|
+
File.should_receive(:writable?).with("/var/run/my").and_return(true)
|
31
|
+
@file.should_receive(:write).with(666)
|
32
|
+
Pidlock.new('my/my.pid').lock
|
33
|
+
end
|
25
34
|
|
26
|
-
it "
|
35
|
+
it "tries to lock the file" do
|
27
36
|
@file.should_receive(:flock).with( File::LOCK_EX | File::LOCK_NB).and_return(0)
|
28
37
|
Pidlock.new('my.pid').lock
|
29
38
|
end
|
30
39
|
|
31
|
-
it "
|
40
|
+
it "raises if the lock does not succeed" do
|
32
41
|
@file.should_receive(:flock).with( File::LOCK_EX | File::LOCK_NB).and_return(false)
|
33
42
|
lambda {
|
34
43
|
Pidlock.new('my.pid').lock
|
35
44
|
}.should raise_error Pidlock::FileLockedException
|
36
45
|
end
|
37
46
|
|
38
|
-
it "
|
47
|
+
it "checks if the program name matches the id" do
|
39
48
|
@file.should_receive(:gets).and_return('666')
|
40
49
|
ps = stub("ProcTableStruct", :comm => 'test')
|
41
50
|
::Sys::ProcTable.should_receive(:ps).with(666).and_return(ps)
|
42
51
|
Pidlock.new('my.pid').lock
|
43
52
|
end
|
44
|
-
it "
|
53
|
+
it "raises if the program name does match the pid" do
|
45
54
|
@file.should_receive(:gets).and_return('666')
|
46
55
|
ps = stub("ProcTableStruct", :comm => 'my')
|
47
56
|
::Sys::ProcTable.should_receive(:ps).with(666).and_return(ps)
|
@@ -50,21 +59,32 @@ describe Pidlock do
|
|
50
59
|
}.should raise_error Pidlock::ProcessRunning
|
51
60
|
end
|
52
61
|
|
53
|
-
it "
|
62
|
+
it "uses /tmp if /var/run is not writeable" do
|
54
63
|
File.should_receive(:writable?).with('/var/run').and_return(false)
|
55
64
|
File.should_receive(:open).with('/tmp/my.pid', 'w+').and_return(@file)
|
56
65
|
Pidlock.new('my.pid').lock
|
57
|
-
|
58
66
|
end
|
59
|
-
it "
|
67
|
+
it "warns but continue if the file exists but the process name does not" do
|
60
68
|
@file.should_receive(:gets).and_return('667')
|
61
69
|
ps = stub("ProcTableStruct", :comm => 'test')
|
62
70
|
::Sys::ProcTable.should_receive(:ps).with(667).and_return(nil)
|
63
|
-
|
71
|
+
logger.should_receive(:warn).with('WARNING: resetting stale lockfile')
|
64
72
|
@file.should_receive(:rewind)
|
65
73
|
@file.should_receive(:write).with(666)
|
66
74
|
Pidlock.new('my.pid').lock
|
67
|
-
|
75
|
+
end
|
76
|
+
it "should use a logger if injected" do
|
77
|
+
logger2 = mock(Logger)
|
78
|
+
@file.should_receive(:gets).and_return('667')
|
79
|
+
ps = stub("ProcTableStruct", :comm => 'test')
|
80
|
+
::Sys::ProcTable.should_receive(:ps).with(667).and_return(nil)
|
81
|
+
logger.should_not_receive(:warn).with('WARNING: resetting stale lockfile')
|
82
|
+
logger2.should_receive(:warn).with('WARNING: resetting stale lockfile')
|
83
|
+
@file.should_receive(:rewind)
|
84
|
+
@file.should_receive(:write).with(666)
|
85
|
+
pidlock = Pidlock.new('my.pid')
|
86
|
+
pidlock.logger = logger2
|
87
|
+
pidlock.lock
|
68
88
|
end
|
69
89
|
end
|
70
90
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pidlock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,19 +9,24 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-11-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sys-proctable
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 0.9.
|
21
|
+
version: 0.9.2
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.9.2
|
25
30
|
description: Used for locking processes via PID and file (daemon style).
|
26
31
|
email: anton.bangratz@gmail.com
|
27
32
|
executables: []
|
@@ -53,8 +58,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
58
|
version: '0'
|
54
59
|
requirements: []
|
55
60
|
rubyforge_project:
|
56
|
-
rubygems_version: 1.8.
|
61
|
+
rubygems_version: 1.8.24
|
57
62
|
signing_key:
|
58
63
|
specification_version: 3
|
59
64
|
summary: Using PID/file locking for daemons and long running tasks made easy.
|
60
65
|
test_files: []
|
66
|
+
has_rdoc:
|