liebre 0.1.21 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +2 -0
  3. data/Gemfile.lock +9 -7
  4. data/{LICENSE → LICENSE.txt} +1 -1
  5. data/README.md +492 -195
  6. data/Rakefile +2 -0
  7. data/lib/liebre.rb +27 -16
  8. data/lib/liebre/actor.rb +11 -0
  9. data/lib/liebre/actor/consumer.rb +80 -0
  10. data/lib/liebre/actor/consumer/callback.rb +34 -0
  11. data/lib/liebre/actor/consumer/core.rb +80 -0
  12. data/lib/liebre/actor/consumer/reporter.rb +84 -0
  13. data/lib/liebre/actor/consumer/resources.rb +47 -0
  14. data/lib/liebre/actor/consumer/resources/config.rb +65 -0
  15. data/lib/liebre/actor/context.rb +40 -0
  16. data/lib/liebre/actor/context/declare.rb +44 -0
  17. data/lib/liebre/actor/context/handler.rb +44 -0
  18. data/lib/liebre/actor/publisher.rb +58 -0
  19. data/lib/liebre/actor/publisher/core.rb +42 -0
  20. data/lib/liebre/actor/publisher/reporter.rb +55 -0
  21. data/lib/liebre/actor/publisher/resources.rb +33 -0
  22. data/lib/liebre/actor/rpc/client.rb +88 -0
  23. data/lib/liebre/actor/rpc/client/core.rb +75 -0
  24. data/lib/liebre/actor/rpc/client/pending.rb +65 -0
  25. data/lib/liebre/actor/rpc/client/reporter.rb +71 -0
  26. data/lib/liebre/actor/rpc/client/resources.rb +62 -0
  27. data/lib/liebre/actor/rpc/client/task.rb +33 -0
  28. data/lib/liebre/actor/rpc/server.rb +74 -0
  29. data/lib/liebre/actor/rpc/server/callback.rb +28 -0
  30. data/lib/liebre/actor/rpc/server/core.rb +75 -0
  31. data/lib/liebre/actor/rpc/server/reporter.rb +72 -0
  32. data/lib/liebre/actor/rpc/server/resources.rb +53 -0
  33. data/lib/liebre/adapter.rb +8 -0
  34. data/lib/liebre/adapter/bunny.rb +23 -0
  35. data/lib/liebre/adapter/bunny/chan.rb +38 -0
  36. data/lib/liebre/adapter/bunny/conn.rb +32 -0
  37. data/lib/liebre/adapter/bunny/exchange.rb +20 -0
  38. data/lib/liebre/adapter/bunny/queue.rb +59 -0
  39. data/lib/liebre/adapter/interface.rb +26 -0
  40. data/lib/liebre/adapter/interface/chan.rb +29 -0
  41. data/lib/liebre/adapter/interface/conn.rb +21 -0
  42. data/lib/liebre/adapter/interface/exchange.rb +13 -0
  43. data/lib/liebre/adapter/interface/queue.rb +37 -0
  44. data/lib/liebre/bridge.rb +72 -0
  45. data/lib/liebre/bridge/channel_builder.rb +36 -0
  46. data/lib/liebre/config.rb +8 -38
  47. data/lib/liebre/engine.rb +61 -0
  48. data/lib/liebre/engine/builder.rb +48 -0
  49. data/lib/liebre/engine/repository.rb +56 -0
  50. data/lib/liebre/engine/state.rb +49 -0
  51. data/lib/liebre/runner.rb +15 -47
  52. data/lib/liebre/version.rb +1 -1
  53. data/liebre.gemspec +9 -7
  54. data/spec/integration/publish_and_consume_spec.rb +71 -0
  55. data/spec/integration/rpc_communication_spec.rb +81 -0
  56. data/spec/integration/start_twice_spec.rb +63 -0
  57. data/spec/liebre/actor/consumer_spec.rb +169 -0
  58. data/spec/liebre/actor/context/declare_spec.rb +69 -0
  59. data/spec/liebre/actor/context/handler_spec.rb +65 -0
  60. data/spec/liebre/actor/publisher_spec.rb +58 -0
  61. data/spec/liebre/actor/rpc/client_spec.rb +126 -0
  62. data/spec/liebre/actor/rpc/server_spec.rb +141 -0
  63. data/spec/liebre/adapter/bunny_spec.rb +66 -0
  64. data/spec/liebre/bridge_spec.rb +54 -0
  65. data/spec/liebre/engine/builder_spec.rb +42 -0
  66. data/spec/liebre/engine_spec.rb +90 -0
  67. data/spec/liebre/version_spec.rb +10 -0
  68. data/spec/spec_helper.rb +2 -9
  69. metadata +97 -58
  70. data/lib/liebre/common.rb +0 -7
  71. data/lib/liebre/common/utils.rb +0 -37
  72. data/lib/liebre/connection_manager.rb +0 -85
  73. data/lib/liebre/publisher.rb +0 -113
  74. data/lib/liebre/runner/consumers.rb +0 -46
  75. data/lib/liebre/runner/starter.rb +0 -44
  76. data/lib/liebre/runner/starter/consumer.rb +0 -129
  77. data/lib/liebre/runner/starter/consumer/handler.rb +0 -35
  78. data/lib/liebre/runner/starter/resources.rb +0 -45
  79. data/lib/liebre/runner/starter/resources/queue_builder.rb +0 -63
  80. data/lib/liebre/runner/starter/rpc.rb +0 -59
  81. data/lib/liebre/tasks.rb +0 -12
  82. data/spec/config/liebre.yml +0 -48
  83. data/spec/config/rabbitmq.yml +0 -35
  84. data/spec/integration_spec.rb +0 -76
  85. data/spec/liebre/config_spec.rb +0 -63
  86. data/spec/liebre/connection_manager_spec.rb +0 -44
  87. data/spec/liebre/publisher_spec.rb +0 -92
  88. data/spec/liebre/runner/consumers_spec.rb +0 -59
  89. data/spec/liebre/runner/starter/consumer_spec.rb +0 -145
  90. data/spec/liebre/runner/starter/resources/queue_builder_spec.rb +0 -69
  91. data/spec/liebre/runner/starter/resources_spec.rb +0 -38
  92. data/spec/liebre/runner/starter/rpc_spec.rb +0 -100
  93. data/spec/liebre/runner/starter_spec.rb +0 -70
  94. data/spec/liebre/runner_spec.rb +0 -54
@@ -1,59 +0,0 @@
1
- module Liebre
2
- class Runner
3
- class Starter
4
- class RPC < Consumer
5
-
6
- def start
7
- @consumer = queue.subscribe(:manual_ack => false) do |_info, meta, payload|
8
- begin
9
- debug_string = "Liebre# Received message for #{klass.name}(#{queue.name}): #{payload} - #{meta}"
10
- start_at = Time.now
11
- call_consumer payload, meta
12
- elapsed_time = (Time.now - start_at).to_f * 1000
13
- log_result debug_string, elapsed_time
14
- rescue StandardError => e
15
- response = :error
16
- logger.error error_string(payload, meta)
17
- rescue Exception => e
18
- response = :error
19
- logger.error error_string(payload, meta)
20
- handler.respond response, info
21
- raise e
22
- end
23
- end
24
- end
25
-
26
- protected
27
-
28
- def call_consumer payload, meta
29
- consumer = klass.new(payload, meta, callback(meta))
30
- consumer.call
31
- end
32
-
33
- private
34
-
35
- def callback meta
36
- opts = {
37
- :routing_key => meta.reply_to,
38
- :correlation_id => meta.correlation_id,
39
- :headers => meta.headers
40
- }
41
-
42
- lambda do |response|
43
- logger.debug "Liebre# Responding with #{response}"
44
- exchange.publish(response, opts)
45
- end
46
- end
47
-
48
- def exchange
49
- channel.default_exchange
50
- end
51
-
52
- def parse_config
53
- config
54
- end
55
-
56
- end
57
- end
58
- end
59
- end
@@ -1,12 +0,0 @@
1
- require 'liebre'
2
-
3
- task :environment
4
-
5
- namespace :liebre do
6
-
7
- desc "Starts Liebre:Runner"
8
- task :run => :environment do
9
- Liebre::Runner.new.start
10
- end
11
-
12
- end
@@ -1,48 +0,0 @@
1
- rpc_request_timeout: 30
2
-
3
- consumers:
4
- some_consumer:
5
- class_name: MyConsumer
6
- rpc: false
7
- pool_size: 1
8
- num_threads: 3
9
- prefetch_count: 5
10
- exchange:
11
- name: "consumer_exchange"
12
- type: "fanout"
13
- opts:
14
- durable: false
15
- queue:
16
- name: "consumer_queue"
17
- opts:
18
- durable: false
19
-
20
- some_rpc:
21
- class_name: MyRPC
22
- rpc: true
23
- connection_name: rpc
24
- pool_size: 1
25
- exchange:
26
- name: "rpc_exchange"
27
- type: "fanout"
28
- opts:
29
- durable: false
30
- queue:
31
- name: "rpc_queue"
32
- opts:
33
- durable: false
34
-
35
- publishers:
36
- some_publisher:
37
- exchange:
38
- name: "consumer_exchange"
39
- type: "fanout"
40
- opts:
41
- durable: false
42
- rpc_publisher:
43
- connection_name: rpc
44
- exchange:
45
- name: "rpc_exchange"
46
- type: "fanout"
47
- opts:
48
- durable: false
@@ -1,35 +0,0 @@
1
- development:
2
- default:
3
- :host: localhost
4
- :port: 5672
5
- :user: guest
6
- :pass: guest
7
- :vhost: /
8
- :threaded: true
9
- :heartbeat: 2
10
- rpc:
11
- :host: localhost
12
- :port: 5672
13
- :user: guest
14
- :pass: guest
15
- :vhost: /
16
- :threaded: true
17
- :heartbeat: 2
18
-
19
- test:
20
- default:
21
- :host: localhost
22
- :port: 5672
23
- :user: guest
24
- :pass: guest
25
- :vhost: /
26
- :threaded: true
27
- :heartbeat: 2
28
- rpc:
29
- :host: localhost
30
- :port: 5672
31
- :user: guest
32
- :pass: guest
33
- :vhost: /
34
- :threaded: true
35
- :heartbeat: 2
@@ -1,76 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe "Integration" do
4
-
5
- class MyConsumer
6
-
7
- def initialize payload, meta
8
- @payload = payload
9
- @meta = meta
10
- end
11
-
12
- end
13
-
14
- class MyRPC
15
-
16
- def initialize payload, meta, callback
17
- @payload = payload
18
- @meta = meta
19
- @callback = callback
20
- end
21
-
22
- def call
23
- @callback.call(@payload)
24
- end
25
-
26
- end
27
-
28
- let :config_path do
29
- File.expand_path("../config/liebre.yml" ,__FILE__)
30
- end
31
-
32
- let :connection_path do
33
- File.expand_path("../config/rabbitmq.yml" ,__FILE__)
34
- end
35
-
36
- before do
37
- Liebre::ConnectionManager.instance_variable_set :@singleton__instance__, nil
38
- Liebre::Config.config_path = config_path
39
- Liebre::Config.connection_path = connection_path
40
- end
41
-
42
- let(:consumer) { double 'consumer' }
43
-
44
- it do
45
-
46
- main_thread = Thread.new do
47
- server = Liebre::Runner.new
48
- server.start
49
- end
50
-
51
- sleep 0.1
52
-
53
- publisher = Liebre::Publisher.new("some_publisher")
54
-
55
- allow(MyConsumer).to receive(:new).with("hello", anything).and_return consumer
56
-
57
- #the consumer returns first :ack, then :reject and the message gets requed, then :error, and the message turns dead-lettered
58
- expect(consumer).to receive(:call).and_return :ack, :reject, :error
59
-
60
- publisher.enqueue "hello", :routing_key => "consumer_queue" #:ack
61
- publisher.enqueue "hello", :routing_key => "consumer_queue" #:reject then :error
62
-
63
-
64
-
65
- rpc_publisher = Liebre::Publisher.new("rpc_publisher")
66
-
67
- param = "return this string"
68
-
69
- result = rpc_publisher.rpc param, :routing_key => "rpc_queue"
70
-
71
- expect(result).to eq param
72
-
73
- sleep 0.1
74
- end
75
-
76
- end
@@ -1,63 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe Liebre::Config do
4
-
5
- let :config_path do
6
- File.expand_path("../../config/liebre.yml" ,__FILE__)
7
- end
8
-
9
- let :connection_path do
10
- File.expand_path("../../config/rabbitmq.yml" ,__FILE__)
11
- end
12
-
13
- before do
14
- described_class.config_path = config_path
15
- described_class.connection_path = connection_path
16
- described_class.env = "some_env"
17
- end
18
-
19
- subject { described_class.new }
20
-
21
- describe '.config_path and .connection_path and .env' do
22
-
23
- it do
24
- expect(described_class.config_path).to eq config_path
25
- expect(described_class.connection_path).to eq connection_path
26
- expect(described_class.env).to eq "some_env"
27
- end
28
- end
29
-
30
- describe '#consumers' do
31
-
32
- let(:consumer_names) { %w{ some_consumer some_rpc } }
33
-
34
- let(:consumer_config) do
35
- {
36
- 'class_name' => "MyConsumer",
37
- 'rpc' => false
38
- }
39
- end
40
-
41
- let(:rpc_config) do
42
- {
43
- 'class_name' => "MyRPC",
44
- 'rpc' => true
45
- }
46
- end
47
-
48
- it do
49
-
50
- expect(subject.consumers.keys).to eq consumer_names
51
-
52
- expect(subject.consumers['some_consumer']['class_name']).to eq consumer_config['class_name']
53
- expect(subject.consumers['some_consumer']['rpc']).to eq consumer_config['rpc']
54
- expect(subject.consumers['some_consumer']['prefetch_count']).to eq 5
55
-
56
- expect(subject.consumers['some_rpc']['class_name']).to eq rpc_config['class_name']
57
- expect(subject.consumers['some_rpc']['rpc']).to eq rpc_config['rpc']
58
-
59
- end
60
-
61
- end
62
-
63
- end
@@ -1,44 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe Liebre::ConnectionManager do
4
-
5
- let :connection_path do
6
- File.expand_path("../../config/rabbitmq.yml" ,__FILE__)
7
- end
8
-
9
- before do
10
- Liebre::Config.connection_path = connection_path
11
- end
12
-
13
- subject { described_class.instance }
14
-
15
- describe '.start and .get' do
16
-
17
- it do
18
- subject.start
19
-
20
- bunny = subject.get :default
21
-
22
- expect(bunny.connected?).to be true
23
-
24
- end
25
- end
26
-
27
- describe '.restart' do
28
-
29
- before do
30
- subject.start
31
- end
32
-
33
- it do
34
-
35
- subject.restart
36
-
37
- bunny = subject.get :rpc
38
-
39
- expect(bunny.connected?).to be true
40
-
41
- end
42
- end
43
-
44
- end
@@ -1,92 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe Liebre::Publisher do
4
-
5
- let :exchange_config do
6
- {
7
- 'name' => "test_exchange",
8
- 'type' => "direct",
9
- 'opts' => {
10
- 'durable' => false
11
- }
12
- }
13
- end
14
-
15
- let :publishers_config do
16
- {
17
- 'test_publisher' => {
18
- 'exchange' =>
19
- exchange_config
20
- }
21
- }
22
- end
23
-
24
- let(:channel) { double 'channel' }
25
- let(:connection_manager) { double 'connection_manager' }
26
- let(:exchange) { double 'exchange', :name => "exchange" }
27
-
28
- before do
29
- allow_any_instance_of(Liebre::Config).to receive(:publishers).and_return publishers_config
30
- allow_any_instance_of(Liebre::Config).to receive(:rpc_request_timeout).and_return 10
31
-
32
- allow(Liebre::ConnectionManager).to receive(:instance).and_return connection_manager
33
- expect(connection_manager).to receive(:ensure_started)
34
-
35
- expect(Liebre::Common::Utils).to receive(:create_exchange).
36
- with(channel, exchange_config).and_return exchange
37
-
38
- end
39
-
40
- subject { described_class.new 'test_publisher' }
41
-
42
- describe "#enqueue" do
43
- before do
44
- expect(connection_manager).to receive(:channel_for).with(:default).and_return channel
45
- end
46
-
47
- it do
48
- message = "abc"
49
- expect(exchange).to receive(:publish).with message, {}
50
-
51
- subject.enqueue message
52
- end
53
- end
54
-
55
- describe "#enqueue_and_wait" do
56
-
57
- let(:correlation_id) { "correlation_id" }
58
- let(:reply_queue_name) { "test_publisher_callback_#{correlation_id}" }
59
- let(:reply_queue) { double "reply_queue", :name => reply_queue_name }
60
- let(:delivery_info) { double "delivery_info", :consumer_tag => "tag"}
61
- let(:consumer) { double 'consumer' }
62
- let(:connection) { double 'connection' }
63
- let(:consumers) { {"tag" => consumer} }
64
-
65
- before do
66
- expect(connection_manager).to receive(:get).with(:default).and_return connection
67
- expect(connection).to receive(:create_channel).and_return channel
68
- expect(channel).to receive(:prefetch).with 1
69
- expect(channel).to receive(:queue).with(reply_queue_name, :exclusive => true, :auto_delete => true).
70
- and_return reply_queue
71
- expect(channel).to receive(:close)
72
- end
73
-
74
- it do
75
- message = "question"
76
- answer = "answer"
77
- expect(exchange).to receive(:publish).with message,
78
- {:correlation_id => correlation_id, :reply_to => reply_queue_name}
79
-
80
- expect(reply_queue).to receive(:subscribe).with(:block => false) do |&block|
81
- expect(channel).to receive(:consumers).and_return consumers
82
- expect(consumer).to receive(:cancel)
83
-
84
- block.call(delivery_info, {:correlation_id => correlation_id}, answer)
85
- end
86
-
87
- result = subject.enqueue_and_wait message, :correlation_id => correlation_id
88
- expect(result).to eq answer
89
- end
90
- end
91
-
92
- end
@@ -1,59 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe Liebre::Runner::Consumers do
4
-
5
- let(:conn) { double 'conn' }
6
-
7
- let(:first_expected_config) do
8
- {
9
- "class_name" => "MyConsumer",
10
- "num_threads" => 3,
11
- "rpc" => false
12
- }
13
- end
14
-
15
- let(:second_expected_config) do
16
- {
17
- "class_name" => "MyRPC",
18
- "rpc" => true
19
- }
20
- end
21
-
22
- let(:consumers_config) do
23
- {
24
- "some_consumer" => first_expected_config,
25
- "some_rpc" => second_expected_config
26
- }
27
- end
28
-
29
- before do
30
- allow(subject).to receive(:consumers).and_return consumers_config
31
- end
32
-
33
- subject { described_class.new(conn) }
34
-
35
- describe "#consumer_names" do
36
- it "returns all names" do
37
- expect(subject.consumer_names).to eq ["some_consumer", "some_rpc"]
38
- end
39
- end
40
-
41
- describe "#start_all" do
42
- let(:first_starter) { double 'first_starter' }
43
- let(:second_starter) { double 'second_starter' }
44
-
45
- it "starts the given consumers" do
46
- expect(Liebre::Runner::Starter).to receive(:new).exactly(3).times.
47
- with(conn, first_expected_config).and_return(first_starter)
48
-
49
- expect(Liebre::Runner::Starter).to receive(:new).
50
- with(conn, second_expected_config).and_return(second_starter)
51
-
52
- expect(first_starter ).to receive(:start).exactly(3).times
53
- expect(second_starter).to receive(:start)
54
-
55
- subject.start_all
56
- end
57
- end
58
-
59
- end