fleck 0.7.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +11 -10
  3. data/CHANGELOG.md +89 -71
  4. data/Gemfile +6 -4
  5. data/examples/actions.rb +60 -53
  6. data/examples/blocking_consumer.rb +42 -42
  7. data/examples/consumer_initialization.rb +44 -42
  8. data/examples/deprecation.rb +50 -57
  9. data/examples/example.rb +76 -74
  10. data/examples/expired.rb +72 -76
  11. data/examples/fanout.rb +62 -64
  12. data/fleck.gemspec +37 -36
  13. data/lib/fleck/client.rb +124 -124
  14. data/lib/fleck/configuration.rb +149 -144
  15. data/lib/fleck/consumer.rb +7 -287
  16. data/lib/fleck/core/consumer/action_param.rb +106 -0
  17. data/lib/fleck/core/consumer/actions.rb +79 -0
  18. data/lib/fleck/core/consumer/base.rb +111 -0
  19. data/lib/fleck/core/consumer/configuration.rb +69 -0
  20. data/lib/fleck/core/consumer/decorators.rb +77 -0
  21. data/lib/fleck/core/consumer/helpers_definers.rb +55 -0
  22. data/lib/fleck/core/consumer/logger.rb +88 -0
  23. data/lib/fleck/core/consumer/request.rb +100 -0
  24. data/lib/fleck/core/consumer/response.rb +77 -0
  25. data/lib/fleck/core/consumer/response_helpers.rb +81 -0
  26. data/lib/fleck/core/consumer/validation.rb +163 -0
  27. data/lib/fleck/core/consumer.rb +166 -0
  28. data/lib/fleck/core.rb +9 -0
  29. data/lib/fleck/loggable.rb +15 -10
  30. data/lib/fleck/{hash_with_indifferent_access.rb → utilities/hash_with_indifferent_access.rb} +80 -85
  31. data/lib/fleck/utilities/host_rating.rb +104 -0
  32. data/lib/fleck/version.rb +6 -3
  33. data/lib/fleck.rb +81 -72
  34. metadata +42 -33
  35. data/lib/fleck/consumer/request.rb +0 -52
  36. data/lib/fleck/consumer/response.rb +0 -80
  37. data/lib/fleck/host_rating.rb +0 -74
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d7efdcd95bd6040ca4c66e627a181d7311ede374
4
- data.tar.gz: eeebc0d7dae8f532f69deb5e18671b8d4b7a9812
2
+ SHA256:
3
+ metadata.gz: dca817403e895fd00e7d95e54d278f19e13f0941960b276b501d637c9330f588
4
+ data.tar.gz: 93e27ca7cbd491413e0fc9199c7ba259d05c2d6bef11403ee7feaf403b00453a
5
5
  SHA512:
6
- metadata.gz: cf10fa191317c2b43dbc9e2cc3dc2adfe28ea2131b85794cfe752744b774afdf1590d27803dc68379db8ef86f4b654f93d080c4419f58afc9971c05915999e48
7
- data.tar.gz: 70b3b65e4bbfec3f0ac42f52105cb75aeb99ae61c38a57e5f0bba0640a5f87ca332a012f546b76cc94f60da115061227833567dedcbcca984aeae33cfaa5b78c
6
+ metadata.gz: b6079c6cb3bc70d79e4cc576f7e358bb1043bf996700c073147155bb7c27178836a7f23a67bf1cae51c0a4665838f440c7b39bea64104c5978eb8a9f7eef7375
7
+ data.tar.gz: 6d12f8ffd73af744fd61d3620fc955a310a3b78b4bbf6f06b6070dae9c43f782b1cea0c12e158284e97af8a38ad0f5e34e7d1a16bbfff74edcb09ccc75334228
data/.gitignore CHANGED
@@ -1,10 +1,11 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
10
- /*.gem
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /*.gem
11
+ /.rubocop.yml
data/CHANGELOG.md CHANGED
@@ -1,71 +1,89 @@
1
- # CHANGELOG #
2
-
3
- ## develop ##
4
-
5
- ## v0.7.0 (21 June 2016)
6
- - **NEW** Added multihost support to Fleck configuration, that allows to manage network failure situations and to choose the best options from the list of available hosts.
7
- This feature uses `Fleck::HostRating` to collect TCP latency data about each provided host, so that when a new connection is required, the host with lowest latency
8
- will be choosed. If a host becomes unreachable, it gets the lowest rating and will be used as the last option, allowing you to automatically manage network failures.
9
- - **NEW** Implemented a basic log filter for headers and params in requests and responses.
10
- - **NEW** Log each processed request in `Fleck::Consumer::Response`.
11
- - **NEW** Implemented `:deprecated?` method for `Fleck::Consumer::Response`.
12
- - **NEW** Store client IP address to requests headers, in order to be able to trace requests origin when multiple clients making requests to the same consumer type.
13
-
14
- ## v0.6.0 (16 June 2016)
15
- - **NEW** __(BREAKING CHANGE)__ Use `"fleck"` exchange for RPC simulation, so that reply queues could be used in a RabbitMQ Federation configuration.
16
- Be careful when upgrading `Fleck::Consumer` from version `v0.5.x` or below, because now `Fleck::Consumer` will send responses to a `:direct` exchange
17
- named `"fleck"`. If there're `Fleck::Clients` that are at version `v0.5.x` or below, they will not be able to receive the response from consumers of a
18
- newer version.
19
- - **NEW** Added a filter that prevents from using reserved `Fleck::Consumer` methods as actions.
20
- - **NEW** Implemented the feature that allows to start consumer in a blocking way.
21
- - **NEW** Added `:prefetch` and `:mandatory` options to `Fleck::Consumer` configuration options.
22
-
23
- ## v0.5.1 (20 April 2016)
24
- - **FIX** Don't expire requests with multiple responses if any response is received. Treat that kind of request as expired if no response has been received
25
- until the request expiration.
26
-
27
- ## v0.5.0 (20 April 2016) ##
28
- - **NEW** Added `:autostart` option to `Fleck::Consumer` configuration, so that the developer could decide to start the consumer manually or automatically. By default
29
- the consumer will start automatically.
30
- - **NEW** Implemented the feature that allows to define an initialization block for `Fleck::Consumer`. This feature should be used to initialize consumer instance
31
- variables so that it is not necessary to overwrite `Fleck::Consumer#initialize` method.
32
- - **NEW** Implemented the feature that allows to define a map of actions to consumer methods, so that requests actions are automatically mapped to
33
- consumer methods.
34
- - **NEW** Implemented `#expired?` method for `Fleck::Client::Request`, that tells if the request is expired or not. It makes possible to
35
- distinguish service unavailable responses from expired requests.
36
- - **NEW** Added `:concurrency` option to `Fleck::Client`, that allows to specify the concurrency level for responses parsing.
37
- - **NEW** Add `:version` option to `Fleck::Client#request` and implement `#version` method for `Fleck::Consumer::Request`.
38
- - **NEW** Implemented `#request` and `#response` methods for `Fleck::Consumer`, so that you don't have to pass them as argument every time you
39
- delegate the logic to a different method.
40
- - **NEW** Implemented the feature that allows to deprecate actions within a consumer. Now you can call `deprecated!` inside a consumer to
41
- reply with a response that is marked as **deprecated**.
42
- - **NEW** Add `app_name` configuration, that allows to configure the default `app_id` to set for RabbitMQ messages.
43
- - **NEW** Add process ID to logs, so that if you have multiple instances of the same application writting to the same log file, you'll be able to filter logs by process ID. Also changed logs format.
44
-
45
- ## v0.4.1 (18 April 2016) ##
46
- - **FIX** Fixed a bug of `Fleck::Consumer::Request` class, that was causing errors when RabbitMQ message header wasn't set.
47
-
48
- ## v0.4.0 (15 April 2016) ##
49
- - **NEW** Support different types of exchanges in both `Fleck::Client` and `Fleck::Consumer`.
50
- - **FIX** Use `auto_delete` queue for `Fleck::Client`, so that it is deleted when the client is terminated.
51
- - **NEW** Add `:rmq_options` option to `Fleck::Client::Request`, which can be used to pass options like `:persistent`, `mandatory`, etc.
52
- to RabbitMQ message on publish.
53
- - **NEW** Store `:headers` attribute of `Fleck::Client::Request` into RabbitMQ message `:headers`, so that in the future only
54
- `:params` option will be converted to JSON.
55
- - **NEW** Add `:action` option to `Fleck::Client::Request`, which will replace the action passed within `:headers` hash.
56
-
57
- ## v0.3.0 (1 April 2016)
58
- - **FIX** Use `:compat` mode when using `Oj` gem to dump/load JSON content.
59
- - **FIX** Prevent unnecessary `Fleck::Request` lock for response reception if the response already received.
60
- - **NEW** Implemented a timeout functionality for asynchronous request, so that if the request isn't completed within that timeout, it will be canceled and removed from
61
- requests list.
62
- - **NEW** Set `mandatory: true` when publishing the request to RabbitMQ for both `Fleck::Client` and `Fleck::Consumer`, in order to ensure that requests and responses
63
- are enqueued for sure to RabbitMQ.
64
- - **NEW** Implemented `#pause` and `#resume` methods for `Fleck::Consumer`, that allows to pause the consumer from consuming messages.
65
- - **NEW** `Fleck::Consumer::Response#reject!` support, that allows to reject the processed message. By default `requeue` parameter is set to `false`, so that
66
- failed requests aren't requeued. You should call `response.reject(requeue: true)` within the `on_message` method, if you want to requeue the processing
67
- message.
68
-
69
- ## v0.2.0 (18 February 2016)
70
- - **NEW** `timeout` (synchronous requests only) and `queue` support for `Fleck::Client#request`
71
- - **NEW** Keywords arguments for `Fleck::Client#request` method (ex. `client.request(headers: {h1: v1, ...}, params: {p1: v2, ...}`)
1
+ # CHANGELOG #
2
+
3
+ ## develop ##
4
+
5
+ - **BREAKING CHANGE** Updated minimum required Ruby version to >= 2.5.
6
+ - **NEW** Refactore `Fleck::Consumer` in order to have a better code structure.
7
+ - **NEW** Implemented response helpers, which allow to terminate the request with desired status code and message.
8
+ - **NEW** Implemented action decorators, which allow to define an action and it's parameters directly before the method.
9
+ - **NEW** Implemented action params automatic validation, which can be set by using action decorators.
10
+
11
+ ## v1.0.1 (2 April 2020) ##
12
+
13
+ - **BREAKING CHANGE** Updateg gemset, in order to be able to use new versions of Ruby.
14
+
15
+ ## v0.7.0 (21 June 2016) ##
16
+
17
+ - **NEW** Added multihost support to Fleck configuration, that allows to manage network failure situations and to choose the best options from the list of available hosts.
18
+ This feature uses `Fleck::HostRating` to collect TCP latency data about each provided host, so that when a new connection is required, the host with lowest latency
19
+ will be choosed. If a host becomes unreachable, it gets the lowest rating and will be used as the last option, allowing you to automatically manage network failures.
20
+ - **NEW** Implemented a basic log filter for headers and params in requests and responses.
21
+ - **NEW** Log each processed request in `Fleck::Consumer::Response`.
22
+ - **NEW** Implemented `:deprecated?` method for `Fleck::Consumer::Response`.
23
+ - **NEW** Store client IP address to requests headers, in order to be able to trace requests origin when multiple clients making requests to the same consumer type.
24
+
25
+ ## v0.6.0 (16 June 2016)##
26
+
27
+ - **NEW** __(BREAKING CHANGE)__ Use `"fleck"` exchange for RPC simulation, so that reply queues could be used in a RabbitMQ Federation configuration.
28
+ Be careful when upgrading `Fleck::Consumer` from version `v0.5.x` or below, because now `Fleck::Consumer` will send responses to a `:direct` exchange
29
+ named `"fleck"`. If there're `Fleck::Clients` that are at version `v0.5.x` or below, they will not be able to receive the response from consumers of a
30
+ newer version.
31
+ - **NEW** Added a filter that prevents from using reserved `Fleck::Consumer` methods as actions.
32
+ - **NEW** Implemented the feature that allows to start consumer in a blocking way.
33
+ - **NEW** Added `:prefetch` and `:mandatory` options to `Fleck::Consumer` configuration options.
34
+
35
+ ## v0.5.1 (20 April 2016) ##
36
+
37
+ - **FIX** Don't expire requests with multiple responses if any response is received. Treat that kind of request as expired if no response has been received
38
+ until the request expiration.
39
+
40
+ ## v0.5.0 (20 April 2016) ##
41
+
42
+ - **NEW** Added `:autostart` option to `Fleck::Consumer` configuration, so that the developer could decide to start the consumer manually or automatically. By default
43
+ the consumer will start automatically.
44
+ - **NEW** Implemented the feature that allows to define an initialization block for `Fleck::Consumer`. This feature should be used to initialize consumer instance
45
+ variables so that it is not necessary to overwrite `Fleck::Consumer#initialize` method.
46
+ - **NEW** Implemented the feature that allows to define a map of actions to consumer methods, so that requests actions are automatically mapped to
47
+ consumer methods.
48
+ - **NEW** Implemented `#expired?` method for `Fleck::Client::Request`, that tells if the request is expired or not. It makes possible to
49
+ distinguish service unavailable responses from expired requests.
50
+ - **NEW** Added `:concurrency` option to `Fleck::Client`, that allows to specify the concurrency level for responses parsing.
51
+ - **NEW** Add `:version` option to `Fleck::Client#request` and implement `#version` method for `Fleck::Consumer::Request`.
52
+ - **NEW** Implemented `#request` and `#response` methods for `Fleck::Consumer`, so that you don't have to pass them as argument every time you
53
+ delegate the logic to a different method.
54
+ - **NEW** Implemented the feature that allows to deprecate actions within a consumer. Now you can call `deprecated!` inside a consumer to
55
+ reply with a response that is marked as **deprecated**.
56
+ - **NEW** Add `app_name` configuration, that allows to configure the default `app_id` to set for RabbitMQ messages.
57
+ - **NEW** Add process ID to logs, so that if you have multiple instances of the same application writting to the same log file, you'll be able to filter logs by process ID. Also changed logs format.
58
+
59
+ ## v0.4.1 (18 April 2016) ##
60
+
61
+ - **FIX** Fixed a bug of `Fleck::Consumer::Request` class, that was causing errors when RabbitMQ message header wasn't set.
62
+
63
+ ## v0.4.0 (15 April 2016) ##
64
+
65
+ - **NEW** Support different types of exchanges in both `Fleck::Client` and `Fleck::Consumer`.
66
+ - **FIX** Use `auto_delete` queue for `Fleck::Client`, so that it is deleted when the client is terminated.
67
+ - **NEW** Add `:rmq_options` option to `Fleck::Client::Request`, which can be used to pass options like `:persistent`, `mandatory`, etc.
68
+ to RabbitMQ message on publish.
69
+ - **NEW** Store `:headers` attribute of `Fleck::Client::Request` into RabbitMQ message `:headers`, so that in the future only
70
+ `:params` option will be converted to JSON.
71
+ - **NEW** Add `:action` option to `Fleck::Client::Request`, which will replace the action passed within `:headers` hash.
72
+
73
+ ## v0.3.0 (1 April 2016) ##
74
+
75
+ - **FIX** Use `:compat` mode when using `Oj` gem to dump/load JSON content.
76
+ - **FIX** Prevent unnecessary `Fleck::Request` lock for response reception if the response already received.
77
+ - **NEW** Implemented a timeout functionality for asynchronous request, so that if the request isn't completed within that timeout, it will be canceled and removed from
78
+ requests list.
79
+ - **NEW** Set `mandatory: true` when publishing the request to RabbitMQ for both `Fleck::Client` and `Fleck::Consumer`, in order to ensure that requests and responses
80
+ are enqueued for sure to RabbitMQ.
81
+ - **NEW** Implemented `#pause` and `#resume` methods for `Fleck::Consumer`, that allows to pause the consumer from consuming messages.
82
+ - **NEW** `Fleck::Consumer::Response#reject!` support, that allows to reject the processed message. By default `requeue` parameter is set to `false`, so that
83
+ failed requests aren't requeued. You should call `response.reject(requeue: true)` within the `on_message` method, if you want to requeue the processing
84
+ message.
85
+
86
+ ## v0.2.0 (18 February 2016) ##
87
+
88
+ - **NEW** `timeout` (synchronous requests only) and `queue` support for `Fleck::Client#request`
89
+ - **NEW** Keywords arguments for `Fleck::Client#request` method (ex. `client.request(headers: {h1: v1, ...}, params: {p1: v2, ...}`)
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in fleck.gemspec
4
- gemspec
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in fleck.gemspec
6
+ gemspec
data/examples/actions.rb CHANGED
@@ -1,53 +1,60 @@
1
- #!/usr/bin/env ruby
2
- # encoding: utf-8
3
-
4
- require 'fleck'
5
-
6
- user = ENV['USER'] || 'guest'
7
- pass = ENV['PASS'] || 'guest'
8
-
9
- CONCURRENCY = (ENV['CONCURRENCY'] || 2).to_i
10
- SAMPLES = (ENV['SAMPLES'] || 10).to_i
11
-
12
- Fleck.configure do |config|
13
- config.default_user = user
14
- config.default_pass = pass
15
- config.loglevel = Logger::DEBUG
16
- end
17
-
18
- connection = Fleck.connection(host: "127.0.0.1", port: 5672, user: user, pass: pass, vhost: "/")
19
- client = Fleck::Client.new(connection, "actions.example.queue", concurrency: CONCURRENCY.to_i)
20
-
21
- class MyConsumer < Fleck::Consumer
22
- configure queue: 'actions.example.queue', concurrency: CONCURRENCY.to_i
23
- actions :hello, ciao: 'my_custom_method', aloha: 'my_aloha'
24
-
25
- def hello
26
- response.body = "Hello!"
27
- end
28
-
29
- def my_custom_method
30
- response.body = "Ciao!"
31
- end
32
-
33
- def my_aloha
34
- response.body = "Aloha!"
35
- end
36
-
37
- def not_an_action
38
- logger.warn("I'm not an action, so you should not be able to call me!")
39
- end
40
- end
41
-
42
- actions = [:hello, :ciao, :aloha, :not_an_action]
43
-
44
- SAMPLES.to_i.times do |num|
45
- action = actions[(rand * actions.size).to_i]
46
- client.request(action: action, params: {num: num}, timeout: 5) do |request, response|
47
- if response.status == 200
48
- Fleck.logger.info "ACTION: (#{action.inspect}) #{response.body}"
49
- else
50
- Fleck.logger.error "ACTION: (#{action.inspect}) #{response.errors.join(", ")}"
51
- end
52
- end
53
- end
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'fleck'
5
+
6
+ user = ENV['USER'] || 'guest'
7
+ pass = ENV['PASS'] || 'guest'
8
+
9
+ CONCURRENCY = (ENV['CONCURRENCY'] || 2).to_i
10
+ SAMPLES = (ENV['SAMPLES'] || 10).to_i
11
+ QUEUE = 'actions.example.queue'
12
+
13
+ Fleck.configure do |config|
14
+ config.default_user = user
15
+ config.default_pass = pass
16
+ config.loglevel = Logger::DEBUG
17
+ end
18
+
19
+ connection = Fleck.connection(host: '127.0.0.1', port: 5672, user: user, pass: pass, vhost: '/')
20
+ client = Fleck::Client.new(connection, QUEUE, concurrency: CONCURRENCY.to_i)
21
+
22
+ class MyConsumer < Fleck::Consumer
23
+ configure queue: QUEUE, concurrency: CONCURRENCY.to_i
24
+
25
+ action 'hello', "An action which returns 'Hello'"
26
+ def hello
27
+ ok! 'Hello!'
28
+ end
29
+
30
+ action 'ciao', "An action which returns 'Ciao'"
31
+ param :world, type: 'boolean', required: true, default: false
32
+ def my_custom_method
33
+ ok! params[:world] ? 'Ciao, Mondo!' : 'Ciao!'
34
+ end
35
+
36
+ action :aloha
37
+ param :number, type: 'integer', clamp: [1, 10], required: true
38
+ param :name, type: 'string', default: 'John Doe', required: true
39
+ def my_aloha
40
+ ok! "#{params[:number]}. Aloha, #{params[:name]}!"
41
+ end
42
+
43
+ def not_an_action
44
+ logger.warn("I'm not an action, so you should not be able to call me!")
45
+ end
46
+ end
47
+
48
+ actions = %i[hello ciao aloha not_an_action]
49
+
50
+ SAMPLES.to_i.times do |num|
51
+ action = actions.sample
52
+ name = ['John Doe', 'Willie Wonka', 'Billie Smith'].sample
53
+ client.request(action: action, params: { num: num, name: name, number: rand * 100, world: %w[yes no].sample }, timeout: 5) do |_, response|
54
+ if response.status == 200
55
+ Fleck.logger.info "ACTION: (#{action.inspect}) #{response.body}"
56
+ else
57
+ Fleck.logger.error "ACTION: (#{action.inspect}) #{response.errors.join(', ')} --- #{response.body}"
58
+ end
59
+ end
60
+ end
@@ -1,42 +1,42 @@
1
- #!/usr/bin/env ruby
2
- # encoding: utf-8
3
-
4
- require 'fleck'
5
-
6
- user = ENV['USER'] || 'guest'
7
- pass = ENV['PASS'] || 'guest'
8
-
9
- CONCURRENCY = (ENV['CONCURRENCY'] || 2).to_i
10
- SAMPLES = (ENV['SAMPLES'] || 10).to_i
11
-
12
- Fleck.configure do |config|
13
- config.default_user = user
14
- config.default_pass = pass
15
- config.loglevel = Logger::DEBUG
16
- end
17
-
18
- connection = Fleck.connection(host: "127.0.0.1", port: 5672, user: user, pass: pass, vhost: "/")
19
- client = Fleck::Client.new(connection, "blocking.consumer.example.queue")
20
-
21
- class MyConsumer < Fleck::Consumer
22
- configure queue: 'blocking.consumer.example.queue', autostart: false
23
- actions :quit
24
-
25
- initialize do
26
- @value = "MY CONSUMER :) #{self.object_id}"
27
- end
28
-
29
- def quit
30
- logger.debug "Quit message received, but I'm goint to sleep for 2 seconds ..."
31
- sleep 2
32
- logger.debug "Let's terminate this example!"
33
- terminate
34
- end
35
- end
36
-
37
- client.request(action: 'quit', timeout: 5, async: true)
38
-
39
- MyConsumer.start(block: true)
40
-
41
- puts "We did it :)"
42
- sleep 0.01 # Give some time to Bunny to cancel subscribtions and to close channels and connections
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'fleck'
5
+
6
+ user = ENV['USER'] || 'guest'
7
+ pass = ENV['PASS'] || 'guest'
8
+
9
+ CONCURRENCY = (ENV['CONCURRENCY'] || 2).to_i
10
+ SAMPLES = (ENV['SAMPLES'] || 10).to_i
11
+
12
+ Fleck.configure do |config|
13
+ config.default_user = user
14
+ config.default_pass = pass
15
+ config.loglevel = Logger::DEBUG
16
+ end
17
+
18
+ connection = Fleck.connection(host: "127.0.0.1", port: 5672, user: user, pass: pass, vhost: "/")
19
+ client = Fleck::Client.new(connection, "blocking.consumer.example.queue")
20
+
21
+ class MyConsumer < Fleck::Consumer
22
+ configure queue: 'blocking.consumer.example.queue', autostart: false
23
+ actions :quit
24
+
25
+ initialize do
26
+ @value = "MY CONSUMER :) #{object_id}"
27
+ end
28
+
29
+ def quit
30
+ logger.debug "Quit message received, but I'm goint to sleep for 2 seconds ..."
31
+ sleep 2
32
+ logger.debug "Let's terminate this example!"
33
+ Ztimer.async { self.class.terminate }
34
+ end
35
+ end
36
+
37
+ client.request(action: 'quit', timeout: 5, async: true)
38
+
39
+ MyConsumer.start(block: true)
40
+
41
+ puts "We did it :)"
42
+ sleep 0.01 # Give some time to Bunny to cancel subscribtions and to close channels and connections
@@ -1,42 +1,44 @@
1
- #!/usr/bin/env ruby
2
- # encoding: utf-8
3
-
4
- require 'fleck'
5
-
6
- user = ENV['USER'] || 'guest'
7
- pass = ENV['PASS'] || 'guest'
8
-
9
- CONCURRENCY = (ENV['CONCURRENCY'] || 2).to_i
10
- SAMPLES = (ENV['SAMPLES'] || 10).to_i
11
-
12
- Fleck.configure do |config|
13
- config.default_user = user
14
- config.default_pass = pass
15
- config.loglevel = Logger::DEBUG
16
- end
17
-
18
- connection = Fleck.connection(host: "127.0.0.1", port: 5672, user: user, pass: pass, vhost: "/")
19
- client = Fleck::Client.new(connection, "consumer.initialization.example.queue", concurrency: CONCURRENCY.to_i)
20
-
21
- class MyConsumer < Fleck::Consumer
22
- configure queue: 'consumer.initialization.example.queue', concurrency: CONCURRENCY.to_i
23
- actions :hello
24
-
25
- initialize do
26
- @value = "MY CONSUMER :) #{self.object_id}"
27
- end
28
-
29
- def hello
30
- response.body = "#{@value} Hello!"
31
- end
32
- end
33
-
34
- SAMPLES.to_i.times do |num|
35
- client.request(action: 'hello', params: {num: num}, timeout: 5) do |request, response|
36
- if response.status == 200
37
- Fleck.logger.info response.body
38
- else
39
- Fleck.logger.error response.errors.join(", ")
40
- end
41
- end
42
- end
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'fleck'
5
+
6
+ user = ENV['USER'] || 'guest'
7
+ pass = ENV['PASS'] || 'guest'
8
+
9
+ CONCURRENCY = (ENV['CONCURRENCY'] || 2).to_i
10
+ SAMPLES = (ENV['SAMPLES'] || 10).to_i
11
+ QUEUE = 'consumer.initialization.example.queue'
12
+
13
+ Fleck.configure do |config|
14
+ config.default_user = user
15
+ config.default_pass = pass
16
+ config.loglevel = Logger::DEBUG
17
+ end
18
+
19
+ connection = Fleck.connection(host: '127.0.0.1', port: 5672, user: user, pass: pass, vhost: '/')
20
+ client = Fleck::Client.new(connection, QUEUE, concurrency: CONCURRENCY.to_i)
21
+
22
+ class MyConsumer < Fleck::Consumer
23
+ configure queue: QUEUE, concurrency: CONCURRENCY.to_i
24
+ actions :hello
25
+
26
+ initialize do
27
+ @value = "MY CONSUMER :) #{object_id}"
28
+ end
29
+
30
+ def hello
31
+ logger.info '---------------- HELLO ----------------'
32
+ ok! "#{@value} Hello!"
33
+ end
34
+ end
35
+
36
+ SAMPLES.to_i.times do |num|
37
+ client.request(action: 'hello', params: {num: num}, timeout: 5) do |_, response|
38
+ if response.status == 200
39
+ Fleck.logger.info response.body
40
+ else
41
+ Fleck.logger.error response.errors.join(', ')
42
+ end
43
+ end
44
+ end
@@ -1,57 +1,50 @@
1
- #!/usr/bin/env ruby
2
- # encoding: utf-8
3
-
4
- require 'fleck'
5
-
6
- user = ENV['USER'] || 'guest'
7
- pass = ENV['PASS'] || 'guest'
8
-
9
- CONCURRENCY = (ENV['CONCURRENCY'] || 2).to_i
10
- SAMPLES = (ENV['SAMPLES'] || 10).to_i
11
-
12
- Fleck.configure do |config|
13
- config.default_user = user
14
- config.default_pass = pass
15
- config.loglevel = Logger::DEBUG
16
- end
17
-
18
- connection = Fleck.connection(host: "127.0.0.1", port: 5672, user: user, pass: pass, vhost: "/")
19
- client = Fleck::Client.new(connection, "deprecation.example.queue", concurrency: CONCURRENCY.to_i)
20
-
21
- class MyConsumer < Fleck::Consumer
22
- configure queue: 'deprecation.example.queue', concurrency: CONCURRENCY.to_i
23
-
24
- def on_message(request, response)
25
- case request.action
26
- when 'hello' then hello
27
- else
28
- response.not_found!
29
- end
30
- end
31
-
32
- def hello
33
- version = request.version || 'v1'
34
- case version.to_s
35
- when 'v1' then hello_v1
36
- when 'v2' then hello_v2
37
- else
38
- response.not_found!
39
- end
40
- end
41
-
42
- private
43
-
44
- def hello_v1
45
- deprecated!
46
- response.body = "#{request.params[:num]}. Hello V1!"
47
- end
48
-
49
- def hello_v2
50
- response.body = "#{request.params[:num] + 1}. Hello V2!"
51
- end
52
- end
53
-
54
- SAMPLES.to_i.times do |num|
55
- response = client.request(action: 'hello', version: (rand >= 0.5 ? 'v2' : 'v1'), params: {num: num}, timeout: 5)
56
- puts (response.deprecated? ? "DEPRECATED: #{response.body}" : response.body)
57
- end
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'fleck'
5
+
6
+ user = ENV['USER'] || 'guest'
7
+ pass = ENV['PASS'] || 'guest'
8
+
9
+ CONCURRENCY = (ENV['CONCURRENCY'] || 2).to_i
10
+ SAMPLES = (ENV['SAMPLES'] || 10).to_i
11
+
12
+ Fleck.configure do |config|
13
+ config.default_user = user
14
+ config.default_pass = pass
15
+ config.loglevel = Logger::DEBUG
16
+ end
17
+
18
+ connection = Fleck.connection(host: "127.0.0.1", port: 5672, user: user, pass: pass, vhost: "/")
19
+ client = Fleck::Client.new(connection, "deprecation.example.queue", concurrency: CONCURRENCY.to_i)
20
+
21
+ class MyConsumer < Fleck::Consumer
22
+ configure queue: 'deprecation.example.queue', concurrency: CONCURRENCY.to_i
23
+
24
+ action :hello
25
+ def hello
26
+ version = request.version || 'v1'
27
+ case version.to_s
28
+ when 'v1' then hello_v1
29
+ when 'v2' then hello_v2
30
+ else
31
+ not_found!
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def hello_v1
38
+ deprecated!
39
+ ok! "#{request.params[:num]}. Hello V1!"
40
+ end
41
+
42
+ def hello_v2
43
+ ok! "#{request.params[:num] + 1}. Hello V2!"
44
+ end
45
+ end
46
+
47
+ SAMPLES.to_i.times do |num|
48
+ response = client.request(action: 'hello', version: (rand >= 0.5 ? 'v2' : 'v1'), params: {num: num}, timeout: 5)
49
+ puts (response.deprecated? ? "DEPRECATED: #{response.body}" : response.body)
50
+ end