strum-esb 0.1.2 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4620ba956e7e946d3afb71609f957ba669d417cdf25ad7b6f1a4353254bfc7b0
4
- data.tar.gz: 3d7587e8177b03b73e3853cd57aef137641dd937a23181dfa3fa0ec1e3504201
3
+ metadata.gz: 100cac09566f7c972a3ea3cb7005fc3c57ecc20cc7cb4e159b43dcf4b5d9bc24
4
+ data.tar.gz: dd290413c6ae5f8cc50659a487bc656500c04cd27d289cf13f10eaea14e90343
5
5
  SHA512:
6
- metadata.gz: d135016527999dcbe7a6f2140ab6450186ba0f409ea92e1840c895965d5d08a22a69fd161b21adec6ec1b45c2f13adcde9eb9e34aaa2bf040ee306162397f6f8
7
- data.tar.gz: 5cda659f33b6677c8b30c27f82a87e4bf89ca1889340f82c144ea30b3a44b872c59619bb88f727f0143375800e9fe1dcc8b37d8aea0f353d34f765d42bb30016
6
+ metadata.gz: c1a9a87ba8f0207f0de46d08f19229835e29ab844f2053f38a0eb484efc191460d83a6d5e87421d1d4df09152138e753f93705035b4866c6f0ff502b2cffdadf
7
+ data.tar.gz: b3f3d16931dd745a09a7cfbe938b9170c8e4efb4bf511f7de796595ad43ec683cf72c4865cbfe838ee91005b5df1431b32fa12a09fa5426a176a2f99ede62bc3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- strum-esb (0.1.2)
4
+ strum-esb (0.2.0)
5
5
  bunny (~> 2.15)
6
6
  connection_pool (~> 2.2.2)
7
7
  dry-configurable (~> 0.12.1)
@@ -13,10 +13,10 @@ GEM
13
13
  specs:
14
14
  amq-protocol (2.3.2)
15
15
  ast (2.4.2)
16
- bunny (2.18.0)
16
+ bunny (2.19.0)
17
17
  amq-protocol (~> 2.3, >= 2.3.1)
18
18
  sorted_set (~> 1, >= 1.0.2)
19
- concurrent-ruby (1.1.8)
19
+ concurrent-ruby (1.1.9)
20
20
  connection_pool (2.2.5)
21
21
  debase (0.2.4.1)
22
22
  debase-ruby_core_source (>= 0.10.2)
@@ -25,7 +25,7 @@ GEM
25
25
  dry-configurable (0.12.1)
26
26
  concurrent-ruby (~> 1.0)
27
27
  dry-core (~> 0.5, >= 0.5.0)
28
- dry-core (0.5.0)
28
+ dry-core (0.7.1)
29
29
  concurrent-ruby (~> 1.0)
30
30
  json (2.5.1)
31
31
  parallel (1.20.1)
data/README.md CHANGED
@@ -30,11 +30,28 @@ Strum::Esb.config.action_exchange = `demo.actions`
30
30
  Strum::Esb.config.notice_exchange = `demo.notice`
31
31
  ```
32
32
 
33
- Pre publish hooks allow you to configure custom code that will be able to modify properties of the message that will be published or execute any arbitrary logic
33
+ Before publish hooks allow you to configure custom code that will be able to modify properties of the message that will be published or execute any arbitrary logic
34
34
  ```ruby
35
- Strum::Esb.config.pre_publish_hooks << ->(body, properties) { properties[:correlation_id] = "hello world" }
35
+ Strum::Esb.config.before_publish_hooks << ->(body, properties) { properties[:correlation_id] = "hello world" }
36
36
  ```
37
37
 
38
+ Before handler hooks allow you to inspect raw data before any processing has strated:
39
+ ```ruby
40
+ Strum::Esb.config.before_handler_hooks << ->(deserialized_msg, delivery_info, metadata) do
41
+ puts "We don't even know if msg is valid JSON, this is some text from RabbitMQ"
42
+ puts deserialized_msg
43
+ end
44
+ ```
45
+
46
+ After handler hook allows you to have insight on how handler was executed:
47
+ ```ruby
48
+ Strum::Esb.config.after_handler_hooks << ->(deserialized_msg, delivery_info, metadata, payload, error) do
49
+ puts "Message was deserialized succesfully" if payload
50
+ puts "Error occured" if error
51
+ end
52
+ ```
53
+
54
+
38
55
  ### Sending message:
39
56
 
40
57
  ```ruby
@@ -32,9 +32,10 @@ module Strum
32
32
  end
33
33
  end
34
34
 
35
- def work_with_params(deserialized_msg, _delivery_info, metadata)
36
- notice_data = JSON.parse(deserialized_msg)
35
+ def work_with_params(deserialized_msg, delivery_info, metadata)
36
+ Strum::Esb.config.before_handler_hooks.each { |hook| hook.call(deserialized_msg, delivery_info, metadata) }
37
37
 
38
+ payload = JSON.parse(deserialized_msg)
38
39
  snake_case_modify = ->(string) { string.nil? ? nil : string&.to_s.gsub(/[^a-zA-Z0-9]/, "_")&.downcase }
39
40
  parse_header = ->(string) { metadata[:headers] && metadata[:headers][string] }
40
41
  header = parse_header >> snake_case_modify
@@ -64,22 +65,26 @@ module Strum
64
65
  method_name = ([*methods_names] << "handler").find { |n| respond_to?(n, include_all: true) }
65
66
 
66
67
  unless method_name
67
- logger.error "Handler not found. Message rejected #{metadata[:headers]} with payload #{notice_data}"
68
+ logger.error "Handler not found. Message rejected #{metadata[:headers]} with payload #{payload}"
68
69
  return reject!
69
70
  end
70
71
 
72
+ error = nil
71
73
  method_params = method(method_name)
72
74
  .parameters
73
75
  .map { |param| _, param_name = param; param_name }
74
76
  .then { |m| m & %I[action resource event state info chain] }
75
77
  handler_params = method_params.each_with_object({}) { |i, res| res[i] = eval(i.to_s); }
76
- logger.info("Handler #{method_name} found. Payload: #{notice_data}")
77
- handler_params.count.positive? ? send(method_name, notice_data, handler_params) : send(method_name, notice_data)
78
+ logger.info("Handler #{method_name} found. Payload: #{payload}")
79
+ handler_params.count.positive? ? send(method_name, payload, handler_params) : send(method_name, payload)
78
80
  logger.info("Handler #{method_name} executed")
79
81
  ack!
80
82
  rescue StandardError => e
83
+ error = e
81
84
  logger.error e
82
85
  reject!
86
+ ensure
87
+ Strum::Esb.config.after_handler_hooks.each { |hook| hook.call(deserialized_msg, delivery_info, metadata, payload, error) }
83
88
  end
84
89
 
85
90
  def after_headers_hook; end
@@ -12,7 +12,7 @@ module Strum
12
12
  content_type: "application/json"
13
13
  }
14
14
 
15
- Strum::Esb.config.pre_publish_hooks.each { |hook| hook.call(payload, properties) }
15
+ Strum::Esb.config.before_publish_hooks.each { |hook| hook.call(payload, properties) }
16
16
 
17
17
  properties[:headers] = {} unless properties[:headers].is_a?(Hash)
18
18
  properties[:headers]["pipeline"] ||= Thread.current[:pipeline] if Thread.current[:pipeline]
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Strum
4
4
  module Esb
5
- VERSION = "0.1.2"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
data/lib/strum/esb.rb CHANGED
@@ -35,7 +35,9 @@ module Strum
35
35
  end
36
36
  setting :before_fork_hooks, []
37
37
  setting :after_fork_hooks, []
38
- setting :pre_publish_hooks, []
38
+ setting :before_publish_hooks, []
39
+ setting :before_handler_hooks, []
40
+ setting :after_handler_hooks, []
39
41
 
40
42
  Strum::Esb.config.before_fork_hooks << proc { DB.disconnect } if defined?(DB)
41
43
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strum-esb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Serhiy Nazarov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-04 00:00:00.000000000 Z
11
+ date: 2021-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny