just_one_lock 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/just_one_lock/blocking.rb +3 -4
- data/lib/just_one_lock/non_blocking.rb +4 -4
- data/lib/just_one_lock/version.rb +1 -1
- data/lib/just_one_lock.rb +8 -0
- data/spec/just_one_lock/locking_object.rb +1 -1
- 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: 3e557230f996c20dc4ce8d926ce91ec498c69b16
|
4
|
+
data.tar.gz: 13a29e9beafc7e7bf698e354209a34379c32ae13
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0aa440f661bc1a5dfd94399b34b163b262f2d6dada1c816e8eaf73c63c055f332a56a0cb31382c976c92faf34738d97931c3d6824a30bd23b3d6fe76020fbf3d
|
7
|
+
data.tar.gz: fe426d149e1881bde672fd6d32b454f2641687ccf9421d8172661dc391ef2b3e1f2589d9c583059cf63e61207757d781d8e7a38009f0a1e27f32387e7a4be763
|
data/Gemfile.lock
CHANGED
@@ -2,7 +2,6 @@ require 'timeout'
|
|
2
2
|
|
3
3
|
module JustOneLock::Blocking
|
4
4
|
DEFAULT_TIMEOUT = 1
|
5
|
-
class AlreadyLocked < StandardError; end
|
6
5
|
|
7
6
|
def self.filelock(
|
8
7
|
lockname,
|
@@ -12,7 +11,7 @@ module JustOneLock::Blocking
|
|
12
11
|
)
|
13
12
|
result = nil
|
14
13
|
File.open(lockname, File::RDWR|File::CREAT, 0644) do |f|
|
15
|
-
Timeout::timeout(timeout, JustOneLock::
|
14
|
+
Timeout::timeout(timeout, JustOneLock::AlreadyLocked) { f.flock(File::LOCK_EX) }
|
16
15
|
|
17
16
|
result = JustOneLock.run(f, lockname, &block)
|
18
17
|
end
|
@@ -34,8 +33,8 @@ module JustOneLock::Blocking
|
|
34
33
|
|
35
34
|
begin
|
36
35
|
return filelock(lock_path, timeout: timeout, delete_files: delete_files, &block)
|
37
|
-
rescue JustOneLock::
|
38
|
-
output
|
36
|
+
rescue JustOneLock::AlreadyLocked => e
|
37
|
+
JustOneLock.already_locked(output, scope)
|
39
38
|
end
|
40
39
|
end
|
41
40
|
end
|
@@ -27,10 +27,10 @@ module JustOneLock::NonBlocking
|
|
27
27
|
scope_name = scope.gsub(':', '_')
|
28
28
|
lock_path = File.join(lock_dir, scope_name + '.lock')
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
output
|
30
|
+
begin
|
31
|
+
return filelock(lock_path, delete_files: delete_files, &block)
|
32
|
+
rescue JustOneLock::AlreadyLocked => e
|
33
|
+
JustOneLock.already_locked(output, scope)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
data/lib/just_one_lock.rb
CHANGED
@@ -3,6 +3,8 @@ require 'just_one_lock/blocking'
|
|
3
3
|
require 'just_one_lock/non_blocking'
|
4
4
|
|
5
5
|
module JustOneLock
|
6
|
+
class AlreadyLocked < StandardError; end
|
7
|
+
|
6
8
|
class NullStream
|
7
9
|
class << self
|
8
10
|
def puts(str); end
|
@@ -30,6 +32,12 @@ module JustOneLock
|
|
30
32
|
@files.keys
|
31
33
|
end
|
32
34
|
|
35
|
+
def self.already_locked(output, scope)
|
36
|
+
msg = "Another process <#{scope}> already is running"
|
37
|
+
output.puts msg
|
38
|
+
raise JustOneLock::AlreadyLocked, msg
|
39
|
+
end
|
40
|
+
|
33
41
|
private
|
34
42
|
|
35
43
|
def self.write_pid(f)
|
@@ -77,7 +77,7 @@ shared_examples 'a locking object' do
|
|
77
77
|
JustOneLock::Blocking.filelock lockpath, timeout: 0.001 do
|
78
78
|
answer = 0
|
79
79
|
end
|
80
|
-
end.to raise_error(JustOneLock::
|
80
|
+
end.to raise_error(JustOneLock::AlreadyLocked)
|
81
81
|
|
82
82
|
expect(answer).to eq(42)
|
83
83
|
end
|