locker 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/lib/locker/locker.rb +5 -3
- data/lib/locker/version.rb +1 -1
- data/spec/locker/locker_spec.rb +9 -0
- data/spec/spec_helper.rb +5 -4
- metadata +5 -5
data/.gitignore
CHANGED
data/lib/locker/locker.rb
CHANGED
@@ -39,10 +39,12 @@ class Locker
|
|
39
39
|
|
40
40
|
if @locked
|
41
41
|
begin
|
42
|
+
parent_thread = Thread.current
|
43
|
+
|
42
44
|
renewer = Thread.new do
|
43
45
|
while @locked
|
44
46
|
sleep @renew_every
|
45
|
-
renew
|
47
|
+
renew(parent_thread)
|
46
48
|
end
|
47
49
|
end
|
48
50
|
|
@@ -67,9 +69,9 @@ class Locker
|
|
67
69
|
@locked = update_all(["locked_by = NULL"],["key = ? and locked_by = ?", @key, @identifier])
|
68
70
|
end
|
69
71
|
|
70
|
-
def renew
|
72
|
+
def renew(thread=Thread.current)
|
71
73
|
@locked = update_all(["locked_until = clock_timestamp() at time zone 'UTC' + #{lock_interval}"], ["key = ? and locked_by = ?", @key, @identifier])
|
72
|
-
raise LockStolen unless @locked
|
74
|
+
thread.raise LockStolen unless @locked
|
73
75
|
@locked
|
74
76
|
end
|
75
77
|
|
data/lib/locker/version.rb
CHANGED
data/spec/locker/locker_spec.rb
CHANGED
@@ -76,6 +76,15 @@ describe Locker do
|
|
76
76
|
lock.update_attribute(:locked_by, "someone else")
|
77
77
|
expect{ locker.renew }.to raise_error(Locker::LockStolen)
|
78
78
|
end
|
79
|
+
|
80
|
+
it "should raise to the parent thread when the renewer is in a thread and someone steals the lock" do
|
81
|
+
expect do
|
82
|
+
Locker.run("steal me", :renew_every => (0.1).seconds) do
|
83
|
+
Lock.find_by_key("steal me").update_attribute(:locked_by, "contrived example")
|
84
|
+
sleep(0.3)
|
85
|
+
end
|
86
|
+
end.to raise_error(Locker::LockStolen)
|
87
|
+
end
|
79
88
|
end
|
80
89
|
|
81
90
|
describe "blocking" do
|
data/spec/spec_helper.rb
CHANGED
@@ -11,14 +11,15 @@ ActiveRecord::Base.time_zone_aware_attributes = true
|
|
11
11
|
ActiveRecord::Base.default_timezone = "UTC"
|
12
12
|
|
13
13
|
config = YAML.load_file(File.join(File.dirname(__FILE__), 'database.yml'))
|
14
|
-
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
|
15
14
|
begin
|
16
|
-
ActiveRecord::Base.establish_connection(config)
|
17
|
-
rescue
|
15
|
+
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
|
18
16
|
ActiveRecord::Base.connection.create_database(config['database'], config.merge("encoding" => config['encoding'] || ENV['CHARSET'] || 'utf8'))
|
19
|
-
|
17
|
+
rescue ActiveRecord::StatementInvalid => e
|
18
|
+
raise unless e.message =~ /database "locker_test" already exists/
|
20
19
|
end
|
21
20
|
|
21
|
+
ActiveRecord::Base.establish_connection(config)
|
22
|
+
|
22
23
|
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS locks")
|
23
24
|
ActiveRecord::Base.connection.create_table(:locks) do |t|
|
24
25
|
t.string :locked_by
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: locker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Nathan Sutton
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-
|
19
|
+
date: 2011-09-02 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
name: activerecord
|
@@ -136,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
136
|
requirements: []
|
137
137
|
|
138
138
|
rubyforge_project: locker
|
139
|
-
rubygems_version: 1.8.
|
139
|
+
rubygems_version: 1.8.6
|
140
140
|
signing_key:
|
141
141
|
specification_version: 3
|
142
142
|
summary: Locker is a locking mechanism for limiting the concurrency of ruby code using the database.
|