easy_redis_lock 0.0.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.
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rbenv*
19
+ .ruby-version
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in easy_redis_lock.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Hubert Liu
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.
@@ -0,0 +1,36 @@
1
+ # EasyRedisLock
2
+
3
+ A simple redis locking gem to help with race conditions.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'easy_redis_lock', :git => 'git://github.com/Rigor/easy_redis_lock.git'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ ## Usage
16
+
17
+ Easy:
18
+ ```ruby
19
+ EasyRedisLock::GateKeeper.new.with_lock("unique_lock_key") { # code to wrap in the redis lock }
20
+ ```
21
+
22
+ ```ruby
23
+ @record = Record.find(99)
24
+ EasyRedisLock::GateKeeper.new.with_lock(@record.id) do
25
+ status = @record.run_count > 5 ? "done" : "in progress"
26
+ @record.update_attribute(:status, status)
27
+ end
28
+ ```
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'easy_redis_lock/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "easy_redis_lock"
8
+ spec.version = EasyRedisLock::VERSION
9
+ spec.authors = ["Hubert Liu"]
10
+ spec.email = ["hubert.liu@rigor.com"]
11
+ spec.description = "Easy redis locking gem"
12
+ spec.summary = "Easy redis locking gem"
13
+ spec.homepage = "https://github.com/Rigor/easy_redis_lock"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "fakeredis"
24
+
25
+ spec.add_dependency "redis"
26
+ end
@@ -0,0 +1,64 @@
1
+ require "easy_redis_lock/version"
2
+ require 'uri'
3
+ require 'redis'
4
+
5
+ module EasyRedisLock
6
+ class GateKeeper
7
+ attr_reader :delay, :redis, :seconds_delay, :lock_time
8
+
9
+ def initialize options={}
10
+ @delay = options.fetch(:delay, 1500)
11
+ @lock_time = options.fetch(:lock_time, 30) # seconds
12
+ @redis = options.fetch(:redis) { Redis.new }
13
+ end
14
+
15
+ def should_delay? delay_key
16
+ in_progress? delay_key
17
+ end
18
+
19
+ def mark_in_progress delay_key
20
+ set_progress delay_key
21
+ end
22
+
23
+ def with_lock delay_key=Time.now.to_i, delay=seconds_delay, &block
24
+ retries = 0
25
+ begin
26
+ while should_delay?(delay_key) do
27
+ sleep(delay)
28
+ retries += 1
29
+ break if retries >= 30
30
+ end
31
+ mark_in_progress(delay_key)
32
+ yield if block_given?
33
+ ensure
34
+ expire_lock(delay_key)
35
+ end
36
+ end
37
+
38
+ def cleanup
39
+ close_connection!
40
+ end
41
+
42
+ private
43
+
44
+ def seconds_delay
45
+ delay.to_f / 1000.0
46
+ end
47
+
48
+ def close_connection!
49
+ redis.quit
50
+ end
51
+
52
+ def expire_lock delay_key
53
+ redis.del("easy_redis_lock:#{delay_key}")
54
+ end
55
+
56
+ def in_progress? delay_key
57
+ redis.exists("easy_redis_lock:#{delay_key}")
58
+ end
59
+
60
+ def set_progress delay_key
61
+ redis.setex("easy_redis_lock:#{delay_key}", lock_time, 1) # auto expires after lock_time seconds
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module EasyRedisLock
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe EasyRedisLock::GateKeeper do
4
+
5
+ let!(:gatekeeper) { EasyRedisLock::GateKeeper.new }
6
+
7
+ it "should delay if the lock id exists" do
8
+ gatekeeper.redis.set("easy_redis_lock:locked_key", 1)
9
+ gatekeeper.should_delay?("locked_key").should be_true
10
+ end
11
+
12
+ it "should not delay if the lock id doesnt exist" do
13
+ gatekeeper.should_delay?("not_locked_key").should be_false
14
+ end
15
+
16
+ context "redis provided" do
17
+ it "uses that redis connection" do
18
+ keeper = EasyRedisLock::GateKeeper.new(:redis => Redis.new(:host => "someredishost", :port => 6379))
19
+ expect(keeper.redis.client.host).to eql("someredishost")
20
+ end
21
+ end
22
+
23
+ context "redis not provided" do
24
+ it "creates a redis connection" do
25
+ keeper = EasyRedisLock::GateKeeper.new
26
+ expect(keeper.redis.client.host).to eql("127.0.0.1")
27
+ end
28
+ end
29
+
30
+ context "#with_lock" do
31
+ it "should sleep the delay time if required" do
32
+ gatekeeper.redis.set("easy_redis_lock:pop_and_lock", 1)
33
+
34
+ gatekeeper.should_receive(:sleep).at_least(1).times.with(1.0)
35
+ gatekeeper.with_lock("pop_and_lock", 1) {}
36
+ end
37
+
38
+ it "should not sleep the delay time if not locked" do
39
+ gatekeeper.should_not_receive(:sleep)
40
+ gatekeeper.with_lock("not_pop_and_lock", 1) {}
41
+ end
42
+
43
+ it "should locked the key if not locked and unlock after" do
44
+ gatekeeper.should_not_receive(:sleep)
45
+ gatekeeper.with_lock("not_pop_and_lock", 1) {
46
+ gatekeeper.redis.exists("easy_redis_lock:not_pop_and_lock").should be_true
47
+ }
48
+ gatekeeper.redis.exists("easy_redis_lock:not_pop_and_lock").should be_false
49
+ end
50
+
51
+ it "should stop trying after 30 tries" do
52
+ gatekeeper.redis.set("easy_redis_lock:pop_and_lock", 1)
53
+
54
+ gatekeeper.should_receive(:sleep).exactly(30).times
55
+ gatekeeper.with_lock("pop_and_lock", 1) {}
56
+
57
+ end
58
+ end
59
+
60
+ end
@@ -0,0 +1,6 @@
1
+ require 'easy_redis_lock'
2
+ require 'fakeredis'
3
+
4
+ RSpec.configure do |config|
5
+ config.color_enabled = true # Use color in STDOUT
6
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_redis_lock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hubert Liu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-31 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.3'
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.3'
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'
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: '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: fakeredis
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
+ - !ruby/object:Gem::Dependency
79
+ name: redis
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Easy redis locking gem
95
+ email:
96
+ - hubert.liu@rigor.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - easy_redis_lock.gemspec
107
+ - lib/easy_redis_lock.rb
108
+ - lib/easy_redis_lock/version.rb
109
+ - spec/easy_redis_lock_spec.rb
110
+ - spec/spec_helper.rb
111
+ homepage: https://github.com/Rigor/easy_redis_lock
112
+ licenses: []
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 1.8.23
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: Easy redis locking gem
135
+ test_files:
136
+ - spec/easy_redis_lock_spec.rb
137
+ - spec/spec_helper.rb