Class: MultiRedisMQHelper
- Inherits:
-
Object
- Object
- MultiRedisMQHelper
- Defined in:
- lib/helpers/multi_redis_mq_helper.rb
Instance Attribute Summary collapse
-
#input_channel ⇒ Object
readonly
Returns the value of attribute input_channel.
-
#ip ⇒ Object
readonly
Returns the value of attribute ip.
-
#output_channel ⇒ Object
readonly
Returns the value of attribute output_channel.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#initialize(name, host_ip, port, input_channels, output_channel) ⇒ MultiRedisMQHelper
constructor
A new instance of MultiRedisMQHelper.
-
#pop_input! ⇒ Object
wait until get input.
-
#push_output!(message) ⇒ Object
push directly without lock, message is string.
Constructor Details
#initialize(name, host_ip, port, input_channels, output_channel) ⇒ MultiRedisMQHelper
Returns a new instance of MultiRedisMQHelper.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/helpers/multi_redis_mq_helper.rb', line 8 def initialize(name, host_ip, port, input_channels, output_channel) @name = name @instance = Redis.new(host: host_ip, port: port) puts "#{@instance.keys('*')}" @ip = host_ip @port = port @input_channels = input_channels @input_locks = @input_channels.map { |input_channel| "#{input_channel}_lock" } @input_overall_lock = "#{@input_channels.sort.map{ |channel| channel}.join('_')}_lock" @output_channel = output_channel @input_locks.each { |input_lock| @instance.setnx(input_lock, 0) } @instance.setnx(@input_overall_lock, 0) end |
Instance Attribute Details
#input_channel ⇒ Object (readonly)
Returns the value of attribute input_channel.
6 7 8 |
# File 'lib/helpers/multi_redis_mq_helper.rb', line 6 def input_channel @input_channel end |
#ip ⇒ Object (readonly)
Returns the value of attribute ip.
6 7 8 |
# File 'lib/helpers/multi_redis_mq_helper.rb', line 6 def ip @ip end |
#output_channel ⇒ Object (readonly)
Returns the value of attribute output_channel.
6 7 8 |
# File 'lib/helpers/multi_redis_mq_helper.rb', line 6 def output_channel @output_channel end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
6 7 8 |
# File 'lib/helpers/multi_redis_mq_helper.rb', line 6 def port @port end |
Instance Method Details
#pop_input! ⇒ Object
wait until get input
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/helpers/multi_redis_mq_helper.rb', line 31 def pop_input! inputs = old_overall_lock = new_overall_lock = nil while true has_all_input = true @input_channels.each do |input_channel| has_all_input &&= (@instance.llen(input_channel) != 0) break if !has_all_input end if !has_all_input puts "#{@name} check empty, sleep" sleep(rand(5) + 5) next end puts "#{@name} check exists, go on" # this if means maybe get the overall lock if(old_overall_lock = @instance.get(@input_overall_lock).to_i == 0) new_overall_lock = @instance.incr(@input_overall_lock).to_i if new_overall_lock != 1 puts "#{@name} check conflict, sleep" sleep(rand(10) * 0.1 + 1) next end puts "#{@name} get overall key!" get_key_channels = [] @input_locks.each do |input_lock| this_key = @instance.incr(input_lock).to_i if this_key == 1 # get this key get_key_channels << input_lock end end # not getting all locks if get_key_channels.size < @input_locks.size # give back individual locks get_key_channels.each do |get_key_channel| @instance.set(get_key_channel, 0) end # give back overall lock @instance.set(@input_overall_lock, 0) puts "#{@name} check some inputs has conflict, sleep" sleep(rand(10) * 0.1 + 1) next end puts "#{@name} get all keys, pop inputs" inputs = @input_channels.map do |input_channel| @instance.rpop(input_channel) end @input_locks.each do |input_lock| @instance.set(input_lock, 0) end @instance.set(@input_overall_lock, 0) break end end return inputs end |
#push_output!(message) ⇒ Object
push directly without lock, message is string
26 27 28 |
# File 'lib/helpers/multi_redis_mq_helper.rb', line 26 def push_output!() @instance.rpush(@output_channel, ) end |