simple-redis-mq 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a44fa2d5b85b4118706e7262d33873a8fbda20a
4
- data.tar.gz: bbf69ab5cac0421f758929aa6b2d2d14db9956bf
3
+ metadata.gz: e12858b764899fe777e420bb600c6fae9fa0fac8
4
+ data.tar.gz: 6eb37c0535b289922f431306e22a7d5f6c6dec38
5
5
  SHA512:
6
- metadata.gz: 108f5a7182123c2e3e239c612e1f483c51bd06b28f96cccc1be52f954353be9e91104e86608b130d083157c7304f246c85bd2799eb73b6fd75f57bb7ce0610fa
7
- data.tar.gz: 9679f0e8eea7d515caed3b66a21ee8b162d1574995088e8fafc26704fcb7b32ff63950bd3871856fba3bfc1596de8b996b63e717f9aa200d25552dce1a624fd8
6
+ metadata.gz: 0b18f9218e534e314d42897de44898a410808327ca28314c7b4b71dc28e0e5776c83b63f8bf53fd809303590f76acd71d0da489bec24f4e8a4f4ab53b9d283a2
7
+ data.tar.gz: 615947c7dd4063182a4687c2f4724cf5de26192248fbc511064de251cd53b62fb72ddb6b64646668198ffa6d9455759e9168c17d19a0084654440f1450d9cead
@@ -0,0 +1,101 @@
1
+ require 'redis'
2
+ require 'json'
3
+
4
+ class MultiRedisMQHelper
5
+
6
+ attr_reader :ip, :port, :input_channel, :output_channel
7
+
8
+ def initialize(name, host_ip, port, input_channels, output_channel)
9
+ @name = name
10
+ @instance = Redis.new(host: host_ip, port: port)
11
+ puts "#{@instance.keys('*')}"
12
+ @ip = host_ip
13
+ @port = port
14
+
15
+ @input_channels = input_channels
16
+ @input_locks = @input_channels.map { |input_channel| "#{input_channel}_lock" }
17
+ @input_overall_lock = "#{@input_channels.sort.map{ |channel| channel}.join('_')}_lock"
18
+
19
+ @output_channel = output_channel
20
+
21
+ @input_locks.each { |input_lock| @instance.setnx(input_lock, 0) }
22
+ @instance.setnx(@input_overall_lock, 0)
23
+ end
24
+
25
+ # push directly without lock, message is string
26
+ def push_output!(message)
27
+ @instance.rpush(@output_channel, message)
28
+ end
29
+
30
+ # wait until get input
31
+ def pop_input!
32
+ inputs = old_overall_lock = new_overall_lock = nil
33
+
34
+ while true
35
+ has_all_input = true
36
+ @input_channels.each do |input_channel|
37
+ has_all_input &&= (@instance.llen(input_channel) != 0)
38
+ break if !has_all_input
39
+ end
40
+
41
+ if !has_all_input
42
+ puts "#{@name} check empty, sleep"
43
+ sleep(rand(5) + 5)
44
+ next
45
+ end
46
+
47
+ puts "#{@name} check exists, go on"
48
+
49
+ # this if means maybe get the overall lock
50
+ if(old_overall_lock = @instance.get(@input_overall_lock).to_i == 0)
51
+ new_overall_lock = @instance.incr(@input_overall_lock).to_i
52
+
53
+ if new_overall_lock != 1
54
+ puts "#{@name} check conflict, sleep"
55
+ sleep(rand(10) * 0.1 + 1)
56
+ next
57
+ end
58
+
59
+ puts "#{@name} get overall key!"
60
+
61
+
62
+ get_key_channels = []
63
+ @input_locks.each do |input_lock|
64
+ this_key = @instance.incr(input_lock).to_i
65
+ if this_key == 1 # get this key
66
+ get_key_channels << input_lock
67
+ end
68
+ end
69
+
70
+ # not getting all locks
71
+ if get_key_channels.size < @input_locks.size
72
+
73
+ # give back individual locks
74
+ get_key_channels.each do |get_key_channel|
75
+ @instance.set(get_key_channel, 0)
76
+ end
77
+
78
+ # give back overall lock
79
+ @instance.set(@input_overall_lock, 0)
80
+ puts "#{@name} check some inputs has conflict, sleep"
81
+ sleep(rand(10) * 0.1 + 1)
82
+ next
83
+ end
84
+
85
+ puts "#{@name} get all keys, pop inputs"
86
+ inputs = @input_channels.map do |input_channel|
87
+ @instance.rpop(input_channel)
88
+ end
89
+
90
+ @input_locks.each do |input_lock|
91
+ @instance.set(input_lock, 0)
92
+ end
93
+
94
+ @instance.set(@input_overall_lock, 0)
95
+ break
96
+ end
97
+ end
98
+
99
+ return inputs
100
+ end
101
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-redis-mq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yukai Jin
@@ -24,12 +24,13 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description: a simple redis mq helper, listen to one channel, push to another
27
+ description: one 1-to-1 mq, one *-to-1 mq
28
28
  email: fish1928@outlook.com
29
29
  executables: []
30
30
  extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
+ - lib/helpers/multi_redis_mq_helper.rb
33
34
  - lib/helpers/simple_redis_mq_helper.rb
34
35
  homepage: https://github.com/fish1928/SimpleRedisMQ
35
36
  licenses: