just_one_lock 0.0.3 → 0.0.4
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/just_one_lock/non_blocking.rb +2 -1
- data/lib/just_one_lock/version.rb +1 -1
- data/lib/just_one_lock.rb +12 -1
- data/spec/just_one_lock/blocking_spec.rb +21 -9
- data/spec/just_one_lock/locking_object.rb +0 -12
- data/spec/just_one_lock/non_blocking_spec.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 96a82fccd7eba5bb06192f7ebd10819e794321b6
|
4
|
+
data.tar.gz: e4792fd094d7a23b6571b638fbb838c9b3a80c36
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b370ad55949836b9c1177dabe8e09c838003ec2195a27edfbf606d8457db8f8670a90f183b6932353b0a611e26ed6df7ed35953888aeca33dfe6e093c6c8c527
|
7
|
+
data.tar.gz: c9708ea092dda6892be69665277c0adace654ed86bd046e9a20fe8704e177b6c592b42f546f8983facaaddeedc8e0530c08a059c2571d32ffcc68a2bf46c56ee
|
data/Gemfile.lock
CHANGED
@@ -21,12 +21,13 @@ module JustOneLock::NonBlocking
|
|
21
21
|
lock_dir,
|
22
22
|
scope,
|
23
23
|
output: JustOneLock::NullStream,
|
24
|
+
delete_files: true,
|
24
25
|
&block
|
25
26
|
)
|
26
27
|
scope_name = scope.gsub(':', '_')
|
27
28
|
lock_path = File.join(lock_dir, scope_name + '.lock')
|
28
29
|
|
29
|
-
was_executed = filelock(lock_path, &block)
|
30
|
+
was_executed = filelock(lock_path, delete_files: delete_files, &block)
|
30
31
|
|
31
32
|
unless was_executed
|
32
33
|
output.puts "Another process <#{scope}> already is running"
|
data/lib/just_one_lock.rb
CHANGED
@@ -12,11 +12,22 @@ module JustOneLock
|
|
12
12
|
@files = {}
|
13
13
|
|
14
14
|
def self.delete_unlocked_files
|
15
|
+
paths_to_delete = []
|
16
|
+
|
15
17
|
@files.each do |path, f|
|
16
18
|
if File.exists?(path) && f.closed?
|
17
|
-
|
19
|
+
paths_to_delete << path
|
18
20
|
end
|
19
21
|
end
|
22
|
+
|
23
|
+
paths_to_delete.each do |path|
|
24
|
+
File.delete(path)
|
25
|
+
@files.delete(path)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.lock_paths
|
30
|
+
@files.keys
|
20
31
|
end
|
21
32
|
|
22
33
|
private
|
@@ -40,7 +40,7 @@ describe JustOneLock::Blocking do
|
|
40
40
|
it 'should work for multiple processes' do
|
41
41
|
write('/tmp/number.txt', '0')
|
42
42
|
|
43
|
-
parallel_forks(6
|
43
|
+
parallel_forks(6) do
|
44
44
|
number = File.read('/tmp/number.txt').to_i
|
45
45
|
sleep(JustOneLock::Blocking::DEFAULT_TIMEOUT / 100)
|
46
46
|
write('/tmp/number.txt', (number + 7).to_s)
|
@@ -55,7 +55,7 @@ describe JustOneLock::Blocking do
|
|
55
55
|
write('/tmp/number.txt', '0')
|
56
56
|
|
57
57
|
FORKS_NUMBER = 100
|
58
|
-
parallel_forks(FORKS_NUMBER
|
58
|
+
parallel_forks(FORKS_NUMBER) do
|
59
59
|
number = File.read('/tmp/number.txt').to_i
|
60
60
|
write('/tmp/number.txt', (number + 1).to_s)
|
61
61
|
end
|
@@ -64,17 +64,29 @@ describe JustOneLock::Blocking do
|
|
64
64
|
|
65
65
|
expect(number).to eq(FORKS_NUMBER)
|
66
66
|
end
|
67
|
+
end
|
67
68
|
|
68
|
-
|
69
|
-
|
69
|
+
it 'runs in parallel without race condition' do
|
70
|
+
answer = 0
|
70
71
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
72
|
+
parallel(2) do
|
73
|
+
value = answer
|
74
|
+
sleep(JustOneLock::Blocking::DEFAULT_TIMEOUT / 2)
|
75
|
+
answer = value + 21
|
76
|
+
end
|
75
77
|
|
76
|
-
|
78
|
+
expect(answer).to eq(42)
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'handles high amount of concurrent tasks' do
|
82
|
+
answer = 0
|
83
|
+
|
84
|
+
parallel(100) do
|
85
|
+
value = answer
|
86
|
+
answer = value + 1
|
77
87
|
end
|
88
|
+
|
89
|
+
expect(answer).to eq(100)
|
78
90
|
end
|
79
91
|
end
|
80
92
|
|
@@ -27,18 +27,6 @@ shared_examples 'a locking object' do
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
it 'runs in parallel without race condition' do
|
31
|
-
answer = 0
|
32
|
-
|
33
|
-
parallel(2) do
|
34
|
-
value = answer
|
35
|
-
sleep(JustOneLock::Blocking::DEFAULT_TIMEOUT / 2)
|
36
|
-
answer = value + 21
|
37
|
-
end
|
38
|
-
|
39
|
-
expect(answer).to eq(42)
|
40
|
-
end
|
41
|
-
|
42
30
|
it 'creates lock file on disk during block execution' do
|
43
31
|
lockpath = Tempfile.new(['sample', '.lock']).path
|
44
32
|
parallel(2, lockpath: lockpath) do
|
@@ -11,7 +11,7 @@ describe JustOneLock::NonBlocking do
|
|
11
11
|
dir, scope = dir_and_scope(lockpath)
|
12
12
|
(1..n).map do
|
13
13
|
Thread.new do
|
14
|
-
JustOneLock::
|
14
|
+
JustOneLock::NonBlocking.prevent_multiple_executions(dir, scope, delete_files: false, &block)
|
15
15
|
end
|
16
16
|
end.map(&:join)
|
17
17
|
end
|
@@ -25,7 +25,7 @@ describe JustOneLock::NonBlocking do
|
|
25
25
|
|
26
26
|
(1..n).map do
|
27
27
|
fork {
|
28
|
-
JustOneLock::
|
28
|
+
JustOneLock::NonBlocking.prevent_multiple_executions(dir, scope, delete_files: false, &block)
|
29
29
|
}
|
30
30
|
end.map do |pid|
|
31
31
|
Process.waitpid(pid)
|