safe_flock 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5d93a87058706f01d75abc1892f295c1448bbe62829dbd71fd05f50b21660c24
4
+ data.tar.gz: 50307bf731dcba44c41e0304154cc38e67d8ef3f93e45de02bd7568c05ea12d6
5
+ SHA512:
6
+ metadata.gz: 40506d496027784cd008d5ff95fd855e85d84ea492ff2f7be40a84bb3393fdce11be11ecf0a249d820ea953708eb4e3078a596d433ce3313cc47e5b312021002
7
+ data.tar.gz: 71c2b6401b2e6b93e4172d53b8a93b0fdd6c282c2da976050f7ca5c6923aaf69de94297e48027fe4f23118f57fb7b991a6d9bf1c9723499aa40bec6f7408e2a2
data/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1
4
+ - 2.5
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in safe_flock.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ safe_flock (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.7.0)
12
+ rspec-core (~> 3.7.0)
13
+ rspec-expectations (~> 3.7.0)
14
+ rspec-mocks (~> 3.7.0)
15
+ rspec-core (3.7.1)
16
+ rspec-support (~> 3.7.0)
17
+ rspec-expectations (3.7.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.7.0)
20
+ rspec-mocks (3.7.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-support (3.7.1)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.17)
30
+ rake (~> 10.0)
31
+ rspec (~> 3.7)
32
+ safe_flock!
33
+
34
+ BUNDLED WITH
35
+ 1.17.1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) [year] [fullname]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # SafeFlock
2
+
3
+ Thread-safe, transferable, flock-based file lock.
4
+
5
+ This helper solves mutual exclusion within niche constraints:
6
+
7
+ * A parent process must acquire a lock and transfer it to a child process.
8
+ * The parent may terminate before the child.
9
+ * Locks may be created within threads within the parent.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'safe_flock'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install safe_flock
26
+
27
+ ## Usage
28
+
29
+ Simple example:
30
+
31
+ ```
32
+ require "safe_flock"
33
+
34
+ SafeFlock.create("/var/run/myapp/myapp.lck") do
35
+ # ... mutually excluded processing
36
+ end
37
+ ```
38
+
39
+ The use case for which the helper was created:
40
+
41
+ ```
42
+ require "safe_flock"
43
+
44
+ SafeFlock.create("/var/run/myapp/myapp.lck") do
45
+ child = fork do
46
+ # ... mutually excluded processing
47
+ end
48
+ end
49
+ Process.detach(child)
50
+ ```
51
+
52
+ ## Development
53
+
54
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
55
+
56
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/starjuice/safe_flock.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
3
+
4
+ begin
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ rescue LoadError
8
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "safe_flock"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,95 @@
1
+ require "time"
2
+
3
+ module SafeFlock
4
+
5
+ class Lockfile
6
+
7
+ class Error < RuntimeError; end
8
+ class Locked < RuntimeError; end
9
+
10
+ @@global_mutex = Mutex.new unless defined?(@@global_mutex)
11
+ @@path_mutex = {} unless defined?(@@path_mutex)
12
+
13
+ # +path+ full pathname of mutually agreed lock file.
14
+ #
15
+ # +options+
16
+ # * +max_wait+ seconds to retry acquiring lock before giving up and raising +Error+ (+5.0+)
17
+ #
18
+ def initialize(path, max_wait: 5.0)
19
+ @pid = $$
20
+ @thread_id = Thread.current.object_id
21
+ @path = path
22
+ @max_wait = max_wait
23
+ @wait_per_try = 0.1
24
+ @mlocked = false
25
+ @lockfd = nil
26
+ end
27
+
28
+ def lock
29
+ deadline = Time.now.to_f + @max_wait
30
+ while !(is_locked = try_lock)
31
+ if Time.now.to_f < deadline
32
+ sleep @wait_per_try
33
+ else
34
+ break
35
+ end
36
+ end
37
+ is_locked
38
+ end
39
+
40
+ # If the lock is inherited by a forked child process, it will hold the lock until
41
+ # the child calls +unlock+ (or terminates) _and_ the parent's +Lockfile.create+
42
+ # block terminates. The parent should not call +unlock+.
43
+ #
44
+ def unlock
45
+ if @lockfd
46
+ @lockfd.close
47
+ @lockfd = nil
48
+ end
49
+ if @mlocked && @pid == $$ and @thread_id == Thread.current.object_id
50
+ mutex_unlock
51
+ end
52
+ end
53
+
54
+ attr_reader :path, :pid, :thread_id
55
+
56
+ private
57
+
58
+ def try_lock
59
+ begin
60
+ if try_mutex_lock
61
+ @lockfd = File.new(@path, "a")
62
+ @lockfd.flock(File::LOCK_EX | File::LOCK_NB)
63
+ end
64
+ rescue
65
+ @lockfd.close if @lockfd and !@lockfd.closed?
66
+ @lockfd = nil
67
+ mutex_unlock
68
+ raise
69
+ end
70
+ end
71
+
72
+ def try_mutex_lock
73
+ @@global_mutex.synchronize do
74
+ @@path_mutex[@path] = Mutex.new unless @@path_mutex[@path]
75
+ @mlocked = @@path_mutex[@path].try_lock
76
+ end
77
+ end
78
+
79
+ def mutex_unlock
80
+ @@global_mutex.synchronize do
81
+ if @mlocked
82
+ @@path_mutex[@path].unlock
83
+ @@path_mutex.delete(@path)
84
+ @mlocked = false
85
+ end
86
+ end
87
+ end
88
+
89
+ def debug(*args)
90
+ #$stderr.puts "DEBUG: LOCKFILE: [#{$$}]<#{"0x%x" % Thread.current.object_id}>: " + args.join(": ")
91
+ end
92
+
93
+ end
94
+
95
+ end
@@ -0,0 +1,3 @@
1
+ module SafeFlock
2
+ VERSION = "0.1.0"
3
+ end
data/lib/safe_flock.rb ADDED
@@ -0,0 +1,25 @@
1
+ require "safe_flock/lockfile"
2
+ require "safe_flock/version"
3
+
4
+ module SafeFlock
5
+
6
+ class Error < RuntimeError; end
7
+ class Locked < RuntimeError; end
8
+
9
+ def self.create(path, options = {})
10
+ raise(ArgumentError, "Block required") unless block_given?
11
+ lockfile = Lockfile.new(path, options)
12
+ begin
13
+ if lockfile.lock
14
+ begin
15
+ yield lockfile
16
+ ensure
17
+ lockfile.unlock
18
+ end
19
+ else
20
+ raise Locked, "Timed out waiting for lock"
21
+ end
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,28 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "safe_flock/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "safe_flock"
8
+ spec.version = SafeFlock::VERSION
9
+ spec.authors = ["Sheldon Hearn"]
10
+ spec.email = ["sheldonh@starjuice.net"]
11
+
12
+ spec.summary = %q{Thread-safe, transferable, flock-based lock file}
13
+ spec.description = %q{Thread-safe, transferable, flock-based lock file}
14
+ spec.homepage = "https://github.com/starjuice/safe_flock"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.17"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.7"
28
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: safe_flock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sheldon Hearn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-02 00:00:00.000000000 Z
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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.7'
55
+ description: Thread-safe, transferable, flock-based lock file
56
+ email:
57
+ - sheldonh@starjuice.net
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/safe_flock.rb
73
+ - lib/safe_flock/lockfile.rb
74
+ - lib/safe_flock/version.rb
75
+ - safe_flock.gemspec
76
+ homepage: https://github.com/starjuice/safe_flock
77
+ licenses: []
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.7.6
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Thread-safe, transferable, flock-based lock file
99
+ test_files: []