fleck 2.2.1 → 2.2.2
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/.gitignore +11 -11
- data/Gemfile +6 -6
- data/examples/actions.rb +60 -60
- data/examples/blocking_consumer.rb +42 -42
- data/examples/consumer_initialization.rb +44 -44
- data/examples/deprecation.rb +50 -50
- data/examples/example.rb +76 -76
- data/examples/expired.rb +72 -72
- data/examples/fanout.rb +62 -62
- data/lib/fleck/client.rb +124 -124
- data/lib/fleck/configuration.rb +147 -147
- data/lib/fleck/core/consumer/configuration.rb +69 -69
- data/lib/fleck/core/consumer/helpers_definers.rb +56 -56
- data/lib/fleck/core/consumer/logger.rb +88 -88
- data/lib/fleck/core/consumer/request.rb +100 -100
- data/lib/fleck/core/consumer/response.rb +77 -77
- data/lib/fleck/core/consumer/response_helpers.rb +81 -81
- data/lib/fleck/core/consumer.rb +1 -1
- data/lib/fleck/loggable.rb +15 -15
- data/lib/fleck/utilities/host_rating.rb +102 -102
- data/lib/fleck/version.rb +1 -1
- data/lib/fleck.rb +82 -82
- metadata +2 -2
data/lib/fleck/configuration.rb
CHANGED
@@ -1,147 +1,147 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Open `Fleck` module to add `Configuration` class implementation.
|
4
|
-
module Fleck
|
5
|
-
# `Fleck::Configuration` implements a set of methods useful for `Fleck` clients and consumers configuration.
|
6
|
-
class Configuration
|
7
|
-
attr_reader :logfile, :loglevel, :progname, :hosts
|
8
|
-
attr_accessor :default_user, :default_pass, :default_host, :default_port, :default_vhost, :default_queue,
|
9
|
-
:app_name, :filters
|
10
|
-
|
11
|
-
def initialize
|
12
|
-
@logfile = $stdout
|
13
|
-
@loglevel = ::Logger::INFO
|
14
|
-
@progname = 'Fleck'
|
15
|
-
@app_name = $PROGRAM_NAME
|
16
|
-
@default_host = '127.0.0.1'
|
17
|
-
@default_port = 5672
|
18
|
-
@default_user = nil
|
19
|
-
@default_pass = nil
|
20
|
-
@default_vhost = '/'
|
21
|
-
@default_queue = 'default'
|
22
|
-
@filters = %w[password secret token]
|
23
|
-
@hosts = []
|
24
|
-
@credentials = {}
|
25
|
-
end
|
26
|
-
|
27
|
-
def hosts=(*args)
|
28
|
-
args.flatten.each do |host|
|
29
|
-
add_host host
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def add_host(data)
|
34
|
-
case data
|
35
|
-
when String then add_host_from_string(data)
|
36
|
-
when Hash then add_host_from_hash(data)
|
37
|
-
else
|
38
|
-
raise ArgumentError, "Invalid host type #{data.inspect}: String or Hash expected"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def default_options
|
43
|
-
best = @hosts.min
|
44
|
-
opts = {}
|
45
|
-
|
46
|
-
host = best ? best.host : @default_host
|
47
|
-
port = best ? best.port : @default_port
|
48
|
-
credentials = @credentials["#{host}:#{port}"] || { user: @default_user, pass: @default_pass }
|
49
|
-
|
50
|
-
opts[:host] = host
|
51
|
-
opts[:port] = port
|
52
|
-
opts[:user] = credentials[:user] || @default_user
|
53
|
-
opts[:pass] = credentials[:pass] || @default_pass
|
54
|
-
opts[:vhost] = @default_vhost
|
55
|
-
opts[:queue] = @default_queue
|
56
|
-
|
57
|
-
opts
|
58
|
-
end
|
59
|
-
|
60
|
-
def logfile=(new_logfile)
|
61
|
-
return unless @logfile != new_logfile
|
62
|
-
|
63
|
-
@logfile = new_logfile
|
64
|
-
reset_logger
|
65
|
-
end
|
66
|
-
|
67
|
-
def loglevel=(new_loglevel)
|
68
|
-
@loglevel = new_loglevel
|
69
|
-
@logger.level = @loglevel if @logger
|
70
|
-
end
|
71
|
-
|
72
|
-
def progname=(new_progname)
|
73
|
-
@progname = new_progname
|
74
|
-
@logger.progname = @progname if @logger
|
75
|
-
end
|
76
|
-
|
77
|
-
def logger
|
78
|
-
@logger || reset_logger
|
79
|
-
end
|
80
|
-
|
81
|
-
def logger=(new_logger)
|
82
|
-
@logger&.close
|
83
|
-
|
84
|
-
if new_logger.nil?
|
85
|
-
@logger = ::Logger.new(nil)
|
86
|
-
else
|
87
|
-
@logger = new_logger.clone
|
88
|
-
@logger.formatter = formatter
|
89
|
-
@logger.progname = @progname
|
90
|
-
@logger.level = @loglevel
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
def reset_logger
|
95
|
-
new_logger = ::Logger.new(@logfile)
|
96
|
-
new_logger.formatter = formatter
|
97
|
-
new_logger.progname = @progname
|
98
|
-
new_logger.level = @loglevel
|
99
|
-
@logger&.close
|
100
|
-
@logger = new_logger
|
101
|
-
|
102
|
-
@logger
|
103
|
-
end
|
104
|
-
|
105
|
-
def formatter
|
106
|
-
return @formatter if @formatter
|
107
|
-
|
108
|
-
@formatter = proc do |severity, datetime, progname, msg|
|
109
|
-
color = severity_color(severity)
|
110
|
-
"[#{datetime.strftime('%F %T.%L')}]".color(:cyan) +
|
111
|
-
"(#{$PID})".color(:blue) +
|
112
|
-
"|#{severity}|".color(color) +
|
113
|
-
(progname ? "<#{progname}>".color(:yellow) : '') +
|
114
|
-
" #{msg}\n"
|
115
|
-
end
|
116
|
-
|
117
|
-
@formatter
|
118
|
-
end
|
119
|
-
|
120
|
-
private
|
121
|
-
|
122
|
-
def add_host_from_string(data)
|
123
|
-
host, port = data.split(':')
|
124
|
-
port = port ? port.to_i : 5672
|
125
|
-
@hosts << Fleck::HostRating.new(host: host, port: port)
|
126
|
-
@credentials["#{host}:#{port}"] ||= { user: @default_user, pass: @default_pass }
|
127
|
-
end
|
128
|
-
|
129
|
-
def add_host_from_hash(data)
|
130
|
-
data = data.to_hash_with_indifferent_access
|
131
|
-
host = data[:host] || @default_host
|
132
|
-
port = data[:port] || @default_port
|
133
|
-
@hosts << Fleck::HostRating.new(host: host, port: port)
|
134
|
-
@credentials["#{host}:#{port}"] ||= { user: data[:user] || @default_user, pass: data[:pass] || @default_pass }
|
135
|
-
end
|
136
|
-
|
137
|
-
def severity_color(severity)
|
138
|
-
case severity
|
139
|
-
when 'DEBUG' then '#512DA8'
|
140
|
-
when 'INFO' then '#33691E'
|
141
|
-
when 'WARN' then '#E65100'
|
142
|
-
when 'ERROR', 'FATAL' then '#B71C1C'
|
143
|
-
else '#00BCD4'
|
144
|
-
end
|
145
|
-
end
|
146
|
-
end
|
147
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Open `Fleck` module to add `Configuration` class implementation.
|
4
|
+
module Fleck
|
5
|
+
# `Fleck::Configuration` implements a set of methods useful for `Fleck` clients and consumers configuration.
|
6
|
+
class Configuration
|
7
|
+
attr_reader :logfile, :loglevel, :progname, :hosts
|
8
|
+
attr_accessor :default_user, :default_pass, :default_host, :default_port, :default_vhost, :default_queue,
|
9
|
+
:app_name, :filters
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@logfile = $stdout
|
13
|
+
@loglevel = ::Logger::INFO
|
14
|
+
@progname = 'Fleck'
|
15
|
+
@app_name = $PROGRAM_NAME
|
16
|
+
@default_host = '127.0.0.1'
|
17
|
+
@default_port = 5672
|
18
|
+
@default_user = nil
|
19
|
+
@default_pass = nil
|
20
|
+
@default_vhost = '/'
|
21
|
+
@default_queue = 'default'
|
22
|
+
@filters = %w[password secret token]
|
23
|
+
@hosts = []
|
24
|
+
@credentials = {}
|
25
|
+
end
|
26
|
+
|
27
|
+
def hosts=(*args)
|
28
|
+
args.flatten.each do |host|
|
29
|
+
add_host host
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_host(data)
|
34
|
+
case data
|
35
|
+
when String then add_host_from_string(data)
|
36
|
+
when Hash then add_host_from_hash(data)
|
37
|
+
else
|
38
|
+
raise ArgumentError, "Invalid host type #{data.inspect}: String or Hash expected"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def default_options
|
43
|
+
best = @hosts.min
|
44
|
+
opts = {}
|
45
|
+
|
46
|
+
host = best ? best.host : @default_host
|
47
|
+
port = best ? best.port : @default_port
|
48
|
+
credentials = @credentials["#{host}:#{port}"] || { user: @default_user, pass: @default_pass }
|
49
|
+
|
50
|
+
opts[:host] = host
|
51
|
+
opts[:port] = port
|
52
|
+
opts[:user] = credentials[:user] || @default_user
|
53
|
+
opts[:pass] = credentials[:pass] || @default_pass
|
54
|
+
opts[:vhost] = @default_vhost
|
55
|
+
opts[:queue] = @default_queue
|
56
|
+
|
57
|
+
opts
|
58
|
+
end
|
59
|
+
|
60
|
+
def logfile=(new_logfile)
|
61
|
+
return unless @logfile != new_logfile
|
62
|
+
|
63
|
+
@logfile = new_logfile
|
64
|
+
reset_logger
|
65
|
+
end
|
66
|
+
|
67
|
+
def loglevel=(new_loglevel)
|
68
|
+
@loglevel = new_loglevel
|
69
|
+
@logger.level = @loglevel if @logger
|
70
|
+
end
|
71
|
+
|
72
|
+
def progname=(new_progname)
|
73
|
+
@progname = new_progname
|
74
|
+
@logger.progname = @progname if @logger
|
75
|
+
end
|
76
|
+
|
77
|
+
def logger
|
78
|
+
@logger || reset_logger
|
79
|
+
end
|
80
|
+
|
81
|
+
def logger=(new_logger)
|
82
|
+
@logger&.close
|
83
|
+
|
84
|
+
if new_logger.nil?
|
85
|
+
@logger = ::Logger.new(nil)
|
86
|
+
else
|
87
|
+
@logger = new_logger.clone
|
88
|
+
@logger.formatter = formatter
|
89
|
+
@logger.progname = @progname
|
90
|
+
@logger.level = @loglevel
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def reset_logger
|
95
|
+
new_logger = ::Logger.new(@logfile)
|
96
|
+
new_logger.formatter = formatter
|
97
|
+
new_logger.progname = @progname
|
98
|
+
new_logger.level = @loglevel
|
99
|
+
@logger&.close
|
100
|
+
@logger = new_logger
|
101
|
+
|
102
|
+
@logger
|
103
|
+
end
|
104
|
+
|
105
|
+
def formatter
|
106
|
+
return @formatter if @formatter
|
107
|
+
|
108
|
+
@formatter = proc do |severity, datetime, progname, msg|
|
109
|
+
color = severity_color(severity)
|
110
|
+
"[#{datetime.strftime('%F %T.%L')}]".color(:cyan) +
|
111
|
+
"(#{$PID})".color(:blue) +
|
112
|
+
"|#{severity}|".color(color) +
|
113
|
+
(progname ? "<#{progname}>".color(:yellow) : '') +
|
114
|
+
" #{msg}\n"
|
115
|
+
end
|
116
|
+
|
117
|
+
@formatter
|
118
|
+
end
|
119
|
+
|
120
|
+
private
|
121
|
+
|
122
|
+
def add_host_from_string(data)
|
123
|
+
host, port = data.split(':')
|
124
|
+
port = port ? port.to_i : 5672
|
125
|
+
@hosts << Fleck::HostRating.new(host: host, port: port)
|
126
|
+
@credentials["#{host}:#{port}"] ||= { user: @default_user, pass: @default_pass }
|
127
|
+
end
|
128
|
+
|
129
|
+
def add_host_from_hash(data)
|
130
|
+
data = data.to_hash_with_indifferent_access
|
131
|
+
host = data[:host] || @default_host
|
132
|
+
port = data[:port] || @default_port
|
133
|
+
@hosts << Fleck::HostRating.new(host: host, port: port)
|
134
|
+
@credentials["#{host}:#{port}"] ||= { user: data[:user] || @default_user, pass: data[:pass] || @default_pass }
|
135
|
+
end
|
136
|
+
|
137
|
+
def severity_color(severity)
|
138
|
+
case severity
|
139
|
+
when 'DEBUG' then '#512DA8'
|
140
|
+
when 'INFO' then '#33691E'
|
141
|
+
when 'WARN' then '#E65100'
|
142
|
+
when 'ERROR', 'FATAL' then '#B71C1C'
|
143
|
+
else '#00BCD4'
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
@@ -1,69 +1,69 @@
|
|
1
|
-
module Fleck
|
2
|
-
module Core
|
3
|
-
class Consumer
|
4
|
-
module Configuration
|
5
|
-
def self.included(base)
|
6
|
-
base.extend ClassMethods
|
7
|
-
base.send :include, InstanceMethods
|
8
|
-
end
|
9
|
-
|
10
|
-
# Defines class methods to import when `Configuration` module is imported.
|
11
|
-
module ClassMethods
|
12
|
-
attr_accessor :configs
|
13
|
-
|
14
|
-
def configure(opts = {})
|
15
|
-
configs.merge!(opts)
|
16
|
-
logger.debug 'Consumer configurations updated.'
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
# Defines instance methods to import when `Configuration` module is imported.
|
21
|
-
module InstanceMethods
|
22
|
-
def configs
|
23
|
-
@configs ||= self.class.configs
|
24
|
-
end
|
25
|
-
|
26
|
-
def rmq_host
|
27
|
-
@rmq_host ||= configs[:host]
|
28
|
-
end
|
29
|
-
|
30
|
-
def rmq_port
|
31
|
-
@rmq_port ||= configs[:port]
|
32
|
-
end
|
33
|
-
|
34
|
-
def rmq_user
|
35
|
-
@rmq_user ||= configs.fetch(:user, 'guest')
|
36
|
-
end
|
37
|
-
|
38
|
-
def rmq_pass
|
39
|
-
@rmq_pass ||= configs.fetch(:password, configs[:pass])
|
40
|
-
end
|
41
|
-
|
42
|
-
def rmq_vhost
|
43
|
-
@rmq_vhost ||= configs.fetch(:vhost, '/')
|
44
|
-
end
|
45
|
-
|
46
|
-
def queue_name
|
47
|
-
@queue_name ||= configs[:queue]
|
48
|
-
end
|
49
|
-
|
50
|
-
def rmq_exchange_type
|
51
|
-
@rmq_exchange_type ||= configs.fetch(:exchange_type, :direct)
|
52
|
-
end
|
53
|
-
|
54
|
-
def rmq_exchange_name
|
55
|
-
@rmq_exchange_name ||= configs.fetch(:exchange_name, '')
|
56
|
-
end
|
57
|
-
|
58
|
-
def ack_mandatory?
|
59
|
-
@ack_mandatory ||= !configs[:mandatory].nil?
|
60
|
-
end
|
61
|
-
|
62
|
-
def prefetch_size
|
63
|
-
@prefetch_size ||= configs.fetch(:prefetch, 100).to_i
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
1
|
+
module Fleck
|
2
|
+
module Core
|
3
|
+
class Consumer
|
4
|
+
module Configuration
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
base.send :include, InstanceMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
# Defines class methods to import when `Configuration` module is imported.
|
11
|
+
module ClassMethods
|
12
|
+
attr_accessor :configs
|
13
|
+
|
14
|
+
def configure(opts = {})
|
15
|
+
configs.merge!(opts)
|
16
|
+
logger.debug 'Consumer configurations updated.'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Defines instance methods to import when `Configuration` module is imported.
|
21
|
+
module InstanceMethods
|
22
|
+
def configs
|
23
|
+
@configs ||= self.class.configs
|
24
|
+
end
|
25
|
+
|
26
|
+
def rmq_host
|
27
|
+
@rmq_host ||= configs[:host]
|
28
|
+
end
|
29
|
+
|
30
|
+
def rmq_port
|
31
|
+
@rmq_port ||= configs[:port]
|
32
|
+
end
|
33
|
+
|
34
|
+
def rmq_user
|
35
|
+
@rmq_user ||= configs.fetch(:user, 'guest')
|
36
|
+
end
|
37
|
+
|
38
|
+
def rmq_pass
|
39
|
+
@rmq_pass ||= configs.fetch(:password, configs[:pass])
|
40
|
+
end
|
41
|
+
|
42
|
+
def rmq_vhost
|
43
|
+
@rmq_vhost ||= configs.fetch(:vhost, '/')
|
44
|
+
end
|
45
|
+
|
46
|
+
def queue_name
|
47
|
+
@queue_name ||= configs[:queue]
|
48
|
+
end
|
49
|
+
|
50
|
+
def rmq_exchange_type
|
51
|
+
@rmq_exchange_type ||= configs.fetch(:exchange_type, :direct)
|
52
|
+
end
|
53
|
+
|
54
|
+
def rmq_exchange_name
|
55
|
+
@rmq_exchange_name ||= configs.fetch(:exchange_name, '')
|
56
|
+
end
|
57
|
+
|
58
|
+
def ack_mandatory?
|
59
|
+
@ack_mandatory ||= !configs[:mandatory].nil?
|
60
|
+
end
|
61
|
+
|
62
|
+
def prefetch_size
|
63
|
+
@prefetch_size ||= configs.fetch(:prefetch, 100).to_i
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -1,56 +1,56 @@
|
|
1
|
-
module Fleck
|
2
|
-
module Core
|
3
|
-
class Consumer
|
4
|
-
module HelpersDefiners
|
5
|
-
INTERRUPT_NAME = :terminate_execution
|
6
|
-
|
7
|
-
def self.included(base)
|
8
|
-
base.extend ClassMethods
|
9
|
-
base.send :include, InstanceMethods
|
10
|
-
end
|
11
|
-
|
12
|
-
# Defines class methods to import when `HelpersDefilers` module is imported.
|
13
|
-
module ClassMethods
|
14
|
-
def error_method(name, code, message)
|
15
|
-
define_method(name) do |error: nil, body: nil, interrupt: true|
|
16
|
-
response.render_error(code, [message] + [error].flatten)
|
17
|
-
response.body = body
|
18
|
-
throw INTERRUPT_NAME if interrupt
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def redirect_method(name, code)
|
23
|
-
success_method(name, code)
|
24
|
-
end
|
25
|
-
|
26
|
-
def success_method(name, code)
|
27
|
-
define_method(name) do |*args|
|
28
|
-
interrupt = (args[1] ? args[1][:interrupt] : true)
|
29
|
-
response.status = code
|
30
|
-
response.body = args[0]
|
31
|
-
throw INTERRUPT_NAME if interrupt
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def information_method(name, code)
|
36
|
-
success_method(name, code)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
# Defines instance methods to import when `HelpersDefilers` module is imported.
|
41
|
-
module InstanceMethods
|
42
|
-
def halt(code, body = nil, errors = nil)
|
43
|
-
response.body = body
|
44
|
-
if code >= 400
|
45
|
-
response.render_error(code, [errors].flatten)
|
46
|
-
else
|
47
|
-
response.status = code
|
48
|
-
end
|
49
|
-
|
50
|
-
throw INTERRUPT_NAME
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
1
|
+
module Fleck
|
2
|
+
module Core
|
3
|
+
class Consumer
|
4
|
+
module HelpersDefiners
|
5
|
+
INTERRUPT_NAME = :terminate_execution
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.extend ClassMethods
|
9
|
+
base.send :include, InstanceMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
# Defines class methods to import when `HelpersDefilers` module is imported.
|
13
|
+
module ClassMethods
|
14
|
+
def error_method(name, code, message)
|
15
|
+
define_method(name) do |error: nil, body: nil, interrupt: true|
|
16
|
+
response.render_error(code, [message] + [error].flatten)
|
17
|
+
response.body = body
|
18
|
+
throw INTERRUPT_NAME if interrupt
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def redirect_method(name, code)
|
23
|
+
success_method(name, code)
|
24
|
+
end
|
25
|
+
|
26
|
+
def success_method(name, code)
|
27
|
+
define_method(name) do |*args|
|
28
|
+
interrupt = (args[1] ? args[1][:interrupt] : true)
|
29
|
+
response.status = code
|
30
|
+
response.body = args[0]
|
31
|
+
throw INTERRUPT_NAME if interrupt
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def information_method(name, code)
|
36
|
+
success_method(name, code)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Defines instance methods to import when `HelpersDefilers` module is imported.
|
41
|
+
module InstanceMethods
|
42
|
+
def halt(code, body = nil, errors = nil)
|
43
|
+
response.body = body
|
44
|
+
if code >= 400
|
45
|
+
response.render_error(code, [errors].flatten)
|
46
|
+
else
|
47
|
+
response.status = code
|
48
|
+
end
|
49
|
+
|
50
|
+
throw INTERRUPT_NAME
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|