Skip to content

Commit

Permalink
system:put : first success
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Mar 9, 2011
1 parent 33f2e93 commit 813741f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 48 deletions.
92 changes: 50 additions & 42 deletions lib/register/system.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,56 +33,64 @@ def self.put_system(redis)
'_id' => 'system', '_id' => 'system',
'_rev' => '0', '_rev' => '0',


'set' => proc { |args| 'put' => proc { |item|
@redis.set(args['key'], Rufus::Json.encode(args['value'])) 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, }.to_source,


#'put' => proc { |args|
# item = args
# lock(item['_id']) do
# # TODO
# end
#}.to_source,

'echo' => proc { |args| 'echo' => proc { |args|
args.collect { |a| a.to_s }.join(' ') args.collect { |a| a.to_s }.join(' ')
}.to_source }.to_source


).to_json) ).to_json)
end end


# # A locking mecha. # A locking mecha.
# # #
# # Mostly inspired from http://code.google.com/p/redis/wiki/SetnxCommand # Mostly inspired from http://code.google.com/p/redis/wiki/SetnxCommand
# # #
# def self.lock(key) def self.lock(redis, key)
#
# kl = "#{key}-lock" kl = "#{key}-lock"
#
# loop do loop do
#
# break if @redis.setnx(kl, Time.now.to_f.to_s) != false break if redis.setnx(kl, Time.now.to_f.to_s) != false
# # locking successful # locking successful
#
# # #
# # already locked # already locked
#
# t = @redis.get(kl) t = redis.get(kl)
#
# @redis.del(kl) if t && Time.now.to_f - t.to_f > 60.0 redis.del(kl) if t && Time.now.to_f - t.to_f > 60.0
# # after 1 minute, locks time out # after 1 minute, locks time out
#
# sleep 0.007 # let's try to lock again after a while sleep 0.007 # let's try to lock again after a while
# end end
#
# #@redis.expire(kl, 2) #redis.expire(kl, 2)
# # this doesn't work, it makes the next call to setnx succeed # this doesn't work, it makes the next call to setnx succeed
#
# result = yield result = yield
#
# @redis.del(kl) redis.del(kl)
#
# result result
# end end
end end


7 changes: 6 additions & 1 deletion lib/register/worker.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ def step
def reply(call, success, result) def reply(call, success, result)


if ticket = call['ticket'] if ticket = call['ticket']
@client.redis.hset('_tickets', ticket, [ success, result ])
@client.redis.hset(
'_tickets',
ticket,
Rufus::Json.encode([ success, result ]))

#else #else
# no ticket given back # no ticket given back
end end
Expand Down
10 changes: 5 additions & 5 deletions spec/system_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
end end
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) wo.send(:step)


cl.result(ticket).should == [ true, 'OK' ] cl.result(ticket).should == [ true, 1 ]
@r.get('x').should == Rufus::Json.encode(%w[ y z ]) @r.get('x').should == Rufus::Json.encode('_id' => 'x', '_rev' => 1)
end end
end end
end end
Expand Down

0 comments on commit 813741f

Please sign in to comment.