twingly-amqp 3.3.0 → 4.0.0
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/README.md +24 -16
- data/lib/twingly/amqp.rb +2 -4
- data/lib/twingly/amqp/connection.rb +0 -1
- data/lib/twingly/amqp/ping_options.rb +39 -0
- data/lib/twingly/amqp/{ping.rb → pinger.rb} +20 -18
- data/lib/twingly/amqp/session.rb +14 -12
- data/lib/twingly/amqp/subscription.rb +2 -12
- data/lib/twingly/amqp/version.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6219ee67359e165f827b9c944028390b4506a0e6
|
4
|
+
data.tar.gz: 83fedd73df0b32f0d9929001ca2bc431ab2149c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2e923cc07303a09621892d0405093a247c4828c02060c585fa5a011b70321b14cd3adfa12560338d67c68d773cb214d2e2f876b9c0117fceb00a4d24979a76ef
|
7
|
+
data.tar.gz: ad3c376131f4309417d7d0292da8ca35347478469d390fc543ecbb72abcab06d78eddab1cc8a90d2daa6a0fa4dc1bbc16c3acca02faec34689fc991ad833c039
|
data/README.md
CHANGED
@@ -21,14 +21,14 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
Environment variables:
|
23
23
|
|
24
|
-
* `RABBITMQ_N_HOST`
|
25
|
-
* `AMQP_USERNAME`
|
26
|
-
* `AMQP_PASSWORD`
|
27
|
-
* `AMQP_TLS`
|
24
|
+
* `RABBITMQ_N_HOST` - Defaults to `localhost`
|
25
|
+
* `AMQP_USERNAME` - Defaults to `guest`
|
26
|
+
* `AMQP_PASSWORD` - Defaults to `guest`
|
27
|
+
* `AMQP_TLS` - Use TLS connection if set
|
28
28
|
|
29
29
|
### Customize options
|
30
30
|
|
31
|
-
If you don't have the RabbitMQ hosts, user or password in your ENV you can set them with `Twingly::AMQP::Connection.options=` before you create an instance of `Subscription` or `
|
31
|
+
If you don't have the RabbitMQ hosts, user or password in your ENV you can set them with `Twingly::AMQP::Connection.options=` before you create an instance of `Subscription` or `Pinger`.
|
32
32
|
|
33
33
|
*Options set in `Connection.options=` take precedence over the options defined in `ENV`.*
|
34
34
|
|
@@ -59,9 +59,9 @@ subscription = Twingly::AMQP::Subscription.new(
|
|
59
59
|
subscription.on_exception { |exception| puts "Oh noes! #{exception.message}" }
|
60
60
|
subscription.before_handle_message { |raw_message_payload| puts raw_message }
|
61
61
|
|
62
|
-
subscription.
|
62
|
+
subscription.each_message do |message| # An instance of Twingly::AMQP::Message
|
63
63
|
begin
|
64
|
-
response = client.post(payload.fetch(:url))
|
64
|
+
response = client.post(message.payload.fetch(:url))
|
65
65
|
|
66
66
|
case response.code
|
67
67
|
when 200 then message.ack # No error
|
@@ -78,29 +78,37 @@ end
|
|
78
78
|
### Ping urls
|
79
79
|
|
80
80
|
```ruby
|
81
|
-
pinger = Twingly::AMQP::
|
82
|
-
provider_name: "a-provider-name",
|
81
|
+
pinger = Twingly::AMQP::Pinger.new(
|
83
82
|
queue_name: "provider-ping",
|
84
|
-
priority: 1,
|
85
|
-
source_ip: "?.?.?.?", # Optional, can be given to #ping
|
86
83
|
url_cache: url_cache, # Optional, see below
|
87
84
|
)
|
88
85
|
|
86
|
+
# Optional, options can also be given to #ping
|
87
|
+
pinger.default_ping_options do |options|
|
88
|
+
options.provider_name = "TestProvider"
|
89
|
+
options.source_ip = "?.?.?.?"
|
90
|
+
options.priority = 1
|
91
|
+
end
|
92
|
+
|
89
93
|
urls = [
|
90
94
|
"http://blog.twingly.com",
|
91
95
|
]
|
92
96
|
|
93
|
-
|
97
|
+
# Optional, is merged with the default options above
|
98
|
+
options = {
|
99
|
+
provider_name: "a-provider-name",
|
100
|
+
source_ip: "?.?.?.?",
|
101
|
+
priority: 1,
|
102
|
+
}
|
103
|
+
|
104
|
+
pinger.ping(urls, options) do |pinged_url|
|
94
105
|
# Optional block that gets called for each pinged url
|
95
106
|
end
|
96
|
-
|
97
|
-
# Send a ping using another source ip
|
98
|
-
pinger.ping(urls, source_ip: "1.2.3.4")
|
99
107
|
```
|
100
108
|
|
101
109
|
#### Url cache
|
102
110
|
|
103
|
-
`Twingly::AMQP::
|
111
|
+
`Twingly::AMQP::Pinger.new` can optionally take an url cache which caches the urls and only pings in the urls that isn't already cached. The cache needs to respond to the two following methods:
|
104
112
|
|
105
113
|
```ruby
|
106
114
|
class UrlCache
|
data/lib/twingly/amqp.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
require "twingly/amqp/version"
|
2
|
-
|
3
|
-
ENV["RUBY_ENV"] ||= "development"
|
4
|
-
|
5
2
|
require "twingly/amqp/session"
|
6
3
|
require "twingly/amqp/connection"
|
7
4
|
require "twingly/amqp/subscription"
|
8
|
-
require "twingly/amqp/
|
5
|
+
require "twingly/amqp/ping_options"
|
6
|
+
require "twingly/amqp/pinger"
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Twingly
|
2
|
+
module AMQP
|
3
|
+
class PingOptions
|
4
|
+
attr_accessor :provider_name, :source_ip, :priority
|
5
|
+
|
6
|
+
def initialize(provider_name: nil, source_ip: nil, priority: nil)
|
7
|
+
self.provider_name = provider_name
|
8
|
+
self.source_ip = source_ip
|
9
|
+
self.priority = priority
|
10
|
+
|
11
|
+
yield self if block_given?
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_h
|
15
|
+
{
|
16
|
+
automatic_ping: false,
|
17
|
+
provider_name: provider_name,
|
18
|
+
source_ip: source_ip,
|
19
|
+
priority: priority,
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
def validate
|
24
|
+
missing_keys = to_h.select { |_, value| value.to_s.empty? }.keys
|
25
|
+
if missing_keys.any?
|
26
|
+
fail ArgumentError, "Required options not set: #{missing_keys}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def merge(other)
|
31
|
+
PingOptions.new do |options|
|
32
|
+
options.provider_name = other.provider_name || provider_name
|
33
|
+
options.source_ip = other.source_ip || source_ip
|
34
|
+
options.priority = other.priority || priority
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -1,22 +1,26 @@
|
|
1
1
|
require "twingly/amqp/connection"
|
2
|
+
require "twingly/amqp/ping_options"
|
2
3
|
require "json"
|
3
4
|
|
4
5
|
module Twingly
|
5
6
|
module AMQP
|
6
|
-
class
|
7
|
-
def initialize(
|
8
|
-
@
|
9
|
-
|
10
|
-
@provider_name = provider_name
|
11
|
-
@queue_name = queue_name
|
12
|
-
@source_ip = source_ip
|
13
|
-
@priority = priority
|
7
|
+
class Pinger
|
8
|
+
def initialize(queue_name:, url_cache: NullCache, connection: nil)
|
9
|
+
@queue_name = queue_name
|
10
|
+
@url_cache = url_cache
|
14
11
|
|
15
12
|
connection ||= Connection.instance
|
16
13
|
@channel = connection.create_channel
|
14
|
+
|
15
|
+
@default_ping_options = PingOptions.new
|
17
16
|
end
|
18
17
|
|
19
|
-
def ping(urls,
|
18
|
+
def ping(urls, options_hash = {})
|
19
|
+
options = PingOptions.new(options_hash)
|
20
|
+
options = @default_ping_options.merge(options)
|
21
|
+
|
22
|
+
options.validate
|
23
|
+
|
20
24
|
Array(urls).each do |url|
|
21
25
|
unless cached?(url)
|
22
26
|
publish(url, options)
|
@@ -27,6 +31,10 @@ module Twingly
|
|
27
31
|
end
|
28
32
|
end
|
29
33
|
|
34
|
+
def default_ping_options
|
35
|
+
yield @default_ping_options
|
36
|
+
end
|
37
|
+
|
30
38
|
private
|
31
39
|
|
32
40
|
def publish(url, options)
|
@@ -43,16 +51,10 @@ module Twingly
|
|
43
51
|
end
|
44
52
|
|
45
53
|
def message(url, options)
|
46
|
-
|
47
|
-
|
54
|
+
ping_message = options.to_h
|
55
|
+
ping_message[:url] = url
|
48
56
|
|
49
|
-
|
50
|
-
automatic_ping: false,
|
51
|
-
provider_name: @provider_name,
|
52
|
-
priority: @priority,
|
53
|
-
source_ip: source_ip,
|
54
|
-
url: url,
|
55
|
-
}
|
57
|
+
ping_message
|
56
58
|
end
|
57
59
|
|
58
60
|
def cached?(url)
|
data/lib/twingly/amqp/session.rb
CHANGED
@@ -1,8 +1,14 @@
|
|
1
|
+
require "bunny"
|
2
|
+
|
1
3
|
module Twingly
|
2
4
|
module AMQP
|
3
5
|
class Session
|
4
6
|
attr_reader :connection, :options
|
5
7
|
|
8
|
+
DEFAULT_USER = "guest"
|
9
|
+
DEFAULT_PASS = "guest"
|
10
|
+
DEFAULT_HOSTS = ["localhost"]
|
11
|
+
|
6
12
|
def initialize(options = {})
|
7
13
|
@options = options
|
8
14
|
@connection = create_connection
|
@@ -11,11 +17,7 @@ module Twingly
|
|
11
17
|
private
|
12
18
|
|
13
19
|
def create_connection
|
14
|
-
|
15
|
-
connection = Bunny.new
|
16
|
-
else
|
17
|
-
connection = Bunny.new(connection_options)
|
18
|
-
end
|
20
|
+
connection = Bunny.new(connection_options)
|
19
21
|
connection.start
|
20
22
|
connection
|
21
23
|
end
|
@@ -34,26 +36,26 @@ module Twingly
|
|
34
36
|
}
|
35
37
|
end
|
36
38
|
|
37
|
-
def ruby_env
|
38
|
-
ENV.fetch("RUBY_ENV")
|
39
|
-
end
|
40
|
-
|
41
39
|
def tls?
|
42
40
|
ENV.has_key?("AMQP_TLS")
|
43
41
|
end
|
44
42
|
|
45
43
|
def user_from_env
|
46
|
-
ENV.fetch("AMQP_USERNAME")
|
44
|
+
ENV.fetch("AMQP_USERNAME") { DEFAULT_USER }
|
47
45
|
end
|
48
46
|
|
49
47
|
def password_from_env
|
50
|
-
ENV.fetch("AMQP_PASSWORD")
|
48
|
+
ENV.fetch("AMQP_PASSWORD") { DEFAULT_PASS }
|
51
49
|
end
|
52
50
|
|
53
51
|
def hosts_from_env
|
54
52
|
# Matches env keys like `RABBITMQ_01_HOST`
|
55
53
|
environment_keys_with_host = ENV.keys.select { |key| key =~ /^rabbitmq_\d+_host$/i }
|
56
|
-
environment_keys_with_host.map { |key| ENV[key] }
|
54
|
+
hosts = environment_keys_with_host.map { |key| ENV[key] }
|
55
|
+
|
56
|
+
return DEFAULT_HOSTS if hosts.empty?
|
57
|
+
|
58
|
+
hosts
|
57
59
|
end
|
58
60
|
end
|
59
61
|
end
|
@@ -24,7 +24,7 @@ module Twingly
|
|
24
24
|
@on_exception_callback = Proc.new {}
|
25
25
|
end
|
26
26
|
|
27
|
-
def
|
27
|
+
def each_message(&block)
|
28
28
|
setup_traps
|
29
29
|
|
30
30
|
consumer = @queue.subscribe(subscribe_options) do |delivery_info, metadata, payload|
|
@@ -45,7 +45,6 @@ module Twingly
|
|
45
45
|
sleep 0.5
|
46
46
|
end
|
47
47
|
|
48
|
-
puts "Shutting down" if development?
|
49
48
|
consumer.cancel
|
50
49
|
end
|
51
50
|
|
@@ -71,20 +70,11 @@ module Twingly
|
|
71
70
|
channel = connection.create_channel(nil, @consumer_threads)
|
72
71
|
channel.prefetch(@prefetch)
|
73
72
|
channel.on_uncaught_exception do |exception, _|
|
74
|
-
puts exception.message, exception.backtrace if development?
|
75
73
|
@on_exception_callback.call(exception)
|
76
74
|
end
|
77
75
|
channel
|
78
76
|
end
|
79
77
|
|
80
|
-
def development?
|
81
|
-
ruby_env == "development"
|
82
|
-
end
|
83
|
-
|
84
|
-
def ruby_env
|
85
|
-
ENV.fetch("RUBY_ENV")
|
86
|
-
end
|
87
|
-
|
88
78
|
def queue_options
|
89
79
|
{
|
90
80
|
durable: true,
|
@@ -99,7 +89,7 @@ module Twingly
|
|
99
89
|
end
|
100
90
|
|
101
91
|
def consumer_tag
|
102
|
-
tag_name =
|
92
|
+
tag_name = Socket.gethostname
|
103
93
|
@channel.generate_consumer_tag(tag_name)
|
104
94
|
end
|
105
95
|
|
data/lib/twingly/amqp/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twingly-amqp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 4.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Twingly AB
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bunny
|
@@ -63,7 +63,8 @@ files:
|
|
63
63
|
- lib/twingly/amqp.rb
|
64
64
|
- lib/twingly/amqp/connection.rb
|
65
65
|
- lib/twingly/amqp/message.rb
|
66
|
-
- lib/twingly/amqp/
|
66
|
+
- lib/twingly/amqp/ping_options.rb
|
67
|
+
- lib/twingly/amqp/pinger.rb
|
67
68
|
- lib/twingly/amqp/session.rb
|
68
69
|
- lib/twingly/amqp/subscription.rb
|
69
70
|
- lib/twingly/amqp/version.rb
|
@@ -86,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
87
|
version: '0'
|
87
88
|
requirements: []
|
88
89
|
rubyforge_project:
|
89
|
-
rubygems_version: 2.
|
90
|
+
rubygems_version: 2.5.0
|
90
91
|
signing_key:
|
91
92
|
specification_version: 4
|
92
93
|
summary: Ruby library for talking to RabbitMQ
|