rmsg 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/rmsg.rb +1 -0
- data/lib/rmsg/task.rb +25 -0
- data/lib/rmsg/version.rb +1 -1
- data/test/bin/task_consumer.rb +11 -0
- data/test/bin/task_publisher.rb +14 -0
- data/test/bin/{topic_consumer.rb → topic_subscriber.rb} +0 -0
- data/test/spec/task_spec.rb +43 -0
- data/test/spec/topic_spec.rb +7 -6
- metadata +11 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 384ab1300d4295b580a206f3607756bb2f3dc54b
|
4
|
+
data.tar.gz: 44032af1a2b53f8b9ec0aac81da5b255b3b01af4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 358b8c438fcd876e91a25dfec69a0492893be5e65f3780e3d2394c9c4c085445ff1645394a40ab0b9afc1975ec469b44aeb197f7e9653401ce440b98c7764c3c
|
7
|
+
data.tar.gz: 981c1c80528e67ca5f6c9ffb6ed38fa20a2b03cea470a145452bc9f805298fbec27a6e6ff48e381eede4d73d6a35e08ac328843bf1139f05b5c5c995df13fe28
|
data/lib/rmsg.rb
CHANGED
data/lib/rmsg/task.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module Rmsg
|
2
|
+
class Task
|
3
|
+
def initialize(params)
|
4
|
+
@rabbit = params[:rabbit]
|
5
|
+
@queue = @rabbit.channel.queue(params[:queue], durable: true)
|
6
|
+
end
|
7
|
+
|
8
|
+
def publish(message)
|
9
|
+
@queue.publish(message.to_json, presistent: true)
|
10
|
+
end
|
11
|
+
|
12
|
+
def subscribe
|
13
|
+
@rabbit.channel.prefetch(1)
|
14
|
+
begin
|
15
|
+
@queue.subscribe(block: true, manual_ack: true) do |delivery_info, metadata, payload|
|
16
|
+
message = JSON.parse(payload, symbolize_names: true)
|
17
|
+
yield message
|
18
|
+
@rabbit.channel.ack(delivery_info.delivery_tag)
|
19
|
+
end
|
20
|
+
rescue Interrupt => _
|
21
|
+
@rabbit.close
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/rmsg/version.rb
CHANGED
File without changes
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require_relative './_init'
|
2
|
+
require 'open3'
|
3
|
+
|
4
|
+
describe 'Task' do
|
5
|
+
before do
|
6
|
+
@rabbit = Rmsg::Rabbit.new
|
7
|
+
@task = Rmsg::Task.new(
|
8
|
+
rabbit: @rabbit,
|
9
|
+
queue: 'messages'
|
10
|
+
)
|
11
|
+
@message = {
|
12
|
+
id: 1,
|
13
|
+
body: 'email body'
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'is a tasks handler over RabbitMQ' do
|
18
|
+
@task.must_be_instance_of Rmsg::Task
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'works in a publisher/consumer scenario' do
|
22
|
+
completed_job = ''
|
23
|
+
publisher = 'bundle exec ruby test/bin/task_publisher.rb'
|
24
|
+
consumer = 'bundle exec ruby test/bin/task_consumer.rb'
|
25
|
+
|
26
|
+
# Start the consumer, wait for it to be up,
|
27
|
+
# start the publisher and wait for the message
|
28
|
+
# on the consumer side. Process, then kill
|
29
|
+
# the long-running consumer.
|
30
|
+
Open3.popen3(consumer) do |stdin, stdout, stderr, wait_thr|
|
31
|
+
sleep 2
|
32
|
+
spawn(publisher)
|
33
|
+
completed_job = stdout.gets
|
34
|
+
Process.kill('INT', wait_thr.pid)
|
35
|
+
end
|
36
|
+
|
37
|
+
eval(completed_job.chomp).must_equal @message
|
38
|
+
end
|
39
|
+
|
40
|
+
after do
|
41
|
+
@rabbit.close
|
42
|
+
end
|
43
|
+
end
|
data/test/spec/topic_spec.rb
CHANGED
@@ -18,18 +18,19 @@ describe 'Topic' do
|
|
18
18
|
@topic.must_be_instance_of Rmsg::Topic
|
19
19
|
end
|
20
20
|
|
21
|
-
|
22
|
-
it 'can publish and read back a message' do
|
21
|
+
it 'works in a pub/sub scenario with a topic and a key' do
|
23
22
|
read_message = ''
|
24
|
-
consumer = 'bundle exec ruby test/bin/topic_consumer.rb'
|
25
23
|
publisher = 'bundle exec ruby test/bin/topic_publisher.rb'
|
24
|
+
subscriber = 'bundle exec ruby test/bin/topic_subscriber.rb'
|
26
25
|
|
27
|
-
|
28
|
-
|
26
|
+
# Start the subscriber, wait for it to be up,
|
27
|
+
# start the publisher and wait for the message
|
28
|
+
# on the subscriber side. Then kill
|
29
|
+
# the long-running subscriber.
|
30
|
+
Open3.popen3(subscriber) do |stdin, stdout, stderr, wait_thr|
|
29
31
|
sleep 2
|
30
32
|
spawn(publisher)
|
31
33
|
read_message = stdout.gets
|
32
|
-
# Kill long-running consumer
|
33
34
|
Process.kill('INT', wait_thr.pid)
|
34
35
|
end
|
35
36
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rmsg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- badshark
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -81,13 +81,17 @@ files:
|
|
81
81
|
- Rakefile
|
82
82
|
- lib/rmsg.rb
|
83
83
|
- lib/rmsg/rabbit.rb
|
84
|
+
- lib/rmsg/task.rb
|
84
85
|
- lib/rmsg/topic.rb
|
85
86
|
- lib/rmsg/version.rb
|
86
87
|
- rmsg.gemspec
|
87
|
-
- test/bin/
|
88
|
+
- test/bin/task_consumer.rb
|
89
|
+
- test/bin/task_publisher.rb
|
88
90
|
- test/bin/topic_publisher.rb
|
91
|
+
- test/bin/topic_subscriber.rb
|
89
92
|
- test/spec/_init.rb
|
90
93
|
- test/spec/rabbit_spec.rb
|
94
|
+
- test/spec/task_spec.rb
|
91
95
|
- test/spec/topic_spec.rb
|
92
96
|
homepage: https://github.com/badshark/rmsg
|
93
97
|
licenses:
|
@@ -114,8 +118,11 @@ signing_key:
|
|
114
118
|
specification_version: 4
|
115
119
|
summary: RabbitMQ messaging.
|
116
120
|
test_files:
|
117
|
-
- test/bin/
|
121
|
+
- test/bin/task_consumer.rb
|
122
|
+
- test/bin/task_publisher.rb
|
118
123
|
- test/bin/topic_publisher.rb
|
124
|
+
- test/bin/topic_subscriber.rb
|
119
125
|
- test/spec/_init.rb
|
120
126
|
- test/spec/rabbit_spec.rb
|
127
|
+
- test/spec/task_spec.rb
|
121
128
|
- test/spec/topic_spec.rb
|