From 2db7fba4d74b663da16625a990f7b7624842712a Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Fri, 18 Dec 2015 15:59:03 +0200 Subject: Added queue-demonstration programs. --- queue/set/add.rb | 22 ++++++++++++++++++++++ queue/set/run.rb | 32 ++++++++++++++++++++++++++++++++ queue/zset/add.rb | 22 ++++++++++++++++++++++ queue/zset/run.rb | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100755 queue/set/add.rb create mode 100755 queue/set/run.rb create mode 100755 queue/zset/add.rb create mode 100755 queue/zset/run.rb diff --git a/queue/set/add.rb b/queue/set/add.rb new file mode 100755 index 0000000..ab40607 --- /dev/null +++ b/queue/set/add.rb @@ -0,0 +1,22 @@ +#!/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 new file mode 100755 index 0000000..f0b7476 --- /dev/null +++ b/queue/set/run.rb @@ -0,0 +1,32 @@ +#!/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 new file mode 100755 index 0000000..cd63503 --- /dev/null +++ b/queue/zset/add.rb @@ -0,0 +1,22 @@ +#!/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.zadd('zset', Time.now.to_i, test) + end +end diff --git a/queue/zset/run.rb b/queue/zset/run.rb new file mode 100755 index 0000000..726e19a --- /dev/null +++ b/queue/zset/run.rb @@ -0,0 +1,38 @@ +#!/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.ZREVRANGE('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 + + + +while( x = fetch() ) + puts "Got job : #{x}" + if ( x =~ /ping/i ) + puts "PING TEST - sleeping" + sleep 5 + end +end -- cgit v1.2.1