just_one_lock 0.2.0 → 0.2.2

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
- SHA1:
3
- metadata.gz: 279edd5887b6cab66bb98750888a34874a01b326
4
- data.tar.gz: 05ed51a2381bc20704ebcbe1f995ea9f1fbc8efc
2
+ SHA256:
3
+ metadata.gz: 720c9f19a7d392419709231fb7990f0e178acad4cb33c434e9db52137e2a372d
4
+ data.tar.gz: b1d86cedb534ad6b81c5b9e5e59ca2e9cfa570fe1f3d585cffae2bfb36efe9ed
5
5
  SHA512:
6
- metadata.gz: 14b521caf2b66a1f7604f799fc3c4a8b3db6ed161b05b560c7a82427e53fb25fd396384108d6608c9339ca08095435b5a5a28d6c1d0a0bf14c0aedbb9b623552
7
- data.tar.gz: 9193f32028551b0455011a11bf9b8f08f0360657a482455cbfc783f4aaffe5e26fc8420e7c413d2ba8a81cf8d9ec9463196d0b1fe4c54f51429ab4d9f01a0a91
6
+ metadata.gz: 985e020c4dbf0b4c2a901d07d370fc26c4e34a4152bb8cb84855080592d0076d00612a8975f3562aceda0b33671175bc5d0557ca17def01c622267f77877a780
7
+ data.tar.gz: 1de801e3d26fa640f841f68e5960ab39a86d2b1c2669c00fbcc26d91787dccdfe17c22e412cc0edef0b4f550f5250376eccd4f7536f9a6d46ac5e5e962cc5451
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
data/Dockerfile ADDED
@@ -0,0 +1,7 @@
1
+ FROM httplab/ruby-dev-app:2.3
2
+
3
+ VOLUME $HOME/$APP
4
+
5
+ USER $USER
6
+
7
+ CMD [ 'irb' ]
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  source 'https://rubygems.org'
2
3
 
3
4
  # Specify your gem's dependencies in filelock.gemspec
data/Rakefile CHANGED
@@ -1 +1,2 @@
1
- require "bundler/gem_tasks"
1
+ # frozen_string_literal: true
2
+ require 'bundler/gem_tasks'
data/bin/just_one_lock CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'gli'
4
5
  require_relative '../lib/just_one_lock'
@@ -10,25 +11,25 @@ include GLI::App
10
11
  program_desc 'Just one lock runner'
11
12
  version JustOneLock::VERSION
12
13
 
13
- flag [:l,:lock_dir], default_value: '/tmp'
14
- flag [:s,:scope]
15
- flag [:t,:timeout], desc: 'Timeout in seconds'
14
+ flag [:l, :lock_dir], default_value: '/tmp'
15
+ flag [:s, :scope]
16
+ flag [:t, :timeout], desc: 'Timeout in seconds'
16
17
 
17
18
  desc 'Execute system command'
18
19
  command :exec do |c|
19
- c.action do |global_options, options, args|
20
+ c.action do |global_options, _options, args|
20
21
  JustOneLock.world.directory = global_options[:lock_dir]
21
22
  scope = global_options[:scope]
22
23
  timeout = global_options[:timeout].to_f
23
24
  command_to_run = args.first
24
25
 
25
- if timeout > 0
26
- locker = JustOneLock::BlockingLocker.new(timeout: timeout)
27
- else
28
- locker = JustOneLock::NonBlockingLocker.new
29
- end
26
+ locker = if timeout > 0
27
+ JustOneLock::BlockingLocker.new(timeout: timeout)
28
+ else
29
+ JustOneLock::NonBlockingLocker.new
30
+ end
30
31
 
31
- JustOneLock::prevent_multiple_executions(locker, scope) do
32
+ JustOneLock.prevent_multiple_executions(scope, locker) do
32
33
  system command_to_run
33
34
  end
34
35
  end
@@ -37,4 +38,3 @@ end
37
38
  default_command :exec
38
39
 
39
40
  exit run(ARGV)
40
-
@@ -0,0 +1,5 @@
1
+ dev:
2
+ build: .
3
+ volumes:
4
+ - .:/home/apps/app
5
+ - ~/data/gems-2.3:/.gem
@@ -1,25 +1,24 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'just_one_lock/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "just_one_lock"
7
+ spec.name = 'just_one_lock'
8
8
  spec.version = JustOneLock::VERSION
9
- spec.authors = ["Yury Kotov"]
10
- spec.email = ["bairkan@gmail.com"]
11
- spec.description = %q{Simple solution to prevent multiple executions using flock}
12
- spec.summary = %q{Simple solution to prevent multiple executions using flock}
13
- spec.homepage = "http://github.com/beorc/just_one_lock"
14
- spec.license = "MIT"
9
+ spec.authors = ['Yury Kotov']
10
+ spec.email = ['bairkan@gmail.com']
11
+ spec.description = 'Simple solution to prevent multiple executions using flock'
12
+ spec.summary = 'Simple solution to prevent multiple executions using flock'
13
+ spec.homepage = 'http://github.com/beorc/just_one_lock'
14
+ spec.license = 'MIT'
15
15
 
16
- spec.files = `git ls-files`.split($/)
16
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake", '~> 10.3'
23
- spec.add_development_dependency "rspec", '~> 3.0'
21
+ spec.add_development_dependency 'rake', '~> 10.3'
22
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
23
  spec.add_dependency 'gli', '~> 2.11'
25
24
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  class JustOneLock::BaseLocker
2
3
  def already_locked(scope)
3
4
  msg = "Another process <#{scope}> already is running"
@@ -14,14 +15,13 @@ class JustOneLock::BaseLocker
14
15
  f.truncate(f.pos)
15
16
  end
16
17
 
17
- def run(f, path, &block)
18
+ def run(f, path)
18
19
  write_pid(f)
19
20
 
20
21
  JustOneLock.before_lock(path, f)
21
- result = block.call
22
+ result = yield
22
23
  JustOneLock.after_lock(path, f)
23
24
 
24
25
  result
25
26
  end
26
27
  end
27
-
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'timeout'
2
3
 
3
4
  class JustOneLock::BlockingLocker < JustOneLock::BaseLocker
@@ -12,8 +13,8 @@ class JustOneLock::BlockingLocker < JustOneLock::BaseLocker
12
13
  def lock(lock_path, &block)
13
14
  result = nil
14
15
 
15
- File.open(lock_path, File::RDWR|File::CREAT, 0644) do |f|
16
- Timeout::timeout(@timeout, JustOneLock::AlreadyLocked) { f.flock(File::LOCK_EX) }
16
+ File.open(lock_path, File::RDWR | File::CREAT, 0o644) do |f|
17
+ Timeout.timeout(@timeout, JustOneLock::AlreadyLocked) { f.flock(File::LOCK_EX) }
17
18
 
18
19
  result = run(f, lock_path, &block)
19
20
  end
@@ -21,4 +22,3 @@ class JustOneLock::BlockingLocker < JustOneLock::BaseLocker
21
22
  result
22
23
  end
23
24
  end
24
-
@@ -1,16 +1,16 @@
1
+ # frozen_string_literal: true
1
2
  class JustOneLock::NonBlockingLocker < JustOneLock::BaseLocker
2
3
  def lock(lock_path, &block)
3
4
  result = nil
4
5
 
5
- File.open(lock_path, File::RDWR|File::CREAT, 0644) do |f|
6
- if f.flock(File::LOCK_NB|File::LOCK_EX)
6
+ File.open(lock_path, File::RDWR | File::CREAT, 0o644) do |f|
7
+ if f.flock(File::LOCK_NB | File::LOCK_EX)
7
8
  result = run(f, lock_path, &block)
8
9
  else
9
- fail JustOneLock::AlreadyLocked
10
+ raise JustOneLock::AlreadyLocked
10
11
  end
11
12
  end
12
13
 
13
14
  result
14
15
  end
15
16
  end
16
-
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module JustOneLock
2
- VERSION = '0.2.0'
3
+ VERSION = '0.2.2'
3
4
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  class JustOneLock::World
2
3
  attr_accessor :output, :directory, :delete_files
3
4
 
@@ -12,9 +13,7 @@ class JustOneLock::World
12
13
  paths_to_delete = []
13
14
 
14
15
  @files.each do |path, f|
15
- if File.exists?(path) && f.closed?
16
- paths_to_delete << path
17
- end
16
+ paths_to_delete << path if File.exist?(path) && f.closed?
18
17
  end
19
18
 
20
19
  paths_to_delete.each do |path|
@@ -27,7 +26,7 @@ class JustOneLock::World
27
26
  @files[name] = file
28
27
  end
29
28
 
30
- def after_lock(name, file)
29
+ def after_lock(_name, _file)
31
30
  delete_unlocked_files if delete_files
32
31
  end
33
32
 
data/lib/just_one_lock.rb CHANGED
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
1
2
  require 'just_one_lock/version'
2
3
  require 'just_one_lock/world'
3
4
  require 'just_one_lock/base_locker'
4
5
  require 'just_one_lock/blocking_locker'
5
6
  require 'just_one_lock/non_blocking_locker'
6
7
  require 'forwardable'
8
+ require 'pathname'
7
9
 
8
10
  module JustOneLock
9
11
  class AlreadyLocked < StandardError; end
@@ -22,7 +24,7 @@ module JustOneLock
22
24
  locker = JustOneLock::NonBlockingLocker.new,
23
25
  &block
24
26
  )
25
- scope_name = scope.gsub(':', '_')
27
+ scope_name = scope.tr(':', '_')
26
28
  lock_path = File.join(world.directory, scope_name + '.lock')
27
29
 
28
30
  begin
@@ -32,4 +34,3 @@ module JustOneLock
32
34
  end
33
35
  end
34
36
  end
35
-
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'just_one_lock'
2
3
  require 'tempfile'
3
4
  require 'timeout'
@@ -61,4 +62,3 @@ describe JustOneLock::BlockingLocker do
61
62
  expect(answer).to eq(100)
62
63
  end
63
64
  end
64
-
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'spec_helper'
2
3
 
3
4
  shared_examples 'a locking object' do
@@ -17,7 +18,6 @@ shared_examples 'a locking object' do
17
18
  it 'returns value returned by block' do
18
19
  Dir.mktmpdir do |lock_dir|
19
20
  lockpath = File.join(lock_dir, 'sample.lock')
20
- answer = 0
21
21
 
22
22
  answer = locker.lock lockpath do
23
23
  42
@@ -67,10 +67,8 @@ shared_examples 'a locking object' do
67
67
  end
68
68
  end
69
69
 
70
- Timeout::timeout(1) do
71
- while locked == false
72
- sleep 0.1
73
- end
70
+ Timeout.timeout(1) do
71
+ sleep 0.1 while locked == false
74
72
  end
75
73
 
76
74
  expect do
@@ -88,27 +86,25 @@ shared_examples 'a locking object' do
88
86
 
89
87
  it 'should unblock files when killing processes' do
90
88
  lockpath = Tempfile.new(['sample', '.lock']).path
91
- dir, scope = dir_and_scope(lockpath)
89
+ _dir, scope = dir_and_scope(lockpath)
92
90
 
93
- pid = fork {
94
- JustOneLock::prevent_multiple_executions(scope, locker) do
91
+ pid = fork do
92
+ JustOneLock.prevent_multiple_executions(scope, locker) do
95
93
  sleep 10
96
94
  end
97
- }
95
+ end
98
96
 
99
- Timeout::timeout(1) do
100
- while !File.exist?(lockpath)
101
- sleep 0.1
102
- end
97
+ Timeout.timeout(1) do
98
+ sleep 0.1 until File.exist?(lockpath)
103
99
  end
104
100
 
105
101
  answer = 0
106
102
 
107
- thread = Thread.new {
108
- JustOneLock::prevent_multiple_executions(scope, locker) do
103
+ thread = Thread.new do
104
+ JustOneLock.prevent_multiple_executions(scope, locker) do
109
105
  answer += 42
110
106
  end
111
- }
107
+ end
112
108
 
113
109
  expect(answer).to eq(0)
114
110
  Process.kill(9, pid)
@@ -152,5 +148,3 @@ shared_examples 'a locking object' do
152
148
  expect(File.exist?(filename)).to eq(true)
153
149
  end
154
150
  end
155
-
156
-
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'just_one_lock'
2
3
  require 'tempfile'
3
4
  require 'timeout'
@@ -7,4 +8,3 @@ describe JustOneLock::NonBlockingLocker do
7
8
  let(:locker) { JustOneLock::NonBlockingLocker.new }
8
9
  it_behaves_like 'a locking object'
9
10
  end
10
-
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'just_one_lock'
2
3
  require 'tempfile'
3
4
  require 'timeout'
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Helper because File.write won't work for older Ruby
2
3
  def write(filename, contents)
3
4
  File.open(filename.to_s, 'w') { |f| f.write(contents) }
@@ -12,13 +13,13 @@ def dir_and_scope(lockpath)
12
13
  end
13
14
 
14
15
  def parallel(n = 2, lockpath: Tempfile.new(['sample', '.lock']).path, &block)
15
- Timeout::timeout(5) do
16
+ Timeout.timeout(5) do
16
17
  dir, scope = dir_and_scope(lockpath)
17
18
  JustOneLock.world.directory = dir
18
19
 
19
20
  (1..n).map do
20
21
  Thread.new do
21
- JustOneLock::prevent_multiple_executions(scope, locker, &block)
22
+ JustOneLock.prevent_multiple_executions(scope, locker, &block)
22
23
  end
23
24
  end.map(&:join)
24
25
  end
@@ -27,14 +28,14 @@ def parallel(n = 2, lockpath: Tempfile.new(['sample', '.lock']).path, &block)
27
28
  end
28
29
 
29
30
  def parallel_forks(n = 2, lockpath: Tempfile.new(['sample', '.lock']).path, &block)
30
- Timeout::timeout(5) do
31
+ Timeout.timeout(5) do
31
32
  dir, scope = dir_and_scope(lockpath)
32
33
  JustOneLock.world.directory = dir
33
34
 
34
35
  (1..n).map do
35
- fork {
36
- JustOneLock::prevent_multiple_executions(scope, locker, &block)
37
- }
36
+ fork do
37
+ JustOneLock.prevent_multiple_executions(scope, locker, &block)
38
+ end
38
39
  end.map do |pid|
39
40
  Process.waitpid(pid)
40
41
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: just_one_lock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yury Kotov
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-17 00:00:00.000000000 Z
11
+ date: 2024-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.3'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.3'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rake
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -76,12 +62,15 @@ extra_rdoc_files: []
76
62
  files:
77
63
  - ".gitignore"
78
64
  - ".rspec"
65
+ - ".rubocop.yml"
66
+ - Dockerfile
79
67
  - Gemfile
80
68
  - Gemfile.lock
81
69
  - LICENSE
82
70
  - README.md
83
71
  - Rakefile
84
72
  - bin/just_one_lock
73
+ - docker-compose.yml
85
74
  - just_one_lock.gemspec
86
75
  - lib/just_one_lock.rb
87
76
  - lib/just_one_lock/base_locker.rb
@@ -98,7 +87,7 @@ homepage: http://github.com/beorc/just_one_lock
98
87
  licenses:
99
88
  - MIT
100
89
  metadata: {}
101
- post_install_message:
90
+ post_install_message:
102
91
  rdoc_options: []
103
92
  require_paths:
104
93
  - lib
@@ -113,9 +102,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
102
  - !ruby/object:Gem::Version
114
103
  version: '0'
115
104
  requirements: []
116
- rubyforge_project:
117
- rubygems_version: 2.2.2
118
- signing_key:
105
+ rubygems_version: 3.5.3
106
+ signing_key:
119
107
  specification_version: 4
120
108
  summary: Simple solution to prevent multiple executions using flock
121
109
  test_files: