summaryrefslogtreecommitdiff
path: root/queue/zset/add.rb
blob: 2d9f27a355d020a9ea83d074333e0a63073bf305 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/ruby
#
# Add the same three tests over and over again.
#
#
require "redis"

@redis = Redis.new(:host => "127.0.0.1")

x = []

#
# Queue loads of tests
#
(1..10).to_a.each do |i|
  x.push( "test #{i}" )
end

loop do
    x.each do |test|
        @redis.watch('zset')

        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
end