Permalink
Browse files

system:put : first success

  • Loading branch information...
1 parent 33f2e93 commit 813741fa4a072ff1b7aef30a637508f54a2ff6f6 @jmettraux committed Mar 9, 2011
Showing with 61 additions and 48 deletions.
  1. +50 −42 lib/register/system.rb
  2. +6 −1 lib/register/worker.rb
  3. +5 −5 spec/system_spec.rb
View
@@ -33,56 +33,64 @@ def self.put_system(redis)
'_id' => 'system',
'_rev' => '0',
- 'set' => proc { |args|
- @redis.set(args['key'], Rufus::Json.encode(args['value']))
+ 'put' => proc { |item|
+ rev = item['_rev']
+ Register.lock(@redis, item['_id']) do
+ current = @redis.get(item['_id'])
+ current_rev = current ? current['_rev'] : nil
+ if current_rev && rev != current_rev
+ current
+ elsif rev && current_rev.nil?
+ false
+ else
+ nrev = (rev || 0) + 1
+ @redis.set(
+ item['_id'],
+ Rufus::Json.encode(item.merge('_rev' => nrev)))
+ nrev
+ end
+ end
}.to_source,
- #'put' => proc { |args|
- # item = args
- # lock(item['_id']) do
- # # TODO
- # end
- #}.to_source,
-
'echo' => proc { |args|
args.collect { |a| a.to_s }.join(' ')
}.to_source
).to_json)
end
-# # A locking mecha.
-# #
-# # Mostly inspired from http://code.google.com/p/redis/wiki/SetnxCommand
-# #
-# def self.lock(key)
-#
-# kl = "#{key}-lock"
-#
-# loop do
-#
-# break if @redis.setnx(kl, Time.now.to_f.to_s) != false
-# # locking successful
-#
-# #
-# # already locked
-#
-# t = @redis.get(kl)
-#
-# @redis.del(kl) if t && Time.now.to_f - t.to_f > 60.0
-# # after 1 minute, locks time out
-#
-# sleep 0.007 # let's try to lock again after a while
-# end
-#
-# #@redis.expire(kl, 2)
-# # this doesn't work, it makes the next call to setnx succeed
-#
-# result = yield
-#
-# @redis.del(kl)
-#
-# result
-# end
+ # A locking mecha.
+ #
+ # Mostly inspired from http://code.google.com/p/redis/wiki/SetnxCommand
+ #
+ def self.lock(redis, key)
+
+ kl = "#{key}-lock"
+
+ loop do
+
+ break if redis.setnx(kl, Time.now.to_f.to_s) != false
+ # locking successful
+
+ #
+ # already locked
+
+ t = redis.get(kl)
+
+ redis.del(kl) if t && Time.now.to_f - t.to_f > 60.0
+ # after 1 minute, locks time out
+
+ sleep 0.007 # let's try to lock again after a while
+ end
+
+ #redis.expire(kl, 2)
+ # this doesn't work, it makes the next call to setnx succeed
+
+ result = yield
+
+ redis.del(kl)
+
+ result
+ end
end
View
@@ -87,7 +87,12 @@ def step
def reply(call, success, result)
if ticket = call['ticket']
- @client.redis.hset('_tickets', ticket, [ success, result ])
+
+ @client.redis.hset(
+ '_tickets',
+ ticket,
+ Rufus::Json.encode([ success, result ]))
+
#else
# no ticket given back
end
View
@@ -25,16 +25,16 @@
end
end
- describe "'set'" do
+ describe "'put'" do
- it 'sets a value' do
+ it 'puts an item' do
- ticket = cl.call('system', 'set', 'key' => 'x', 'value' => %w[ y z ])
+ ticket = cl.call('system', 'put', '_id' => 'x')
wo.send(:step)
- cl.result(ticket).should == [ true, 'OK' ]
- @r.get('x').should == Rufus::Json.encode(%w[ y z ])
+ cl.result(ticket).should == [ true, 1 ]
+ @r.get('x').should == Rufus::Json.encode('_id' => 'x', '_rev' => 1)
end
end
end

0 comments on commit 813741f

Please sign in to comment.