rmsg 0.0.2 → 0.0.3

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: 0745a77f2a010b9c427abda0c18fd0d8f943e32f
4
- data.tar.gz: 6a193f367f4a617f3ab99a38ef74d579e23bd0c3
3
+ metadata.gz: 384ab1300d4295b580a206f3607756bb2f3dc54b
4
+ data.tar.gz: 44032af1a2b53f8b9ec0aac81da5b255b3b01af4
5
5
  SHA512:
6
- metadata.gz: 3065150f870b06d1edc886ef2b60db1800ad021b4cd9ce63742219ca41b90d3eb2f38101fdb88d7e34247a94b527df094cc0c279b6f27a03f206146c75f69c8d
7
- data.tar.gz: 2e6aef12fc29e27ecb8646d6d6afa1936a0bb03d353a56b20a95237191aff786ddf7cecc5c93ccea328d686b3202fcafb9f522d52a092d2de669a239733d8f91
6
+ metadata.gz: 358b8c438fcd876e91a25dfec69a0492893be5e65f3780e3d2394c9c4c085445ff1645394a40ab0b9afc1975ec469b44aeb197f7e9653401ce440b98c7764c3c
7
+ data.tar.gz: 981c1c80528e67ca5f6c9ffb6ed38fa20a2b03cea470a145452bc9f805298fbec27a6e6ff48e381eede4d73d6a35e08ac328843bf1139f05b5c5c995df13fe28
@@ -3,3 +3,4 @@ require 'json'
3
3
  require 'rmsg/version'
4
4
  require 'rmsg/rabbit'
5
5
  require 'rmsg/topic'
6
+ require 'rmsg/task'
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Rmsg
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rmsg'
4
+
5
+ rabbit = Rmsg::Rabbit.new
6
+ messages = Rmsg::Task.new(rabbit: rabbit, queue: 'messages')
7
+
8
+ messages.subscribe do |message|
9
+ sleep 1
10
+ p message
11
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rmsg'
4
+
5
+ rabbit = Rmsg::Rabbit.new
6
+ messages = Rmsg::Task.new(rabbit: rabbit, queue: 'messages')
7
+
8
+ message = {
9
+ id: 1,
10
+ body: 'email body'
11
+ }
12
+ messages.publish(message)
13
+
14
+ rabbit.close
@@ -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
@@ -18,18 +18,19 @@ describe 'Topic' do
18
18
  @topic.must_be_instance_of Rmsg::Topic
19
19
  end
20
20
 
21
- # Simulate two processes communicating using a topic and a key
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
- Open3.popen3(consumer) do |stdin, stdout, stderr, wait_thr|
28
- # Wait for the consumer to be up
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.2
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-06 00:00:00.000000000 Z
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/topic_consumer.rb
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/topic_consumer.rb
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