simple-redis-mq 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5a44fa2d5b85b4118706e7262d33873a8fbda20a
4
+ data.tar.gz: bbf69ab5cac0421f758929aa6b2d2d14db9956bf
5
+ SHA512:
6
+ metadata.gz: 108f5a7182123c2e3e239c612e1f483c51bd06b28f96cccc1be52f954353be9e91104e86608b130d083157c7304f246c85bd2799eb73b6fd75f57bb7ce0610fa
7
+ data.tar.gz: 9679f0e8eea7d515caed3b66a21ee8b162d1574995088e8fafc26704fcb7b32ff63950bd3871856fba3bfc1596de8b996b63e717f9aa200d25552dce1a624fd8
@@ -0,0 +1,57 @@
1
+ require 'redis'
2
+ require 'json'
3
+
4
+ class SimpleRedisMQHelper
5
+
6
+ attr_reader :ip, :port, :input_channel, :output_channel
7
+
8
+ def initialize(name, host_ip, port, input_channel, output_channel)
9
+ @name = name
10
+ @instance = Redis.new(host: host_ip, port: port)
11
+ @ip = host_ip
12
+ @port = port
13
+ @input_channel = input_channel
14
+ @input_lock = "#{input_channel}_lock"
15
+ @output_channel = output_channel
16
+
17
+ @instance.setnx(@input_lock, 0)
18
+ end
19
+
20
+ # push directly without lock, message is string
21
+ def push_output!(message)
22
+ @instance.rpush(@output_channel, message)
23
+ end
24
+
25
+ # wait until get input
26
+ def pop_input!
27
+ input = old_lock = new_lock = nil
28
+
29
+ while true
30
+ if @instance.llen(@input_channel) == 0
31
+ puts "#{@name} check empty, sleep"
32
+ sleep(rand(5) + 5)
33
+ next
34
+ end
35
+
36
+ puts "#{@name} check exists, go on"
37
+
38
+ if (old_lock = @instance.get(@input_lock).to_i) == 0
39
+ new_lock = @instance.incr(@input_lock).to_i
40
+
41
+ if new_lock != 1
42
+ puts "#{@name} check conflict, sleep"
43
+ sleep(rand(10) * 0.1 + 1) # sleep 1~2
44
+ next
45
+ end
46
+
47
+ puts "#{@name} get key!"
48
+ input = @instance.rpop(@input_channel)
49
+ @instance.set(@input_lock, 0)
50
+ break
51
+ end
52
+
53
+ end
54
+
55
+ return input
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-redis-mq
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Yukai Jin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: a simple redis mq helper, listen to one channel, push to another
28
+ email: fish1928@outlook.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/helpers/simple_redis_mq_helper.rb
34
+ homepage: https://github.com/fish1928/SimpleRedisMQ
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.6.14
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: lib/helpers/simple_redis_mq_helper
58
+ test_files: []