cirrus 0.0.3
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.
- data/.gitignore +17 -0
- data/.rvmrc +1 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +41 -0
- data/Rakefile +10 -0
- data/cirrus.gemspec +22 -0
- data/lib/cirrus.rb +24 -0
- data/lib/cirrus/lock.rb +46 -0
- data/lib/cirrus/version.rb +3 -0
- data/test/cirrus/test_cirrus.rb +28 -0
- data/test/cirrus/test_lock.rb +66 -0
- data/test/minitest_helper.rb +3 -0
- metadata +111 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create use "ruby-1.9.3-p194@cirrus"
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Duncan Grazier
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Cirrus 
|
2
|
+
|
3
|
+
A simple tool that leverages Redis to allow you to safetly lock your blocks of code
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'cirrus'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install cirrus
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Cirrus.lock(me.id, you.id) do
|
22
|
+
"Yay!"
|
23
|
+
end
|
24
|
+
|
25
|
+
# => Yay!
|
26
|
+
|
27
|
+
Cirrus.lock(me.id, you.id) do
|
28
|
+
Cirrus.lock(me.id, you.id) do
|
29
|
+
"Yay!"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# => Cirrus::UnlockableExecption
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/cirrus.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/cirrus/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Duncan Grazier"]
|
6
|
+
gem.email = ["itsmeduncan@gmail.com"]
|
7
|
+
gem.description = %q{Transactional-like locking for your blocks of code}
|
8
|
+
gem.summary = %q{A simple tool that leverages Redis to allow you to safetly lock your blocks of code}
|
9
|
+
gem.homepage = "https://github.com/itsmeduncan/cirrus"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test)/})
|
14
|
+
gem.name = "cirrus"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Cirrus::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency("minitest", "~> 3.3.0")
|
19
|
+
|
20
|
+
gem.add_dependency("rake", "~> 0.9.2.2")
|
21
|
+
gem.add_dependency("redis", "~> 3.0.1")
|
22
|
+
end
|
data/lib/cirrus.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__)))
|
2
|
+
|
3
|
+
require "securerandom"
|
4
|
+
require "redis"
|
5
|
+
|
6
|
+
require "cirrus/version"
|
7
|
+
require "cirrus/lock"
|
8
|
+
|
9
|
+
module Cirrus
|
10
|
+
class UnlockableException < Exception; end
|
11
|
+
|
12
|
+
def self.lock(redis, *ids)
|
13
|
+
lock = Cirrus::Lock.new(redis, ids)
|
14
|
+
|
15
|
+
raise UnlockableException.new("Could not set lock: #{ids.inspect}") unless lock.set
|
16
|
+
|
17
|
+
yield
|
18
|
+
|
19
|
+
ensure
|
20
|
+
lock.release
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
data/lib/cirrus/lock.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
class Cirrus::Lock
|
2
|
+
attr_accessor :id, :redis, :values
|
3
|
+
|
4
|
+
LOCKED = "cirrus:locked"
|
5
|
+
LOCKS = "cirrus:locks"
|
6
|
+
|
7
|
+
def initialize(redis, *values)
|
8
|
+
@redis = redis
|
9
|
+
|
10
|
+
@id = SecureRandom.hex(8)
|
11
|
+
@values = values
|
12
|
+
end
|
13
|
+
|
14
|
+
def set
|
15
|
+
redis.multi do
|
16
|
+
redis.sadd(LOCKS, id)
|
17
|
+
redis.sadd(LOCKED, values)
|
18
|
+
redis.sadd(pointer, values)
|
19
|
+
end.all? { |result| result.is_a?(Integer) ? result > 0 : result == true }
|
20
|
+
end
|
21
|
+
|
22
|
+
def release
|
23
|
+
releasable_ids = redis.smembers(pointer)
|
24
|
+
|
25
|
+
redis.multi do
|
26
|
+
redis.srem(pointer, releasable_ids)
|
27
|
+
redis.srem(LOCKED, values)
|
28
|
+
redis.srem(LOCKS, id)
|
29
|
+
end.all? { |result| result.is_a?(Integer) ? result > 0 : result == true }
|
30
|
+
end
|
31
|
+
|
32
|
+
def locked?
|
33
|
+
redis.multi do
|
34
|
+
values.each do |value|
|
35
|
+
redis.sismember(LOCKED, value)
|
36
|
+
end
|
37
|
+
end.any? { |result| result == true }
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def pointer
|
43
|
+
"cirrus:#{id}:pointers"
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'minitest_helper'
|
2
|
+
|
3
|
+
class TestCirrus < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@redis = Redis.new
|
7
|
+
@redis.flushall
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_cirrus_runs_the_block
|
11
|
+
assert_equal(:foo, Cirrus.lock(@redis, 1,2) { :foo })
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_cirrus_raises_because_it_is_locked
|
15
|
+
lambda {
|
16
|
+
Cirrus.lock(@redis, 1, 2) { Cirrus.lock(@redis, 1, 2) { :foo } }
|
17
|
+
}.must_raise Cirrus::UnlockableException
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_it_releases_if_there_is_an_error
|
21
|
+
lambda {
|
22
|
+
Cirrus.lock(@redis, 1, 2) { raise ArgumentError }
|
23
|
+
}.must_raise ArgumentError
|
24
|
+
|
25
|
+
assert_equal(:foo, Cirrus.lock(@redis, 1, 2) { :foo })
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'minitest_helper'
|
2
|
+
|
3
|
+
class TestLock < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@redis = Redis.new
|
7
|
+
@redis.flushall
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_lock_generates_an_id
|
11
|
+
Cirrus::Lock.new(@redis, :foo).id.wont_be_nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_lock_sets_up_redis
|
15
|
+
Cirrus::Lock.new(@redis, :foo).redis.wont_be_nil
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_lock_sets_up_values
|
19
|
+
assert_equal([:foo, :bar], Cirrus::Lock.new(@redis, :foo, :bar).values)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_set_returns_true
|
23
|
+
lock = Cirrus::Lock.new(@redis, :foo, :bar)
|
24
|
+
|
25
|
+
assert_equal(true, lock.set)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_set_returns_false_if_it_is_already_set
|
29
|
+
lock = Cirrus::Lock.new(@redis, :foo, :bar)
|
30
|
+
lock.set
|
31
|
+
|
32
|
+
assert_equal(false, lock.set)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_release_frees_the_lock
|
36
|
+
lock = Cirrus::Lock.new(@redis, :foo, :bar)
|
37
|
+
lock.set
|
38
|
+
assert_equal(true, lock.release)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_release_returns_false
|
42
|
+
lock = Cirrus::Lock.new(@redis, :foo, :bar)
|
43
|
+
assert_equal(false, lock.release)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_release_frees_the_lock
|
47
|
+
lock = Cirrus::Lock.new(@redis, :foo, :bar)
|
48
|
+
lock.set
|
49
|
+
|
50
|
+
lock.release
|
51
|
+
assert_equal(false, lock.locked?)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_locked_returns_true
|
55
|
+
lock = Cirrus::Lock.new(@redis, :foo, :bar)
|
56
|
+
lock.set
|
57
|
+
|
58
|
+
assert_equal(true, lock.locked?)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_locked_returns_false
|
62
|
+
lock = Cirrus::Lock.new(@redis, :foo, :bar)
|
63
|
+
assert_equal(false, lock.locked?)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cirrus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Duncan Grazier
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: minitest
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.3.0
|
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: 3.3.0
|
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: 0.9.2.2
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.9.2.2
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: redis
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 3.0.1
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.0.1
|
62
|
+
description: Transactional-like locking for your blocks of code
|
63
|
+
email:
|
64
|
+
- itsmeduncan@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .rvmrc
|
71
|
+
- .travis.yml
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- cirrus.gemspec
|
77
|
+
- lib/cirrus.rb
|
78
|
+
- lib/cirrus/lock.rb
|
79
|
+
- lib/cirrus/version.rb
|
80
|
+
- test/cirrus/test_cirrus.rb
|
81
|
+
- test/cirrus/test_lock.rb
|
82
|
+
- test/minitest_helper.rb
|
83
|
+
homepage: https://github.com/itsmeduncan/cirrus
|
84
|
+
licenses: []
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 1.8.24
|
104
|
+
signing_key:
|
105
|
+
specification_version: 3
|
106
|
+
summary: A simple tool that leverages Redis to allow you to safetly lock your blocks
|
107
|
+
of code
|
108
|
+
test_files:
|
109
|
+
- test/cirrus/test_cirrus.rb
|
110
|
+
- test/cirrus/test_lock.rb
|
111
|
+
- test/minitest_helper.rb
|