pushyd 0.8.2 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa2f4f22cf8e3d98832400f34893f1128365332f
4
- data.tar.gz: e8307c72a56774a3eb6f8b33d888a88fc1ffaeda
3
+ metadata.gz: 8eb02de265011f5fec1f4d59bc19ea7c74e04cf4
4
+ data.tar.gz: adeae96fac08c64d054849f8a5ecb740f42310e9
5
5
  SHA512:
6
- metadata.gz: 82235ff0e466f37ae7bb1b200e0e49419e2be4116d84616cbf9473d60919640adbcc45855255aaf36c19a8ddc74c6a88e96a064d69ed307d25290a888c77ebc0
7
- data.tar.gz: 3d43ed96eedd95cf24e3471cd4f5975e6a550559d4dd777334ba398c5c1b515dc38718a49f6524e508631c85322dcbad3f6ce421efd70744c8b72bbff5e19b2e
6
+ metadata.gz: 7233e7947eaaaf3111cbfea945a92b9cedcaa11baeb64b95646944ee148a96ed2c1ceba16e8f675721f4c584986493aff1f9d7a20a7fa8e127e0d8fab413ba38
7
+ data.tar.gz: 7b19b454c1e3afd721cbd78a99c4752ec70daec74413c558c31533de8677ce054c21a38cf314e597ad7819e3bc8aa1291fa542081191d023a62af017ffb98f06
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pushyd (0.8.2)
4
+ pushyd (0.9.0)
5
5
  api-auth
6
6
  bunny (~> 2.3)
7
7
  chamber (~> 2.9)
data/defaults.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  # common defaults
2
- amqp: amqp://guest:guest@localhost:5672/%2F
2
+ broker: amqp://guest:guest@localhost:5672/%2F
3
3
  logs:
4
4
  path: '/tmp/'
5
5
  file: pushyd.log
@@ -9,14 +9,14 @@ logs:
9
9
  shout:
10
10
  topic: pushyd
11
11
  period: 10
12
- # keys:
13
- # - tic
14
- # - tac
15
- # - toe
16
- # - created
17
- # - updated
18
- # - deleted
19
- # - crunched
12
+ keys:
13
+ - tic
14
+ - tac
15
+ - toe
16
+ - created
17
+ - updated
18
+ - deleted
19
+ - crunched
20
20
 
21
21
  # newrelic:
22
22
  # licence: ""
@@ -7,6 +7,9 @@ WAY_PROP = "PROP"
7
7
  PROXY_MESSAGE_MAX = 1
8
8
  PROXY_USE_ACK = false
9
9
 
10
+ # Constants: shouter
11
+ SHOUTER_SENTAT_DECIMALS = 6
12
+
10
13
  # Constants: logger
11
14
  LOG_ROTATION = "daily"
12
15
 
@@ -74,7 +74,12 @@ module PushyDaemon
74
74
  # Start connexion to RabbitMQ
75
75
  def connect_channel busconf
76
76
  fail PushyDaemon::EndpointConnexionContext, "invalid bus host/port" unless busconf
77
- info "connecting to #{busconf}"
77
+ info "connecting to bus", {
78
+ broker: busconf,
79
+ recover: AMQP_RECOVERY_INTERVAL,
80
+ heartbeat: AMQP_HEARTBEAT_INTERVAL,
81
+ prefetch: AMQP_PREFETCH
82
+ }
78
83
  conn = Bunny.new busconf.to_s,
79
84
  logger: @logger,
80
85
  # heartbeat: :server,
@@ -83,7 +88,6 @@ module PushyDaemon
83
88
  heartbeat_interval: AMQP_HEARTBEAT_INTERVAL
84
89
  conn.start
85
90
 
86
-
87
91
  # Create channel, prefetch only one message at a time
88
92
  channel = conn.create_channel
89
93
  channel.prefetch(AMQP_PREFETCH)
@@ -151,6 +155,17 @@ module PushyDaemon
151
155
  rand(36**len).to_s(36)
152
156
  end
153
157
 
158
+ def format_bytes number, unit="", decimals = 0
159
+ return "Ø" if number.nil? || number.to_f.zero?
160
+
161
+ units = ["", "k", "M", "G", "T", "P" ]
162
+ index = ( Math.log(number) / Math.log(2) ).to_i / 10
163
+ converted = number.to_f / (1024 ** index)
164
+
165
+ truncated = converted.round(decimals)
166
+ return "#{truncated} #{units[index]}#{unit}"
167
+ end
168
+
154
169
  private
155
170
 
156
171
  end
data/lib/pushyd/proxy.rb CHANGED
@@ -20,7 +20,7 @@ module PushyDaemon
20
20
  @table.align_column(5, :right)
21
21
 
22
22
  # Start connexion to RabbitMQ and create channel
23
- @channel = connect_channel Conf.amqp
23
+ @channel = connect_channel Conf[:broker]
24
24
  log_info "channel connected"
25
25
 
26
26
  # Check config
@@ -45,6 +45,19 @@ module PushyDaemon
45
45
 
46
46
  private
47
47
 
48
+ def extract_delay msg_headers
49
+ return unless msg_headers['sent_at']
50
+
51
+ # Extract sent_at header
52
+ sent_at = Time.iso8601(msg_headers['sent_at'])
53
+ # log_info "sent_at : #{sent_at.to_f}"
54
+ # log_info "timenow : #{Time.now.to_f}"
55
+
56
+ # Compute delay
57
+ return ((Time.now - sent_at)*1000).round(2)
58
+ end
59
+
60
+
48
61
  # Handle the reception of a message on a queue
49
62
  def handle_message rule, delivery_info, metadata, payload
50
63
  # Prepare data
@@ -53,14 +66,20 @@ module PushyDaemon
53
66
  msg_rkey = delivery_info.routing_key.force_encoding('UTF-8')
54
67
  msg_headers = metadata.headers || {}
55
68
 
56
- # Extract fields
57
- data = parse payload, metadata.content_type #, rule
69
+ # Compute delay and size
70
+ delay = extract_delay(msg_headers)
71
+ size = format_bytes(payload.bytesize, "B")
72
+
73
+ # Extract payload
74
+ data = parse payload, metadata.content_type
58
75
 
59
76
  # Announce match
60
77
  log_message MSG_RECV, msg_exchange, msg_rkey, data, {
61
- 'rule' => rule_name,
78
+ 'matched rule' => rule_name,
62
79
  'app-id' => metadata.app_id,
63
80
  'content-type' => metadata.content_type,
81
+ 'delay (ms)' => delay,
82
+ 'body size' => size,
64
83
  }
65
84
 
66
85
  # Build notification payload
@@ -27,7 +27,7 @@ module PushyDaemon
27
27
  @period = config_shout[:period] || 0
28
28
 
29
29
  # Start connexion to RabbitMQ and create channel
30
- @channel = connect_channel Conf.amqp
30
+ @channel = connect_channel Conf[:broker]
31
31
  log_info "channel connected"
32
32
 
33
33
  # Create exchange
@@ -44,9 +44,21 @@ module PushyDaemon
44
44
 
45
45
  # Prepare exchange
46
46
  loop do
47
+ # Generate key and fake id
48
+ if @keys.is_a?(Array) && @keys.any?
49
+ random_key = @keys.sample
50
+ else
51
+ random_key = "random"
52
+ end
53
+ # random_key = @keys.class
47
54
  random_string = SecureRandom.hex
48
- random_key = @keys.sample || "random"
49
- channel_shout [:ping, random_key, random_string], {dummy: true, time: Time.now.to_f, host: Conf.host}
55
+
56
+ # Generate payload
57
+ #payload = {time: Time.now.to_f, host: Conf.host}
58
+ payload = nil
59
+
60
+ # Shout it !
61
+ channel_shout [:ping, random_key, random_string], payload
50
62
  sleep @period
51
63
  end
52
64
  rescue AMQ::Protocol::EmptyResponseError => e
@@ -65,19 +77,20 @@ module PushyDaemon
65
77
  private
66
78
 
67
79
  def channel_shout keys, body = {}
68
- # Prepare headers
69
- headers = {
70
- sent_at: DateTime.now.iso8601,
71
- sent_by: Conf.app_name,
72
- }
73
-
74
80
  # Prepare exchange_name and routing_key
75
81
  exchange_name = @exchange.name
76
- routing_key = keys.unshift(exchange_name).join('.')
82
+ keys.unshift(exchange_name)
83
+ routing_key = keys.join('.')
77
84
 
78
85
  # Announce shout
79
86
  log_message MSG_SEND, exchange_name, routing_key, body
80
87
 
88
+ # Prepare headers
89
+ headers = {
90
+ sent_at: DateTime.now.iso8601(SHOUTER_SENTAT_DECIMALS),
91
+ sent_by: Conf.app_name,
92
+ }
93
+
81
94
  # Publish
82
95
  @exchange.publish(body.to_json,
83
96
  routing_key: routing_key,
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.8.2"
4
+ spec.version = "0.9.0"
5
5
 
6
6
  # Project description
7
7
  spec.name = "pushyd"
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.8.2
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno MEDICI