fleck 2.0.0 → 2.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/actions.rb +7 -6
- data/lib/fleck/configuration.rb +0 -2
- data/lib/fleck/core/consumer/actions.rb +3 -0
- data/lib/fleck/core/consumer/helpers_definers.rb +3 -2
- data/lib/fleck/core/consumer/logger.rb +1 -1
- data/lib/fleck/core/consumer/request.rb +14 -3
- data/lib/fleck/utilities/host_rating.rb +25 -27
- data/lib/fleck/version.rb +1 -1
- data/lib/fleck.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ba4104d51ed9b8954789594108e46d754a5ebedbb281ebf03b11e4c7af762fd
|
4
|
+
data.tar.gz: 7a7fe6fdac9a8fc0ef6effd8ab321dbd0940b7f580a39dc2cc4132dd5f81d4c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b98ae1a23554c782a5d99001edf83a054ccbba794d57a6778a6322dff7e8bf21d09a5d45f797f9d0e3cb247b4cb5b495959e39976d380edcd6a61bd6a288055a
|
7
|
+
data.tar.gz: 603c304bb7bec0bb967fb83afe6bf0f0a27d9ff71a1ea1e3e8d80539bd7ad2ff9c2fb681418461bbde12a7d2df50bb9aa0968bc82b1e59a8e847ba7bb2b05ba4
|
data/examples/actions.rb
CHANGED
@@ -24,12 +24,13 @@ class MyConsumer < Fleck::Consumer
|
|
24
24
|
|
25
25
|
action 'hello', "An action which returns 'Hello'"
|
26
26
|
def hello
|
27
|
-
ok! 'Hello
|
27
|
+
ok! result: 'Hello', message: 'Ciao'
|
28
28
|
end
|
29
29
|
|
30
30
|
action 'ciao', "An action which returns 'Ciao'"
|
31
|
+
param :world, type: 'boolean', required: true, default: false
|
31
32
|
def my_custom_method
|
32
|
-
ok! 'Ciao!'
|
33
|
+
ok! params[:world] ? 'Ciao, Mondo!' : 'Ciao!'
|
33
34
|
end
|
34
35
|
|
35
36
|
action :aloha
|
@@ -47,13 +48,13 @@ end
|
|
47
48
|
actions = %i[hello ciao aloha not_an_action]
|
48
49
|
|
49
50
|
SAMPLES.to_i.times do |num|
|
50
|
-
action = actions
|
51
|
+
action = actions.sample
|
51
52
|
name = ['John Doe', 'Willie Wonka', 'Billie Smith'].sample
|
52
|
-
client.request(action: action, params: { num: num, name: name, number: rand * 100 }, timeout: 5) do |_, response|
|
53
|
+
client.request(action: action, params: { num: num, name: name, number: rand * 100, world: %w[yes no].sample }, timeout: 5) do |_, response|
|
53
54
|
if response.status == 200
|
54
|
-
Fleck.logger.info "ACTION: (#{action.inspect}) #{response.body}"
|
55
|
+
Fleck.logger.info "ACTION: (#{action.inspect}) #{response.body.inspect}"
|
55
56
|
else
|
56
|
-
Fleck.logger.error "ACTION: (#{action.inspect}) #{response.errors.join(', ')} --- #{response.body}"
|
57
|
+
Fleck.logger.error "ACTION: (#{action.inspect}) #{response.errors.join(', ')} --- #{response.body.inspect}"
|
57
58
|
end
|
58
59
|
end
|
59
60
|
end
|
data/lib/fleck/configuration.rb
CHANGED
@@ -4,8 +4,6 @@
|
|
4
4
|
module Fleck
|
5
5
|
# `Fleck::Configuration` implements a set of methods useful for `Fleck` clients and consumers configuration.
|
6
6
|
class Configuration
|
7
|
-
autoload :HostRating, 'fleck/utilities/host_rating.rb'
|
8
|
-
|
9
7
|
attr_reader :logfile, :loglevel, :progname, :hosts
|
10
8
|
attr_accessor :default_user, :default_pass, :default_host, :default_port, :default_vhost, :default_queue,
|
11
9
|
:app_name, :filters
|
@@ -49,6 +49,7 @@ module Fleck
|
|
49
49
|
action_name = request.action.to_s
|
50
50
|
action = actions[action_name]
|
51
51
|
unless action
|
52
|
+
request.log_headers_and_params!
|
52
53
|
message = "Action #{action_name.inspect} not found!"
|
53
54
|
not_found! error: message, body: [
|
54
55
|
{ type: 'action', name: action_name, value: action_name, error: 'not_found', message: message }
|
@@ -58,12 +59,14 @@ module Fleck
|
|
58
59
|
# iterate over action params and use param options to validate incoming request params.
|
59
60
|
action[:params].each { |_, param| validate_action_param!(param) }
|
60
61
|
|
62
|
+
request.log_headers_and_params!
|
61
63
|
send(action[:method_name])
|
62
64
|
end
|
63
65
|
|
64
66
|
def validate_action_param!(param)
|
65
67
|
validation = param.validate(request.params[param.name])
|
66
68
|
unless validation.valid?
|
69
|
+
request.log_headers_and_params!
|
67
70
|
bad_request! error: "Invalid param value: #{param.name} = #{validation.value.inspect}",
|
68
71
|
body: validation.errors
|
69
72
|
end
|
@@ -24,9 +24,10 @@ module Fleck
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def success_method(name, code)
|
27
|
-
define_method(name) do |
|
27
|
+
define_method(name) do |*args|
|
28
|
+
interrupt = (args[1] ? args[1][:interrupt] : true)
|
28
29
|
response.status = code
|
29
|
-
response.body =
|
30
|
+
response.body = args[0]
|
30
31
|
throw INTERRUPT_NAME if interrupt
|
31
32
|
end
|
32
33
|
end
|
@@ -67,7 +67,7 @@ module Fleck
|
|
67
67
|
|
68
68
|
def exchange_and_queue_name
|
69
69
|
ex_name = rmq_exchange_name.to_s == '' ? ''.inspect : rmq_exchange_name
|
70
|
-
"(#{ex_name
|
70
|
+
"(#{ex_name}|#{exchange_type_code}|#{queue_name}) "
|
71
71
|
end
|
72
72
|
|
73
73
|
def request_metadata
|
@@ -19,8 +19,8 @@ module Fleck
|
|
19
19
|
@app_id = metadata[:app_id]
|
20
20
|
@reply_to = @metadata.reply_to
|
21
21
|
@payload = payload
|
22
|
-
@exchange = delivery_info.exchange
|
23
|
-
@queue = delivery_info.routing_key
|
22
|
+
@exchange = delivery_info.exchange
|
23
|
+
@queue = delivery_info.routing_key
|
24
24
|
@delivery_tag = delivery_info.delivery_tag
|
25
25
|
@data = {}
|
26
26
|
@headers = (@metadata.headers || {}).to_hash_with_indifferent_access
|
@@ -61,13 +61,24 @@ module Fleck
|
|
61
61
|
@requeue
|
62
62
|
end
|
63
63
|
|
64
|
+
def log_headers_and_params!
|
65
|
+
queue_name = "(#{@exchange == '' ? @queue : "#{@queue}@#{@exchange}"})".color(:red)
|
66
|
+
endpoint = "/#{action} :#{@version || 'v1'}".color(:red)
|
67
|
+
message = "\n" \
|
68
|
+
"#{ip} - #{queue_name} #{endpoint} [#{@id}]\n" \
|
69
|
+
" ~ headers ~ #{headers.inspect.color(:green)}\n" \
|
70
|
+
" @params #{params.inspect.color(:green)}"
|
71
|
+
logger.debug message
|
72
|
+
end
|
73
|
+
|
64
74
|
protected
|
65
75
|
|
66
76
|
def parse_request!
|
67
77
|
@data = Oj.load(@payload, mode: :compat).to_hash_with_indifferent_access.filtered!
|
68
78
|
@headers.merge!(@data['headers'] || {}).filtered!
|
69
79
|
|
70
|
-
logger.debug "
|
80
|
+
logger.debug "Request (exchange: #{@exchange.inspect}, queue: #{@queue.inspect}, " \
|
81
|
+
"options: #{@headers}, message: #{@data})"
|
71
82
|
|
72
83
|
@action ||= @headers['action']
|
73
84
|
@headers['action'] ||= @action
|
@@ -64,41 +64,39 @@ module Fleck
|
|
64
64
|
ensure
|
65
65
|
@updated_at = Time.now
|
66
66
|
end
|
67
|
-
end
|
68
67
|
|
69
|
-
|
68
|
+
# Use a socket to test connection latency.
|
69
|
+
def measure_latency
|
70
|
+
socket = create_socket
|
70
71
|
|
71
|
-
|
72
|
-
|
73
|
-
|
72
|
+
started_at = Time.now.to_f
|
73
|
+
begin
|
74
|
+
socket.connect_nonblock(sock_addr)
|
75
|
+
rescue IO::WaitWritable
|
76
|
+
IO.select(nil, [socket], nil, CONN_TIMEOUT) or raise Timeout::Error
|
77
|
+
end
|
74
78
|
|
75
|
-
|
76
|
-
|
77
|
-
socket
|
78
|
-
rescue IO::WaitWritable
|
79
|
-
IO.select(nil, [socket], nil, CONN_TIMEOUT) or raise Timeout::Error
|
79
|
+
(Time.now.to_f - started_at) * 1000 # ms
|
80
|
+
ensure
|
81
|
+
socket&.close
|
80
82
|
end
|
81
83
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
# Create a new socket for connection test.
|
88
|
-
def create_socket
|
89
|
-
socket = Socket.new(:AF_INET, :SOCK_STREAM, 0)
|
90
|
-
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
84
|
+
# Create a new socket for connection test.
|
85
|
+
def create_socket
|
86
|
+
socket = Socket.new(:AF_INET, :SOCK_STREAM, 0)
|
87
|
+
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
91
88
|
|
92
|
-
|
93
|
-
|
89
|
+
socket
|
90
|
+
end
|
94
91
|
|
95
|
-
|
96
|
-
|
97
|
-
|
92
|
+
# Resolve domain name in order to obtain IP address to test.
|
93
|
+
def sock_addr
|
94
|
+
return @sock_addr if @sock_addr
|
98
95
|
|
99
|
-
|
100
|
-
|
96
|
+
addr = Socket.getaddrinfo(@host, nil)
|
97
|
+
@sock_addr = Socket.pack_sockaddr_in(@port, addr[0][3])
|
101
98
|
|
102
|
-
|
99
|
+
@sock_addr
|
100
|
+
end
|
103
101
|
end
|
104
102
|
end
|
data/lib/fleck/version.rb
CHANGED
data/lib/fleck.rb
CHANGED
@@ -23,6 +23,7 @@ require 'fleck/utilities/hash_with_indifferent_access'
|
|
23
23
|
module Fleck
|
24
24
|
autoload :VERSION, 'fleck/version.rb'
|
25
25
|
autoload :Loggable, 'fleck/loggable.rb'
|
26
|
+
autoload :HostRating, 'fleck/utilities/host_rating.rb'
|
26
27
|
autoload :Configuration, 'fleck/configuration.rb'
|
27
28
|
autoload :Core, 'fleck/core.rb'
|
28
29
|
autoload :Consumer, 'fleck/consumer.rb'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fleck
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Groza Sergiu
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-02-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|