hq-tools 0.5.0 → 0.6.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/hq/tools/lock.rb +80 -0
- data/spec/hq/tools/lock-spec.rb +33 -0
- metadata +4 -1
@@ -0,0 +1,80 @@
|
|
1
|
+
module HQ
|
2
|
+
module Tools
|
3
|
+
module Lock
|
4
|
+
|
5
|
+
def self.lock_simple filename
|
6
|
+
|
7
|
+
# create a lock file, will raise EEXIST if it exists
|
8
|
+
|
9
|
+
mode = File::WRONLY | File::CREAT | File::EXCL
|
10
|
+
File.open filename, mode do |file|
|
11
|
+
|
12
|
+
# flock it
|
13
|
+
|
14
|
+
file.flock File::LOCK_EX | File::LOCK_NB \
|
15
|
+
or raise "Cannot obtain lock"
|
16
|
+
|
17
|
+
begin
|
18
|
+
|
19
|
+
# write our pid
|
20
|
+
|
21
|
+
file.puts $$
|
22
|
+
file.flush
|
23
|
+
|
24
|
+
# yield
|
25
|
+
|
26
|
+
yield
|
27
|
+
|
28
|
+
ensure
|
29
|
+
|
30
|
+
# delete lock file
|
31
|
+
|
32
|
+
File.unlink filename
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.lock_remove filename
|
41
|
+
|
42
|
+
# open existing lock file
|
43
|
+
|
44
|
+
File.open filename, File::WRONLY do |file|
|
45
|
+
|
46
|
+
# lock it
|
47
|
+
|
48
|
+
file.flock File::LOCK_EX | File::LOCK_NB \
|
49
|
+
or raise "Cannot obtain lock"
|
50
|
+
|
51
|
+
# delete lock file
|
52
|
+
|
53
|
+
File.unlink filename
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.lock filename, &proc
|
60
|
+
|
61
|
+
while true
|
62
|
+
|
63
|
+
begin
|
64
|
+
|
65
|
+
lock_simple filename, &proc
|
66
|
+
return
|
67
|
+
|
68
|
+
rescue Errno::EEXIST => e
|
69
|
+
|
70
|
+
lock_remove filename
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "tmpdir"
|
2
|
+
|
3
|
+
require "hq/tools/lock"
|
4
|
+
|
5
|
+
module HQ
|
6
|
+
module Tools
|
7
|
+
describe Lock do
|
8
|
+
|
9
|
+
context "#lock" do
|
10
|
+
|
11
|
+
before do
|
12
|
+
@old_dir = Dir.pwd
|
13
|
+
@temp_dir = Dir.mktmpdir
|
14
|
+
Dir.chdir @temp_dir
|
15
|
+
end
|
16
|
+
|
17
|
+
it "calls the provided block" do
|
18
|
+
called = false
|
19
|
+
subject.lock "lockfile" do
|
20
|
+
called = true
|
21
|
+
end
|
22
|
+
called.should == true
|
23
|
+
end
|
24
|
+
|
25
|
+
after do
|
26
|
+
Dir.chdir @old_dir
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hq-tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -100,8 +100,10 @@ extra_rdoc_files: []
|
|
100
100
|
files:
|
101
101
|
- lib/hq/tools/getopt.rb
|
102
102
|
- lib/hq/tools/cron.rb
|
103
|
+
- lib/hq/tools/lock.rb
|
103
104
|
- lib/hq/tools/escape.rb
|
104
105
|
- lib/hq/tools/check-script.rb
|
106
|
+
- spec/hq/tools/lock-spec.rb
|
105
107
|
- spec/hq/tools/getopt-spec.rb
|
106
108
|
- spec/hq/tools/cron-spec.rb
|
107
109
|
- spec/hq/tools/check-script-spec.rb
|
@@ -130,6 +132,7 @@ signing_key:
|
|
130
132
|
specification_version: 3
|
131
133
|
summary: HQ tools
|
132
134
|
test_files:
|
135
|
+
- spec/hq/tools/lock-spec.rb
|
133
136
|
- spec/hq/tools/getopt-spec.rb
|
134
137
|
- spec/hq/tools/cron-spec.rb
|
135
138
|
- spec/hq/tools/check-script-spec.rb
|