flock_synchronize 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/flock_synchronize.gemspec +25 -0
- data/lib/flock_synchronize/version.rb +3 -0
- data/lib/flock_synchronize.rb +31 -0
- metadata +121 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# FlockSynchronize
|
2
|
+
|
3
|
+
This gem allows synchronization of code across multiple processes on POSIX
|
4
|
+
systems using flock, similar to how Mutexes work for Ruby threads.
|
5
|
+
|
6
|
+
Wrapping your code in `FlockSynchronize.flock_synchronize` will create a
|
7
|
+
filesystem lock for a given string key. All processes synchronized with the
|
8
|
+
same key will wait for each other.
|
9
|
+
|
10
|
+
For example:
|
11
|
+
|
12
|
+
```
|
13
|
+
FlockSynchronize.flock_synchronize("mykey") do
|
14
|
+
some_code_that_needs_to_be_synchronized()
|
15
|
+
end
|
16
|
+
```
|
17
|
+
|
18
|
+
**Note**: This only works with multiple processes, and not with multiple Ruby
|
19
|
+
threads. If you want the same behaviour with threads, look at the built-in
|
20
|
+
Mutex class.
|
21
|
+
|
22
|
+
## Installation
|
23
|
+
|
24
|
+
Add this line to your application's Gemfile:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
gem 'flock_synchronize'
|
28
|
+
```
|
29
|
+
|
30
|
+
And then execute:
|
31
|
+
|
32
|
+
$ bundle
|
33
|
+
|
34
|
+
Or install it yourself as:
|
35
|
+
|
36
|
+
$ gem install flock_synchronize
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/fishpercolator/flock_synchronize
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "flock_synchronize"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'flock_synchronize/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "flock_synchronize"
|
8
|
+
spec.version = FlockSynchronize::VERSION
|
9
|
+
spec.authors = ["Rich Daley"]
|
10
|
+
spec.email = ["rich@fishpercolator.co.uk"]
|
11
|
+
|
12
|
+
spec.summary = %q{Simple synchronization between Ruby processes using flock}
|
13
|
+
spec.description = %q{Adds a flock_synchronize method that synchronizes code execution between processes, similar to how a Mutex works between threads.}
|
14
|
+
spec.homepage = "https://github.com/fishpercolator/flock_synchronize"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "parallel"
|
25
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "flock_synchronize/version"
|
2
|
+
require 'tmpdir'
|
3
|
+
|
4
|
+
module FlockSynchronize
|
5
|
+
# flock_synchronize wraps a block with a flock-based mutex.
|
6
|
+
#
|
7
|
+
# Any other process calling flock_synchronize with the same key will
|
8
|
+
# wait for the previous block to exit before executing.
|
9
|
+
#
|
10
|
+
# NOTE: This only works on a per-process basis and is not compatible with
|
11
|
+
# Ruby threads. For that, see the built-in Mutex library
|
12
|
+
#
|
13
|
+
# FlockSynchronize.flock_synchronize("my operation") do
|
14
|
+
# some.code_that_needs(synchronizing)
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# The optional locking_constant parameter allows you to specify a different
|
18
|
+
# constant than LOCK_EX. See the File.flock documentation for more info.
|
19
|
+
#
|
20
|
+
def self.flock_synchronize(key, locking_constant=File::LOCK_EX)
|
21
|
+
filename = File.join(Dir.tmpdir, "#{key}.flock")
|
22
|
+
begin
|
23
|
+
File.open(filename, 'w') do |f|
|
24
|
+
f.flock(locking_constant)
|
25
|
+
yield
|
26
|
+
end
|
27
|
+
ensure
|
28
|
+
File.unlink filename if File.exists? filename
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: flock_synchronize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rich Daley
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-10-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.10'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.10'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '10.0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '10.0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: parallel
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Adds a flock_synchronize method that synchronizes code execution between
|
79
|
+
processes, similar to how a Mutex works between threads.
|
80
|
+
email:
|
81
|
+
- rich@fishpercolator.co.uk
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- .gitignore
|
87
|
+
- .rspec
|
88
|
+
- .travis.yml
|
89
|
+
- Gemfile
|
90
|
+
- README.md
|
91
|
+
- Rakefile
|
92
|
+
- bin/console
|
93
|
+
- bin/setup
|
94
|
+
- flock_synchronize.gemspec
|
95
|
+
- lib/flock_synchronize.rb
|
96
|
+
- lib/flock_synchronize/version.rb
|
97
|
+
homepage: https://github.com/fishpercolator/flock_synchronize
|
98
|
+
licenses: []
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 1.8.23
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: Simple synchronization between Ruby processes using flock
|
121
|
+
test_files: []
|