diff options
author | Steve Kemp <steve@steve.org.uk> | 2016-01-18 14:16:07 +0200 |
---|---|---|
committer | Steve Kemp <steve@steve.org.uk> | 2016-01-18 14:16:07 +0200 |
commit | c6f2c6f924e6b263be5a3ba4a4eb7049f27544d3 (patch) | |
tree | e023f6bbef8a2ecf8829a0d0ab5214b902266bc7 /queue | |
parent | fbd4e3bf99afbc80012d6dc029be19e1bd8e6890 (diff) |
Removed queue-examples.
These are no longer required now we're sure we understand the process
of adding jobs to the queue without duplication.
Diffstat (limited to 'queue')
-rwxr-xr-x | queue/list/add.rb | 22 | ||||
-rwxr-xr-x | queue/list/run.rb | 32 | ||||
-rwxr-xr-x | queue/set/add.rb | 22 | ||||
-rwxr-xr-x | queue/set/run.rb | 32 | ||||
-rwxr-xr-x | queue/zset/add.rb | 65 | ||||
-rwxr-xr-x | queue/zset/run.rb | 45 | ||||
-rw-r--r-- | queue/zset/run_with_threads.rb | 75 |
7 files changed, 0 insertions, 293 deletions
diff --git a/queue/list/add.rb b/queue/list/add.rb deleted file mode 100755 index a8676d9..0000000 --- a/queue/list/add.rb +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/ruby -# -# Add the same three tests over and over again. -# -# -require "redis" - -@redis = Redis.new(:host => "127.0.0.1") - - -x = [] - -x.push( "http://example.com/ must run http otherwise 'fail'" ) -x.push( "1.2.3.4 must run ping otherwise 'fail'" ) -x.push( "https://steve.net/ must run https otherwise 'fail'" ) - - -for i in 0 .. 10 - x.each do |test| - @redis.lpush('list', test) - end -end diff --git a/queue/list/run.rb b/queue/list/run.rb deleted file mode 100755 index 08623b0..0000000 --- a/queue/list/run.rb +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/ruby - -require "redis" -require 'pp' -@redis = Redis.new(:host => "127.0.0.1") - - - -def fetch(timeout = 1) - job = nil - - loop do - job = @redis.lpop('list') - - if !job.nil? - return job - else - sleep(timeout) - end - - end -end - - - -while( x = fetch() ) - puts "Got job : #{x}" - if ( x =~ /ping/i ) - puts "PING TEST - sleeping" - sleep 5 - end -end diff --git a/queue/set/add.rb b/queue/set/add.rb deleted file mode 100755 index ab40607..0000000 --- a/queue/set/add.rb +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/ruby -# -# Add the same three tests over and over again. -# -# -require "redis" - -@redis = Redis.new(:host => "127.0.0.1") - - -x = [] - -x.push( "http://example.com/ must run http otherwise 'fail'" ) -x.push( "1.2.3.4 must run ping otherwise 'fail'" ) -x.push( "https://steve.net/ must run https otherwise 'fail'" ) - - -for i in 0 .. 10 - x.each do |test| - @redis.sadd('set', test) - end -end diff --git a/queue/set/run.rb b/queue/set/run.rb deleted file mode 100755 index f0b7476..0000000 --- a/queue/set/run.rb +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/ruby - -require "redis" -require 'pp' -@redis = Redis.new(:host => "127.0.0.1") - - - -def fetch(timeout = 1) - job = nil - - loop do - job = @redis.spop('set') - - if !job.nil? - return job - else - sleep(timeout) - end - - end -end - - - -while( x = fetch() ) - puts "Got job : #{x}" - if ( x =~ /ping/i ) - puts "PING TEST - sleeping" - sleep 5 - end -end diff --git a/queue/zset/add.rb b/queue/zset/add.rb deleted file mode 100755 index 2d9f27a..0000000 --- a/queue/zset/add.rb +++ /dev/null @@ -1,65 +0,0 @@ -#!/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 diff --git a/queue/zset/run.rb b/queue/zset/run.rb deleted file mode 100755 index b6990ff..0000000 --- a/queue/zset/run.rb +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/ruby - -require "redis" -require 'pp' -@redis = Redis.new(:host => "127.0.0.1") - - - -def fetch(timeout = 1) - job = nil - - loop do - job = @redis.ZRANGE('zset', '0', '0') - - if !job.empty? - # We only have one entry in our array - job = job[0] - - # Remove from the queue - @redis.zrem('zset', job ); - - return job - else - sleep(timeout) - end - - end -end - -seen = [] - -while( x = fetch() ) - puts "Got job : #{x}" - if seen.include?(x) - puts "Already seen #{x}" - break - end - - seen << x - - if ( x =~ /test 2/i ) - puts "TEST 2 - sleeping" - sleep 5 - end -end diff --git a/queue/zset/run_with_threads.rb b/queue/zset/run_with_threads.rb deleted file mode 100644 index cba3eda..0000000 --- a/queue/zset/run_with_threads.rb +++ /dev/null @@ -1,75 +0,0 @@ - -#!/usr/bin/ruby - -require "redis" -require 'pp' - -class Arse - - def initialize(name) - @name = name - @redis = Redis.new(:host => "127.0.0.1") - end - - def fetch(timeout = 1) - loop do - job = nil - @redis.watch("zset") - - job = @redis.zrange('zset', '0', '0') - - if job.is_a?(Array) and !job.empty? - # We only have one entry in our array - job = job[0] - - res = @redis.multi do - # Remove from the queue - @redis.zrem('zset', job ); - end - job = nil if res.nil? - end - - @redis.unwatch - - return job if job.is_a?(String) - - sleep(timeout) - end - end - - def run - Thread.new do - while( x = fetch() ) - print "\n" if x == "test 1" - print "#{@name}:#{x}.. " - - $count[x] += 1 - - if ( rand(10) > 5 ) - sleep 1 - end - - end - - end - end - -end - -$count = Hash.new{|h,k| h[k] = 0} -$threads = [] - -Signal.trap("INT") do - pp $count - exit -end - -$threads = [Arse.new("a").run, Arse.new("b").run, Arse.new("c").run] - -while $threads.any?{|t| t.alive?} do - $threads.each do |t| - next if t.alive? - t.join - end - sleep 1 -end |