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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef790c43dd30dabc1e75e662d8e042ff0a49c08f
4
- data.tar.gz: 77ef1490b78280b803160512b96beaa8aeea376f
3
+ metadata.gz: 96a82fccd7eba5bb06192f7ebd10819e794321b6
4
+ data.tar.gz: e4792fd094d7a23b6571b638fbb838c9b3a80c36
5
5
  SHA512:
6
- metadata.gz: 7f33614e6f935dfbb3040e64f09590342988ebe8566ebaca24c315d180bd73040723d165f9ede1c1804fedde2285c4d9b257ac9924ddbc84ae5d2cf94db8f477
7
- data.tar.gz: 68a3d589c4545bdcba5e5467861d8120687e69b1b7438e6b0c5f6b86f7f0854d3674c68236f824da86e7e9f60c1151dad914100ff510beb5e275b20ea1209ee8
6
+ metadata.gz: b370ad55949836b9c1177dabe8e09c838003ec2195a27edfbf606d8457db8f8670a90f183b6932353b0a611e26ed6df7ed35953888aeca33dfe6e093c6c8c527
7
+ data.tar.gz: c9708ea092dda6892be69665277c0adace654ed86bd046e9a20fe8704e177b6c592b42f546f8983facaaddeedc8e0530c08a059c2571d32ffcc68a2bf46c56ee
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- just_one_lock (0.0.2)
4
+ just_one_lock (0.0.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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"
@@ -1,3 +1,3 @@
1
1
  module JustOneLock
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
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
- File.delete(path) rescue nil
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, timeout: 10) do
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, timeout: 10) do
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
- it 'handles high amount of concurrent tasks' do
69
- answer = 0
69
+ it 'runs in parallel without race condition' do
70
+ answer = 0
70
71
 
71
- parallel(100, timeout: JustOneLock::Blocking::DEFAULT_TIMEOUT * 10) do
72
- value = answer
73
- answer = value + 1
74
- end
72
+ parallel(2) do
73
+ value = answer
74
+ sleep(JustOneLock::Blocking::DEFAULT_TIMEOUT / 2)
75
+ answer = value + 21
76
+ end
75
77
 
76
- expect(answer).to eq(100)
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::Blocking.prevent_multiple_executions(dir, scope, delete_files: false, &block)
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::Blocking.prevent_multiple_executions(dir, scope, delete_files: false, &block)
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)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: just_one_lock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Stankiewicz, Yury Kotov