expirable_locking 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.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +34 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/lib/expirable_locking.rb +49 -0
- data/test/expirable_locking_test.rb +105 -0
- data/test/helper.rb +30 -0
- metadata +72 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Eric Chapweske
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
= expirable_locking
|
2
|
+
|
3
|
+
Usage:
|
4
|
+
Provides expirable locking for concurrent processing.
|
5
|
+
|
6
|
+
class MyModel < ActiveRecord::Base
|
7
|
+
extend ExpirableLocking
|
8
|
+
end
|
9
|
+
|
10
|
+
Process A: record.lock_with_expirey
|
11
|
+
Process A: => true
|
12
|
+
Process B: record.lock_with_expirey
|
13
|
+
Process B: => false
|
14
|
+
Process A: record.unlock
|
15
|
+
Process A: => true
|
16
|
+
Process B: record.lock_with_expirey
|
17
|
+
Process B: => true
|
18
|
+
|
19
|
+
By default the lock expires after 10 mintutes, this can be changed by overriding the lock_duration class method.
|
20
|
+
|
21
|
+
== Note on Patches/Pull Requests
|
22
|
+
|
23
|
+
* Fork the project.
|
24
|
+
* Make your feature addition or bug fix.
|
25
|
+
* Add tests for it. This is important so I don't break it in a
|
26
|
+
future version unintentionally.
|
27
|
+
* Commit, do not mess with rakefile, version, or history.
|
28
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
29
|
+
* Send me a pull request. Bonus points for topic branches.
|
30
|
+
|
31
|
+
== Copyright
|
32
|
+
|
33
|
+
Copyright (c) 2010 Eric Chapweske. See LICENSE for details.
|
34
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "expirable_locking"
|
8
|
+
gem.summary = %Q{A tiny ActiveRecord extension for expirable locking.}
|
9
|
+
gem.description = %Q{A tiny ActiveRecord extension for expirable locking.}
|
10
|
+
gem.email = "eac@zendesk.com"
|
11
|
+
gem.homepage = "http://github.com/eac/expirable_locking"
|
12
|
+
gem.authors = ["Eric Chapweske"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/*_test.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/*_test.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
task :test => :check_dependencies
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "expirable_locking #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module ExpirableLocking
|
2
|
+
|
3
|
+
def self.extended(base)
|
4
|
+
base.named_scope :unlocked, lambda {
|
5
|
+
{ :conditions => [ "locked_at IS NULL OR locked_at < ?", base.lock_duration.ago ] }
|
6
|
+
}
|
7
|
+
base.send(:include, InstanceMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
module InstanceMethods
|
11
|
+
|
12
|
+
def lock_with_expirey
|
13
|
+
self.class.lock(self)
|
14
|
+
end
|
15
|
+
|
16
|
+
def unlock
|
17
|
+
self.class.unlock(self)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
def lock_duration
|
23
|
+
10.minutes
|
24
|
+
end
|
25
|
+
|
26
|
+
def lock(record)
|
27
|
+
1 == unlocked.touch_lock(record)
|
28
|
+
end
|
29
|
+
|
30
|
+
def unlock(record)
|
31
|
+
return true if record.destroyed?
|
32
|
+
|
33
|
+
1 == update_all({ :locked_at => nil }, { :id => record, :locked_at => record.locked_at })
|
34
|
+
end
|
35
|
+
|
36
|
+
# Updates lock timestamp without triggering validations/callbacks.
|
37
|
+
def touch_lock(record)
|
38
|
+
locked_at = default_timezone == :utc ? Time.now.utc : Time.now
|
39
|
+
result = update_all({ :locked_at => locked_at }, { :id => record })
|
40
|
+
|
41
|
+
if result == 1
|
42
|
+
record.write_attribute_without_dirty(:locked_at, locked_at)
|
43
|
+
end
|
44
|
+
|
45
|
+
result
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class ExpirableLockingTest < ActiveRecord::TestCase
|
4
|
+
|
5
|
+
class LockableEmail < ActiveRecord::Base
|
6
|
+
extend ExpirableLocking
|
7
|
+
end
|
8
|
+
|
9
|
+
context "Expirable locks" do
|
10
|
+
setup do
|
11
|
+
@model = LockableEmail
|
12
|
+
@record = @model.create!
|
13
|
+
end
|
14
|
+
|
15
|
+
should "have a lock duration of 10 minutes by default" do
|
16
|
+
assert_equal 10.minutes, @model.lock_duration
|
17
|
+
end
|
18
|
+
|
19
|
+
context "unlocked scope" do
|
20
|
+
|
21
|
+
should "include records that have never been locked" do
|
22
|
+
assert @model.unlocked.include?(@record)
|
23
|
+
end
|
24
|
+
|
25
|
+
should "include records with an expired lock" do
|
26
|
+
Timecop.freeze(@model.lock_duration.ago - 1.second) do
|
27
|
+
@model.touch_lock(@record)
|
28
|
+
end
|
29
|
+
|
30
|
+
assert @model.unlocked.include?(@record)
|
31
|
+
end
|
32
|
+
|
33
|
+
should "not include records with a lock that isn't expired" do
|
34
|
+
Timecop.freeze(@model.lock_duration.ago + 1.second) do
|
35
|
+
@model.touch_lock(@record)
|
36
|
+
end
|
37
|
+
|
38
|
+
assert_equal false, @model.unlocked.include?(@record)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "locking" do
|
43
|
+
|
44
|
+
should "fail for locked records" do
|
45
|
+
@model.touch_lock(@record)
|
46
|
+
assert_equal false, @record.lock_with_expirey
|
47
|
+
end
|
48
|
+
|
49
|
+
should "succeed for unlocked records" do
|
50
|
+
assert @model.unlocked.include?(@record)
|
51
|
+
assert_equal true, @record.lock_with_expirey
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
context "unlocking" do
|
57
|
+
|
58
|
+
should "succeed without querying the database for deleted records" do
|
59
|
+
@record.lock_with_expirey
|
60
|
+
@record.destroy
|
61
|
+
|
62
|
+
@record.class.connection.expects(:update).never
|
63
|
+
assert_equal true, @record.unlock
|
64
|
+
end
|
65
|
+
|
66
|
+
should "succeed when the lock hasn't expired" do
|
67
|
+
@record.lock_with_expirey
|
68
|
+
assert_equal true, @record.unlock
|
69
|
+
end
|
70
|
+
|
71
|
+
should "fail when the lock expired and was re-aqcuired by another process" do
|
72
|
+
Timecop.freeze(@model.lock_duration.ago - 1.second) do
|
73
|
+
@record.lock_with_expirey
|
74
|
+
end
|
75
|
+
@model.find(@record).lock_with_expirey # Oh hi, other process
|
76
|
+
|
77
|
+
assert_equal false, @record.unlock
|
78
|
+
end
|
79
|
+
|
80
|
+
should "remove the record's lock" do
|
81
|
+
@model.touch_lock(@record)
|
82
|
+
@record.unlock
|
83
|
+
|
84
|
+
assert @model.unlocked.include?(@record)
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
context "touching the lock" do
|
90
|
+
|
91
|
+
should "update the record's lock timestamp" do
|
92
|
+
Timecop.freeze(Time.now) do
|
93
|
+
@model.touch_lock(@record)
|
94
|
+
|
95
|
+
assert_equal Time.current.to_i, @record.locked_at.to_i
|
96
|
+
assert_equal Time.current.to_i, @record.reload.locked_at.to_i
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
require 'activerecord'
|
5
|
+
require 'mocha'
|
6
|
+
require 'timecop'
|
7
|
+
require 'activesupport'
|
8
|
+
|
9
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
10
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
11
|
+
require 'expirable_locking'
|
12
|
+
|
13
|
+
class Test::Unit::TestCase
|
14
|
+
end
|
15
|
+
|
16
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => '/tmp/expirable_locking.sqlite')
|
17
|
+
|
18
|
+
ActiveRecord::Schema.define do
|
19
|
+
|
20
|
+
create_table :lockable_emails, :force => true do |table|
|
21
|
+
table.string :from
|
22
|
+
table.string :to
|
23
|
+
table.text :mail
|
24
|
+
table.timestamps
|
25
|
+
|
26
|
+
table.datetime :locked_at
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: expirable_locking
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Eric Chapweske
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-07-26 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: A tiny ActiveRecord extension for expirable locking.
|
22
|
+
email: eac@zendesk.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- .document
|
32
|
+
- .gitignore
|
33
|
+
- LICENSE
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- VERSION
|
37
|
+
- lib/expirable_locking.rb
|
38
|
+
- test/expirable_locking_test.rb
|
39
|
+
- test/helper.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/eac/expirable_locking
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- --charset=UTF-8
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.3.6
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: A tiny ActiveRecord extension for expirable locking.
|
70
|
+
test_files:
|
71
|
+
- test/expirable_locking_test.rb
|
72
|
+
- test/helper.rb
|