pushyd 0.2.3 → 0.2.4
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/Gemfile.lock +8 -5
- data/bin/pushyd +5 -3
- data/lib/pushyd/conf.rb +3 -0
- data/lib/pushyd/constants.rb +0 -2
- data/lib/pushyd/proxy.orig.rb +198 -0
- data/lib/pushyd/proxy.rb +0 -7
- data/lib/pushyd/shouter.rb +3 -8
- data/pushyd.gemspec +2 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bef5122c9c7a8a4276bc0e1f735c48b65b88f156
|
4
|
+
data.tar.gz: 8620fcd2277aafc9ff97c6c65ec4abe25a6f5339
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dabd44f13d11e6167bb96b1e4c3d5c044bb0394ee386b3fe6ad84882339a518cf941d6f21aaab79090decd982511cbb1b1fe50ce5df124d70ee8d0487000a945
|
7
|
+
data.tar.gz: dc9779dfd1b20506840ccc799dbe5f8cac4d43daa30634ad4067a32a5a71b11b03dbdecdf4aa3a2075ba02195a6e40994da125cfb3208b8d234d4e2c6d765e38
|
data/Gemfile.lock
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
pushyd (0.2.
|
4
|
+
pushyd (0.2.4)
|
5
5
|
bunny
|
6
6
|
chamber
|
7
7
|
daemons
|
8
8
|
json
|
9
|
-
|
9
|
+
rest-client
|
10
10
|
terminal-table
|
11
11
|
|
12
12
|
GEM
|
@@ -37,14 +37,17 @@ GEM
|
|
37
37
|
http-form_data (1.0.1)
|
38
38
|
http_parser.rb (0.6.0)
|
39
39
|
json (1.8.3)
|
40
|
-
|
40
|
+
mime-types (2.99.2)
|
41
|
+
netrc (0.11.0)
|
41
42
|
parser (2.3.1.2)
|
42
43
|
ast (~> 2.2)
|
43
44
|
powerpack (0.1.1)
|
44
45
|
rainbow (2.1.0)
|
45
46
|
rake (11.1.2)
|
46
|
-
|
47
|
-
|
47
|
+
rest-client (1.8.0)
|
48
|
+
http-cookie (>= 1.0.2, < 2.0)
|
49
|
+
mime-types (>= 1.16, < 3.0)
|
50
|
+
netrc (~> 0.7)
|
48
51
|
rspec (3.4.0)
|
49
52
|
rspec-core (~> 3.4.0)
|
50
53
|
rspec-expectations (~> 3.4.0)
|
data/bin/pushyd
CHANGED
@@ -18,20 +18,22 @@ APP_ROOT = File.expand_path(File.dirname(__FILE__) + "/../")
|
|
18
18
|
begin
|
19
19
|
# Defaults
|
20
20
|
cmd_config = nil
|
21
|
+
cmd_logfile = nil
|
21
22
|
cmd_env = "production"
|
22
23
|
cmd_dump = false
|
23
24
|
|
24
25
|
# Parse options and check compliance
|
25
26
|
OptionParser.new do |opts|
|
26
27
|
opts.banner = "Usage: #{File.basename $PROGRAM_NAME} [options] start|stop"
|
27
|
-
opts.on("-
|
28
|
+
opts.on("-l", "--log LOGFILE") { |path| cmd_logfile = File.expand_path(path)}
|
29
|
+
opts.on("-c", "--config CONFIGFILE") { |path| cmd_config = File.expand_path(path)}
|
28
30
|
opts.on("-e", "--environment ENV") { |env| cmd_env = env }
|
29
31
|
opts.on("-d", "--dump") { cmd_dump = true }
|
30
32
|
opts.on("", "--dev") { cmd_env = "development" }
|
31
33
|
end.order!(ARGV)
|
32
34
|
|
33
35
|
# Build Chamber-based configuration from Gemspec with initial context
|
34
|
-
Conf.prepare root: APP_ROOT, gemspec: "pushyd", env: cmd_env, config: cmd_config
|
36
|
+
Conf.prepare root: APP_ROOT, gemspec: "pushyd", env: cmd_env, config: cmd_config, log: cmd_logfile
|
35
37
|
|
36
38
|
# Display final configuration
|
37
39
|
puts "--- #{Conf.name} #{Conf.version}"
|
@@ -61,7 +63,7 @@ run_options = {
|
|
61
63
|
}
|
62
64
|
Daemons.run_proc('pushy-daemon', run_options) do
|
63
65
|
# Load code
|
64
|
-
puts "--- loading code
|
66
|
+
puts "--- loading code"
|
65
67
|
require_relative "../lib/pushyd"
|
66
68
|
|
67
69
|
# Start daemon
|
data/lib/pushyd/conf.rb
CHANGED
data/lib/pushyd/constants.rb
CHANGED
@@ -0,0 +1,198 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'terminal-table'
|
3
|
+
require 'hashie'
|
4
|
+
require 'securerandom'
|
5
|
+
|
6
|
+
# Log output
|
7
|
+
def header rule, sign, topic, route
|
8
|
+
puts
|
9
|
+
puts SEPARATOR
|
10
|
+
puts sprintf "%s | %-20s %1s %-10s | %s",
|
11
|
+
DateTime.now.iso8601, rule, sign, topic, route
|
12
|
+
puts SEPARATOR
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
def extract ctype, payload, fields = []
|
17
|
+
# Force encoding (pftop...)
|
18
|
+
utf8payload = payload.force_encoding('UTF-8')
|
19
|
+
|
20
|
+
# Parse payload if content-type provided
|
21
|
+
case ctype
|
22
|
+
when "application/json"
|
23
|
+
# if fields = rule[:payload_extract]
|
24
|
+
# data = payload_extract(payload, fields)
|
25
|
+
# data_source = "extract #{fields.inspect} #{data.keys.count}k"
|
26
|
+
return JSON.parse utf8payload
|
27
|
+
|
28
|
+
when "text/plain"
|
29
|
+
return utf8payload.to_s
|
30
|
+
|
31
|
+
else
|
32
|
+
return utf8payload
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
# Handle body parse errors
|
37
|
+
rescue Encoding::UndefinedConversionError => e
|
38
|
+
puts "\t JSON PARSE ERROR: #{e.inspect}"
|
39
|
+
return {}
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
# def payload_extract payload, fields = []
|
45
|
+
# new_payload = payload.force_encoding('UTF-8')
|
46
|
+
# parsed = JSON.parse new_payload
|
47
|
+
|
48
|
+
# rescue Encoding::UndefinedConversionError => e
|
49
|
+
# puts "\t JSON PARSE ERROR: #{e.inspect}"
|
50
|
+
# return {}
|
51
|
+
|
52
|
+
# else
|
53
|
+
# return parsed
|
54
|
+
# end
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
# def handle_message rule_name, rule, delivery_info, metadata, payload
|
59
|
+
# # Prepare data
|
60
|
+
# msg_topic = delivery_info.exchange
|
61
|
+
# msg_rkey = delivery_info.routing_key.force_encoding('UTF-8')
|
62
|
+
# msg_headers = metadata.headers || {}
|
63
|
+
|
64
|
+
# # Extract fields
|
65
|
+
# data = extract metadata.content_type, payload, rule
|
66
|
+
|
67
|
+
# # Announce match
|
68
|
+
# header rule_name, "<", msg_topic, msg_rkey
|
69
|
+
|
70
|
+
# # Build notification payload
|
71
|
+
# body = {
|
72
|
+
# # received: msg_topic,
|
73
|
+
# exchange: msg_topic,
|
74
|
+
# route: msg_rkey,
|
75
|
+
# #headers: msg_headers,
|
76
|
+
# sent_at: msg_headers['sent_at'],
|
77
|
+
# sent_by: msg_headers['sent_by'],
|
78
|
+
# data: data,
|
79
|
+
# }
|
80
|
+
# pretty_body = JSON.pretty_generate(body)
|
81
|
+
|
82
|
+
# # Dump body data
|
83
|
+
# puts "RULE: #{rule.inspect}"
|
84
|
+
# puts "APP-ID: #{metadata.app_id}"
|
85
|
+
# puts "CONTENT-TYPE: #{metadata.content_type}"
|
86
|
+
# puts pretty_body
|
87
|
+
|
88
|
+
# # Propagate data if needed
|
89
|
+
# propagate rule[:relay], pretty_body
|
90
|
+
# end
|
91
|
+
|
92
|
+
def topic channel, name
|
93
|
+
@topics ||= {}
|
94
|
+
@topics[name] ||= channel.topic(name, durable: true, persistent: true)
|
95
|
+
end
|
96
|
+
|
97
|
+
def shout exchange, keys, body = {}
|
98
|
+
# Add timestamp
|
99
|
+
headers = {
|
100
|
+
sent_at: DateTime.now.iso8601,
|
101
|
+
sent_by: PROXY_IDENT
|
102
|
+
}
|
103
|
+
exchange_name = exchange.name
|
104
|
+
|
105
|
+
# Prepare key and data
|
106
|
+
routing_key = keys.unshift(exchange_name).join('.')
|
107
|
+
# payload = data
|
108
|
+
|
109
|
+
# Announce shout
|
110
|
+
header "SHOUT", ">", exchange_name, routing_key
|
111
|
+
puts JSON.pretty_generate(body) unless body.empty?
|
112
|
+
|
113
|
+
# Publish
|
114
|
+
exchange.publish(body.to_json,
|
115
|
+
routing_key: routing_key,
|
116
|
+
headers: headers,
|
117
|
+
app_id: "contributor",
|
118
|
+
content_type: "application/json",
|
119
|
+
)
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
# Init ASCII table
|
125
|
+
config_table = Terminal::Table.new
|
126
|
+
config_table.title = "Message propagation rules"
|
127
|
+
config_table.headings = ["queue binding", "topic", "route", "relay", "title"]
|
128
|
+
config_table.align_column(5, :right)
|
129
|
+
|
130
|
+
|
131
|
+
# Bind every topic
|
132
|
+
config[:rules].each do |rule_name, rule|
|
133
|
+
# Extract information
|
134
|
+
catch_subscribe = rule[:subscribe] || true
|
135
|
+
catch_topic = rule[:topic].to_s
|
136
|
+
catch_routes = rule[:routes].to_s.split(' ')
|
137
|
+
|
138
|
+
if catch_topic.empty? || catch_routes.empty?
|
139
|
+
abort "rule [#{rule_name}] is invalid: missing topic / routes"
|
140
|
+
end
|
141
|
+
|
142
|
+
# Build / attach to queue
|
143
|
+
rule_queue_name = "#{PROXY_IDENT}-#{QUEUE_HOST}-#{rule_name}"
|
144
|
+
|
145
|
+
begin
|
146
|
+
# Bind to this topic if not already done
|
147
|
+
listen_exchange = topic(channel, catch_topic)
|
148
|
+
|
149
|
+
# Pour this into a queue
|
150
|
+
queue = channel.queue(rule_queue_name, auto_delete: false, durable: true)
|
151
|
+
|
152
|
+
# Bind to these events on each route
|
153
|
+
catch_routes.each do |route|
|
154
|
+
# Bind with this routing key
|
155
|
+
queue.bind listen_exchange, routing_key: route
|
156
|
+
puts "BIND \t[#{rule_queue_name}] to [#{catch_topic}] / [#{route}] (subscribe: #{catch_subscribe})"
|
157
|
+
|
158
|
+
# Add row to table
|
159
|
+
config_table.add_row [rule_queue_name, catch_topic, route, rule[:relay].to_s, rule[:title].to_s ]
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
163
|
+
|
164
|
+
|
165
|
+
# Subscribe
|
166
|
+
|
167
|
+
queue.subscribe(block: false, manual_ack: SUB_USE_ACK, message_max: SUB_MSG_MAX) do |delivery_info, metadata, payload|
|
168
|
+
|
169
|
+
# Handle the message
|
170
|
+
handle_message rule_name, rule, delivery_info, metadata, payload
|
171
|
+
|
172
|
+
# Ack the msg
|
173
|
+
# puts "> #{delivery_info.delivery_tag}: nack"
|
174
|
+
# channel.nack(delivery_info.delivery_tag)
|
175
|
+
|
176
|
+
# if !SUB_USE_ACK
|
177
|
+
# puts "> #{delivery_info.delivery_tag}: no ack"
|
178
|
+
# elsif (100*rand) <= ACK_PERCENT
|
179
|
+
# channel.ack(delivery_info.delivery_tag)
|
180
|
+
# puts "> #{delivery_info.delivery_tag}: ACKED"
|
181
|
+
# else
|
182
|
+
# channel.nack(delivery_info.delivery_tag)
|
183
|
+
# puts "> #{delivery_info.delivery_tag}: NOT_ACKED"
|
184
|
+
# end
|
185
|
+
|
186
|
+
end
|
187
|
+
|
188
|
+
# End of loop
|
189
|
+
end
|
190
|
+
|
191
|
+
|
192
|
+
# Display config and susbcribe to queue
|
193
|
+
puts config_table
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
puts
|
198
|
+
puts "ENDED"
|
data/lib/pushyd/proxy.rb
CHANGED
@@ -4,9 +4,6 @@ require 'json'
|
|
4
4
|
require 'terminal-table'
|
5
5
|
|
6
6
|
module PushyDaemon
|
7
|
-
|
8
|
-
# class ProxyConnexionContext < StandardError; end
|
9
|
-
|
10
7
|
class Proxy < Endpoint
|
11
8
|
|
12
9
|
attr_accessor :table
|
@@ -118,10 +115,8 @@ module PushyDaemon
|
|
118
115
|
# data = payload_extract(payload, fields)
|
119
116
|
# data_source = "extract #{fields.inspect} #{data.keys.count}k"
|
120
117
|
return JSON.parse utf8payload
|
121
|
-
|
122
118
|
when "text/plain"
|
123
119
|
return utf8payload.to_s
|
124
|
-
|
125
120
|
else
|
126
121
|
return utf8payload
|
127
122
|
end
|
@@ -133,6 +128,4 @@ module PushyDaemon
|
|
133
128
|
end
|
134
129
|
|
135
130
|
end
|
136
|
-
|
137
131
|
end
|
138
|
-
|
data/lib/pushyd/shouter.rb
CHANGED
@@ -39,10 +39,6 @@ module PushyDaemon
|
|
39
39
|
raise PushyDaemon::EndpointTopicContext unless @topic
|
40
40
|
@exchange = @channel.topic(@topic, durable: true, persistent: true)
|
41
41
|
|
42
|
-
# if shout_config.is_a? Hash
|
43
|
-
# shout_keys = shout_config[:keys] if config_shout[:keys].is_a? Array
|
44
|
-
# end
|
45
|
-
|
46
42
|
rescue Bunny::TCPConnectionFailedForAllHosts => e
|
47
43
|
error "ERROR: cannot connect to RabbitMQ hosts (#{e.inspect})"
|
48
44
|
end
|
@@ -72,14 +68,14 @@ module PushyDaemon
|
|
72
68
|
private
|
73
69
|
|
74
70
|
def channel_shout keys, body = {}
|
75
|
-
#
|
71
|
+
# Prepare headers
|
76
72
|
headers = {
|
77
73
|
sent_at: DateTime.now.iso8601,
|
78
74
|
sent_by: Conf.name
|
79
75
|
}
|
80
|
-
exchange_name = @exchange.name
|
81
76
|
|
82
|
-
# Prepare
|
77
|
+
# Prepare exchange_name and routing_key
|
78
|
+
exchange_name = @exchange.name
|
83
79
|
routing_key = keys.unshift(exchange_name).join('.')
|
84
80
|
|
85
81
|
# Announce shout
|
@@ -97,4 +93,3 @@ module PushyDaemon
|
|
97
93
|
|
98
94
|
end
|
99
95
|
end
|
100
|
-
|
data/pushyd.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
Gem::Specification.new do |spec|
|
3
3
|
# Project version
|
4
|
-
spec.version = "0.2.
|
4
|
+
spec.version = "0.2.4"
|
5
5
|
|
6
6
|
# Project description
|
7
7
|
spec.name = "pushyd"
|
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
|
|
32
32
|
spec.add_runtime_dependency "chamber"
|
33
33
|
spec.add_runtime_dependency "json"
|
34
34
|
spec.add_runtime_dependency "bunny"
|
35
|
-
spec.add_runtime_dependency "
|
35
|
+
spec.add_runtime_dependency "rest-client"
|
36
36
|
spec.add_runtime_dependency "terminal-table"
|
37
37
|
# spec.add_runtime_dependency "newrelic_rpm"
|
38
38
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pushyd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bruno MEDICI
|
@@ -137,7 +137,7 @@ dependencies:
|
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
139
|
- !ruby/object:Gem::Dependency
|
140
|
-
name:
|
140
|
+
name: rest-client
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
142
142
|
requirements:
|
143
143
|
- - ">="
|
@@ -183,6 +183,7 @@ files:
|
|
183
183
|
- lib/pushyd/constants.rb
|
184
184
|
- lib/pushyd/daemon.rb
|
185
185
|
- lib/pushyd/endpoint.rb
|
186
|
+
- lib/pushyd/proxy.orig.rb
|
186
187
|
- lib/pushyd/proxy.rb
|
187
188
|
- lib/pushyd/pushy_logger.rb
|
188
189
|
- lib/pushyd/shouter.rb
|