delivery_boy 0.1.4 → 0.1.5

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: b22537575f9af9b44a9a9f285e74d073b48bec92
4
- data.tar.gz: 2505ca15d3d2b60a94fb4c88f1a959803d334c44
3
+ metadata.gz: fd80f951111ad29fdbf5ff8bbc02aef63b59d176
4
+ data.tar.gz: b9c6b8209940ef36ce24e8aec5261483100726bf
5
5
  SHA512:
6
- metadata.gz: c5c0e279ebcd829f3b0961d10f705fc74d5307a983547c049542d391361daafdb71ef8c1747770e4b946e56d470e88a78d81b811c9995d26380fcf2ffcb5de13
7
- data.tar.gz: 69ef0053f5a4873a6deaa9d7071502adb6ee677d5c9678d33467c1dd65473a6e76a97cd6427fe8b1e23feda1e83c506543ddb6010930d990f3aa71ca27b02edc
6
+ metadata.gz: ddc0d55c7acb75817794d542c1ac132292c39d466f5d65ddc969f51c57815bd8e5951d9e12aa3391263c3bfc3312f4db86e814e13c905c8c790ef19155328070
7
+ data.tar.gz: 0ae524907b55be94d29b2c2849c87ea8d81ba28b80d8f94d9e0ff12822258ced4a0f9581e40e803db5557058b4ab58cf0905a5a8b01cd83e4a9883bd3fa0d00f
data/README.md CHANGED
@@ -71,6 +71,8 @@ $ bundle exec rails generate delivery_boy:install
71
71
 
72
72
  This will create a config file at `config/delivery_boy.yml` with configurations for each of your Rails environments. Open that file in order to make changes.
73
73
 
74
+ Note that for most configuration variables, you can pass in an environment variable. These environment variables all take the form `DELIVERY_BOY_X`, where `X` is the upper-case configuration variable name, e.g. `DELIVERY_BOY_CLIENT_ID`.
75
+
74
76
  The following configuration variables can be set:
75
77
 
76
78
  #### Basic
@@ -75,6 +75,7 @@ module DeliveryBoy
75
75
  def load_env(env)
76
76
  loader = EnvConfigLoader.new(env, self)
77
77
 
78
+ loader.string :client_id
78
79
  loader.string_list :brokers
79
80
  loader.integer :ack_timeout
80
81
  loader.symbol :compression_codec
@@ -0,0 +1,28 @@
1
+ module DeliveryBoy
2
+
3
+ # A fake implementation that is useful for testing.
4
+ class Fake
5
+ FakeMessage = Struct.new(:value, :key, :topic, :partition, :partition_key)
6
+
7
+ def initialize
8
+ @messages = Hash.new {|h, k| h[k] = [] }
9
+ end
10
+
11
+ def deliver(value, topic:, **options)
12
+ message = FakeMessage.new(value: value, topic: topic, **options)
13
+ @messages[topic] << message
14
+
15
+ nil
16
+ end
17
+
18
+ alias deliver_async! deliver
19
+
20
+ def shutdown
21
+ @messages.clear
22
+ end
23
+
24
+ def messages_for(topic)
25
+ @messages[topic]
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,86 @@
1
+ module DeliveryBoy
2
+
3
+ # This class implements the actual logic of DeliveryBoy. The DeliveryBoy module
4
+ # has a module-level singleton instance.
5
+ class Instance
6
+ def initialize(config, logger)
7
+ @config = config
8
+ @logger = logger
9
+ end
10
+
11
+ def deliver(value, topic:, **options)
12
+ sync_producer.produce(value, topic: topic, **options)
13
+ sync_producer.deliver_messages
14
+ rescue
15
+ # Make sure to clear any buffered messages if there's an error.
16
+ sync_producer.clear_buffer
17
+
18
+ raise
19
+ end
20
+
21
+ def deliver_async!(value, topic:, **options)
22
+ async_producer.produce(value, topic: topic, **options)
23
+ end
24
+
25
+ def shutdown
26
+ sync_producer.shutdown if sync_producer?
27
+ async_producer.shutdown if async_producer?
28
+ end
29
+
30
+ private
31
+
32
+ attr_reader :config, :logger
33
+
34
+ def sync_producer
35
+ # We want synchronous producers to be per-thread in order to avoid problems with
36
+ # concurrent deliveries.
37
+ Thread.current[:delivery_boy_sync_producer] ||= kafka.producer(**producer_options)
38
+ end
39
+
40
+ def sync_producer?
41
+ Thread.current.key?(:delivery_boy_sync_producer)
42
+ end
43
+
44
+ def async_producer
45
+ # The async producer doesn't have to be per-thread, since all deliveries are
46
+ # performed by a single background thread.
47
+ @async_producer ||= kafka.async_producer(
48
+ max_queue_size: config.max_queue_size,
49
+ delivery_threshold: config.delivery_threshold,
50
+ delivery_interval: config.delivery_interval,
51
+ **producer_options,
52
+ )
53
+ end
54
+
55
+ def async_producer?
56
+ !@async_producer.nil?
57
+ end
58
+
59
+ def kafka
60
+ @kafka ||= Kafka.new(
61
+ seed_brokers: config.brokers,
62
+ client_id: config.client_id,
63
+ logger: logger,
64
+ connect_timeout: config.connect_timeout,
65
+ socket_timeout: config.socket_timeout,
66
+ ssl_ca_cert: config.ssl_ca_cert,
67
+ ssl_client_cert: config.ssl_client_cert,
68
+ ssl_client_cert_key: config.ssl_client_cert_key,
69
+ )
70
+ end
71
+
72
+ # Options for both the sync and async producers.
73
+ def producer_options
74
+ {
75
+ required_acks: config.required_acks,
76
+ ack_timeout: config.ack_timeout,
77
+ max_retries: config.max_retries,
78
+ retry_backoff: config.retry_backoff,
79
+ max_buffer_size: config.max_buffer_size,
80
+ max_buffer_bytesize: config.max_buffer_bytesize,
81
+ compression_codec: config.compression_codec,
82
+ compression_threshold: config.compression_threshold,
83
+ }
84
+ end
85
+ end
86
+ end
@@ -1,3 +1,3 @@
1
1
  module DeliveryBoy
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/delivery_boy.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require "logger"
2
2
  require "kafka"
3
3
  require "delivery_boy/version"
4
+ require "delivery_boy/instance"
5
+ require "delivery_boy/fake"
4
6
  require "delivery_boy/config"
5
7
  require "delivery_boy/railtie" if defined?(Rails)
6
8
 
@@ -23,13 +25,7 @@ module DeliveryBoy
23
25
  # @raise [Kafka::BufferOverflow] if the producer's buffer is full.
24
26
  # @raise [Kafka::DeliveryFailed] if delivery failed for some reason.
25
27
  def deliver(value, topic:, **options)
26
- sync_producer.produce(value, topic: topic, **options)
27
- sync_producer.deliver_messages
28
- rescue
29
- # Make sure to clear any buffered messages if there's an error.
30
- sync_producer.clear_buffer
31
-
32
- raise
28
+ instance.deliver(value, topic: topic, **options)
33
29
  end
34
30
 
35
31
  # Like {.deliver_async!}, but handles +Kafka::BufferOverflow+ errors
@@ -48,7 +44,7 @@ module DeliveryBoy
48
44
  #
49
45
  # @return [nil]
50
46
  def deliver_async!(value, topic:, **options)
51
- async_producer.produce(value, topic: topic, **options)
47
+ instance.deliver_async!(value, topic: topic, **options)
52
48
  end
53
49
 
54
50
  # Shut down DeliveryBoy.
@@ -57,8 +53,7 @@ module DeliveryBoy
57
53
  #
58
54
  # @return [nil]
59
55
  def shutdown
60
- sync_producer.shutdown if sync_producer?
61
- async_producer.shutdown if async_producer?
56
+ instance.shutdown
62
57
  end
63
58
 
64
59
  # The logger used by DeliveryBoy.
@@ -77,58 +72,18 @@ module DeliveryBoy
77
72
  @config ||= DeliveryBoy::Config.new(env: ENV)
78
73
  end
79
74
 
80
- private
81
-
82
- def sync_producer
83
- # We want synchronous producers to be per-thread in order to avoid problems with
84
- # concurrent deliveries.
85
- Thread.current[:delivery_boy_sync_producer] ||= kafka.producer(**producer_options)
86
- end
87
-
88
- def sync_producer?
89
- Thread.current.key?(:delivery_boy_sync_producer)
75
+ def test_mode!
76
+ @instance = testing
90
77
  end
91
78
 
92
- def async_producer
93
- # The async producer doesn't have to be per-thread, since all deliveries are
94
- # performed by a single background thread.
95
- @async_producer ||= kafka.async_producer(
96
- max_queue_size: config.max_queue_size,
97
- delivery_threshold: config.delivery_threshold,
98
- delivery_interval: config.delivery_interval,
99
- **producer_options,
100
- )
79
+ def testing
80
+ @testing ||= Fake.new
101
81
  end
102
82
 
103
- def async_producer?
104
- !@async_producer.nil?
105
- end
106
-
107
- def kafka
108
- @kafka ||= Kafka.new(
109
- seed_brokers: config.brokers,
110
- client_id: config.client_id,
111
- logger: logger,
112
- connect_timeout: config.connect_timeout,
113
- socket_timeout: config.socket_timeout,
114
- ssl_ca_cert: config.ssl_ca_cert,
115
- ssl_client_cert: config.ssl_client_cert,
116
- ssl_client_cert_key: config.ssl_client_cert_key,
117
- )
118
- end
83
+ private
119
84
 
120
- # Options for both the sync and async producers.
121
- def producer_options
122
- {
123
- required_acks: config.required_acks,
124
- ack_timeout: config.ack_timeout,
125
- max_retries: config.max_retries,
126
- retry_backoff: config.retry_backoff,
127
- max_buffer_size: config.max_buffer_size,
128
- max_buffer_bytesize: config.max_buffer_bytesize,
129
- compression_codec: config.compression_codec,
130
- compression_threshold: config.compression_threshold,
131
- }
85
+ def instance
86
+ @instance ||= Instance.new(config, logger)
132
87
  end
133
88
  end
134
89
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delivery_boy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Schierbeck
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-07 00:00:00.000000000 Z
11
+ date: 2017-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-kafka
@@ -90,6 +90,8 @@ files:
90
90
  - lib/delivery_boy/config_error.rb
91
91
  - lib/delivery_boy/config_file_loader.rb
92
92
  - lib/delivery_boy/env_config_loader.rb
93
+ - lib/delivery_boy/fake.rb
94
+ - lib/delivery_boy/instance.rb
93
95
  - lib/delivery_boy/railtie.rb
94
96
  - lib/delivery_boy/version.rb
95
97
  - lib/generators/delivery_boy/config_file.yml.erb