summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick J Cherry <patrick@bytemark.co.uk>2015-12-21 17:48:59 +0000
committerPatrick J Cherry <patrick@bytemark.co.uk>2015-12-21 17:48:59 +0000
commitbf480214a66d6df62c9bfcf5c448a375a67b3482 (patch)
tree187c04fa04ff852a35f19b84f8e7ff4de745665b
parentddd8335d24b2e59aaf7eec64bbef3d4d95b89733 (diff)
updated the add routine to ensure we update the set in order.
-rwxr-xr-xqueue/zset/add.rb58
1 files changed, 47 insertions, 11 deletions
diff --git a/queue/zset/add.rb b/queue/zset/add.rb
index c75bcd6..2d9f27a 100755
--- a/queue/zset/add.rb
+++ b/queue/zset/add.rb
@@ -7,23 +7,59 @@ require "redis"
@redis = Redis.new(:host => "127.0.0.1")
-
x = []
-x.push( "test 1" )
-x.push( "test 2" )
-x.push( "test 3" )
-
+#
+# Queue loads of tests
+#
+(1..10).to_a.each do |i|
+ x.push( "test #{i}" )
+end
-for i in 0 .. 10
+loop do
x.each do |test|
@redis.watch('zset')
- if (!(@redis.zscore("zset", test)))
- res = @redis.multi do |r|
- r.zadd('zset', Time.now.to_f * 10000000, test)
- end
+
+ print "adding #{test}"
+
+ #
+ # This is run in a loop, as we have to wait until both
+ #
+ # (a) the score is missing
+ # (b) the zadd function succeeds
+ #
+ loop do
+ #
+ # Print a dot for each go through the loop
+ #
+ print "."
+
+ #
+ # Only update if no score is set
+ #
+ if !@redis.zscore("zset", test)
+
+ #
+ # If MULTI returns nil, the transaction failed, so we need to try
+ # again.
+ #
+ break unless @redis.multi do |r|
+ @redis.zadd('zset', Time.now.to_f, test)
+ end.nil?
+
+ end
+
+ #
+ # This could be tighter..
+ #
+ sleep 0.1
end
+
+ print "\n"
+
+ #
+ # Do we need to unwatch here?
+ #
@redis.unwatch
end
- sleep 1
end