jackhammer 1.1.0 → 1.2.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
  SHA256:
3
- metadata.gz: 24bc68715c2aa86d9452a0c4e4ac0cf3ca7505d1e985a1f109d67b74a497d03c
4
- data.tar.gz: dcc42e3bef975c828c73ca65cdab23c921c258977e6692fb522c00ef98a2eb51
3
+ metadata.gz: b05bbadd40bdcdbf69d7ed079f5da3b5ef7494e5cfab42288b0e114aa1effb7c
4
+ data.tar.gz: 4db8060a85601dae226889a134531c48bf4b2b389b399f9bf782985f6909dd03
5
5
  SHA512:
6
- metadata.gz: 174982c352090d5bde707a1d4046093fa356512bfc2b5b9754ebe49d8e7ee3f02624719ee87567a7930ef00a583532b816a490b95d8d5efba283dc823cc309ba
7
- data.tar.gz: bc4423245e71589db053aba661a79ed2adb84e2fcbdf34187139c91c4e16942c2cd8dddb60c0824fe916f5487107342211a48305b143a914bf3b834a8804784a
6
+ metadata.gz: 3685542942aa0dcb6568bfa408a44015eeeade76268f610bd7a119feb8d6092e6c588dc846c7699325646ca86e6e242c7935b5eac1efa2e6334bdef1bfafcdd2
7
+ data.tar.gz: 16300121a91938775bf6f4a6e7e89cae0a4764653fc64e4e92f8c7a655be7e2953ff8a473a95f8d5205909150ac2226a641c7c687b06f30abff8e412c87a919e
@@ -0,0 +1,18 @@
1
+ version: 1
2
+
3
+ update_configs:
4
+ - package_manager: "ruby:bundler"
5
+ directory: "/"
6
+ update_schedule: "monthly"
7
+ commit_message:
8
+ prefix: "[dependabot]"
9
+ allowed_updates:
10
+ - match:
11
+ update_type: "all"
12
+ automerged_updates:
13
+ - match:
14
+ dependency_type: "development"
15
+ update_type: "all"
16
+ - match:
17
+ dependency_type: "production"
18
+ update_type: "all"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jackhammer (1.1.0)
4
+ jackhammer (1.2.0)
5
5
  bunny (~> 2.14)
6
6
 
7
7
  GEM
@@ -13,11 +13,11 @@ GEM
13
13
  amq-protocol (~> 2.3, >= 2.3.0)
14
14
  bunny-mock (1.7.0)
15
15
  bunny (>= 1.7)
16
- byebug (11.0.1)
16
+ byebug (11.1.1)
17
17
  diff-lcs (1.3)
18
18
  jaro_winkler (1.5.4)
19
19
  parallel (1.19.1)
20
- parser (2.6.5.0)
20
+ parser (2.7.0.2)
21
21
  ast (~> 2.4.0)
22
22
  rainbow (3.0.0)
23
23
  rake (13.0.1)
@@ -25,26 +25,26 @@ GEM
25
25
  rspec-core (~> 3.9.0)
26
26
  rspec-expectations (~> 3.9.0)
27
27
  rspec-mocks (~> 3.9.0)
28
- rspec-core (3.9.0)
29
- rspec-support (~> 3.9.0)
28
+ rspec-core (3.9.1)
29
+ rspec-support (~> 3.9.1)
30
30
  rspec-expectations (3.9.0)
31
31
  diff-lcs (>= 1.2.0, < 2.0)
32
32
  rspec-support (~> 3.9.0)
33
- rspec-mocks (3.9.0)
33
+ rspec-mocks (3.9.1)
34
34
  diff-lcs (>= 1.2.0, < 2.0)
35
35
  rspec-support (~> 3.9.0)
36
- rspec-support (3.9.0)
37
- rubocop (0.78.0)
36
+ rspec-support (3.9.2)
37
+ rubocop (0.79.0)
38
38
  jaro_winkler (~> 1.5.1)
39
39
  parallel (~> 1.10)
40
- parser (>= 2.6)
40
+ parser (>= 2.7.0.1)
41
41
  rainbow (>= 2.2.2, < 4.0)
42
42
  ruby-progressbar (~> 1.7)
43
43
  unicode-display_width (>= 1.4.0, < 1.7)
44
- rubocop-performance (1.5.1)
44
+ rubocop-performance (1.5.2)
45
45
  rubocop (>= 0.71.0)
46
46
  ruby-progressbar (1.10.1)
47
- unicode-display_width (1.6.0)
47
+ unicode-display_width (1.6.1)
48
48
 
49
49
  PLATFORMS
50
50
  ruby
@@ -60,4 +60,4 @@ DEPENDENCIES
60
60
  rubocop-performance
61
61
 
62
62
  BUNDLED WITH
63
- 2.0.2
63
+ 2.1.4
data/README.md CHANGED
@@ -36,12 +36,32 @@ default: &default
36
36
  auto_delete: false
37
37
  durable: true
38
38
  queues:
39
+ # queue_name => options pairs
39
40
  americas.south:
40
41
  auto_delete: true
41
42
  durable: false
42
43
  exclusive: false
43
44
  handler: "MyApp::SouthAmericaHandler"
44
45
  routing_key: "americas.south.#"
46
+ americas.north:
47
+ auto_delete: true
48
+ durable: false
49
+ exclusive: false
50
+ handler: "MyApp::NorthAmericaHandler"
51
+ routing_key: "americas.north.#"
52
+ # or an array
53
+ - queue_name: americas.south
54
+ auto_delete: true
55
+ durable: false
56
+ exclusive: false
57
+ handler: "MyApp::SouthAmericaHandler"
58
+ routing_key: "americas.south.#"
59
+ # queue_name can be skipped and auto generated from routing key
60
+ - auto_delete: true
61
+ durable: false
62
+ exclusive: false
63
+ handler: "MyApp::SouthAmericaHandler"
64
+ routing_key: "americas.south.#"
45
65
 
46
66
  development:
47
67
  <<: *default
@@ -70,6 +90,7 @@ module MyApp
70
90
  config.logger = Logger.new(IO::NULL)
71
91
  config.publish_options = { mandatory: true, persistent: true }
72
92
  config.yaml_config = "config/jackhammer.yml"
93
+ config.app_name = "my_app"
73
94
  end
74
95
  end
75
96
  end
data/lib/jackhammer.rb CHANGED
@@ -7,6 +7,7 @@ require 'jackhammer/exceptions'
7
7
  require 'jackhammer/log'
8
8
  require 'jackhammer/configuration'
9
9
  require 'jackhammer/message_receiver'
10
+ require 'jackhammer/queue_name'
10
11
  require 'jackhammer/queue'
11
12
  require 'jackhammer/topic'
12
13
  require 'jackhammer/topic_manager'
@@ -1,6 +1,7 @@
1
1
  module Jackhammer
2
2
  class Configuration
3
3
  attr_accessor(
4
+ :app_name,
4
5
  :connection_options,
5
6
  :connection_url,
6
7
  :environment,
@@ -1,17 +1,19 @@
1
1
  module Jackhammer
2
2
  class Queue
3
- def initialize(topic:, queue:, handler:, routing:)
3
+ attr_reader :queue, :handler_object
4
+
5
+ def initialize(topic:, queue:, handler:, routing_key:)
4
6
  @topic = topic
5
7
  @queue = queue
6
- @queue.bind @topic, routing_key: routing
8
+ @queue.bind @topic, routing_key: routing_key
7
9
  @handler_object = handler
8
10
  end
9
11
 
10
12
  def subscribe
11
- @queue.subscribe do |delivery_info, properties, content|
13
+ queue.subscribe do |delivery_info, properties, content|
12
14
  Log.info { [delivery_info.inspect, properties.inspect].join(' || ') }
13
15
  Log.debug { content }
14
- @handler_object.call content
16
+ handler_object.call content
15
17
  rescue StandardError => e
16
18
  Log.error e
17
19
  Jackhammer.configuration.exception_adapter.call e
@@ -0,0 +1,13 @@
1
+ module Jackhammer
2
+ class QueueName
3
+ def self.app_name
4
+ Jackhammer.configuration.app_name
5
+ end
6
+
7
+ def self.from_routing_key(routing_key)
8
+ fail(InvalidConfigError, "app_name must be set to determine queue_name from routing_key") if app_name.to_s.empty?
9
+
10
+ "#{app_name}_#{routing_key}_q".gsub(/[^\w]+/, '_').gsub(/[_]+/, '_')
11
+ end
12
+ end
13
+ end
@@ -1,8 +1,11 @@
1
1
  module Jackhammer
2
2
  class Topic
3
+ QUEUE_NAME_KEY = 'queue_name'.freeze
4
+ ROUTING_KEY_KEY = 'routing_key'.freeze
5
+
3
6
  def initialize(name:, options:, queue_config:)
4
7
  @topic = Jackhammer.channel.topic name, options
5
- @queue_config = queue_config
8
+ @queue_config = normalize_queue_config(queue_config)
6
9
  end
7
10
 
8
11
  def subscribe_queues
@@ -13,18 +16,41 @@ module Jackhammer
13
16
  # for each message published.
14
17
  def publish(message, options)
15
18
  full_options = Jackhammer.configuration.publish_options.dup.merge options
16
- @topic.publish message, full_options
19
+ topic.publish message, full_options
17
20
  end
18
21
 
19
22
  def queues
20
23
  return @queues if @queues
21
24
 
22
- @queues = @queue_config.map do |name, options|
25
+ @queues = queue_config.map do |options|
23
26
  handler = MessageReceiver.new(options.delete('handler'))
24
- routing = options.delete 'routing_key'
25
- queue = Jackhammer.channel.queue name, options
26
- Queue.new topic: @topic, queue: queue, handler: handler, routing: routing
27
+ routing_key = fetch_and_delete_key(options, ROUTING_KEY_KEY)
28
+ queue_name = options.delete(QUEUE_NAME_KEY) || QueueName.from_routing_key(routing_key)
29
+ queue = Jackhammer.channel.queue(queue_name, options)
30
+ Log.info { "'#{queue_name}' configured to subscribe on '#{routing_key}'" }
31
+ Queue.new(topic: topic, queue: queue, handler: handler, routing_key: routing_key)
27
32
  end
28
33
  end
34
+
35
+ private
36
+
37
+ attr_reader :topic, :queue_config
38
+
39
+ # `queue_config` can be either:
40
+ # - an array of options containing `queue_name` key
41
+ # or
42
+ # - a hash containing `queue_name => options` pairs
43
+ def normalize_queue_config(config)
44
+ return config if config.is_a?(Array)
45
+
46
+ config.map do |name, options|
47
+ options[QUEUE_NAME_KEY] = name
48
+ options
49
+ end
50
+ end
51
+
52
+ def fetch_and_delete_key(options, key)
53
+ options.delete(key) || fail(InvalidConfigError, "#{key} not found in #{options.inspect}")
54
+ end
29
55
  end
30
56
  end
@@ -1,3 +1,3 @@
1
1
  module Jackhammer
2
- VERSION = '1.1.0'.freeze
2
+ VERSION = '1.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jackhammer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Serok
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-23 00:00:00.000000000 Z
11
+ date: 2020-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny
@@ -130,6 +130,7 @@ executables:
130
130
  extensions: []
131
131
  extra_rdoc_files: []
132
132
  files:
133
+ - ".dependabot/config.yml"
133
134
  - ".gitignore"
134
135
  - ".rspec"
135
136
  - ".travis.yml"
@@ -148,6 +149,7 @@ files:
148
149
  - lib/jackhammer/log.rb
149
150
  - lib/jackhammer/message_receiver.rb
150
151
  - lib/jackhammer/queue.rb
152
+ - lib/jackhammer/queue_name.rb
151
153
  - lib/jackhammer/server.rb
152
154
  - lib/jackhammer/topic.rb
153
155
  - lib/jackhammer/topic_manager.rb