event_router 0.1.0 → 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: 50b25ed1a6cbef8f2dc0b4b571d76118e987ed9b123de3f1a759e4393b7e801b
4
- data.tar.gz: 8d2c80e151358fa7fe58a2eff3f0250f2f795eeda327c4248ca554452be5ff24
3
+ metadata.gz: 79dbcbada6f98ca97196267161c48f526c84e4003b9b17f08d802204e9f46772
4
+ data.tar.gz: 7b6bbaeed68e1264329c5ef2e616fcccdab0792b95dfc180af466e1159829838
5
5
  SHA512:
6
- metadata.gz: 713cc074634b7af45b462e4f8f31bb8ea4515aaa3485fb70a7b04bd83fad6136d9924b2a5c33e641c615c10d51c8497d9cb91a1f27a8ea1b1e64de5d22d57112
7
- data.tar.gz: cff521d40abfc0260d5e64e42e2bf7274919e84b0c80e2dcb49edd25e4a42b526099742de7973cf3fbc152b4c548cfe969aacab1dcb952aeda5bd35c9efeb016
6
+ metadata.gz: 5ce10b55197712cf503960918fb7c6936ed334441a882eb374cbb50ca746d1b6d7a5287d8f4b33465e557aba62c85ef80103037dc286ad30277fe0a0d91a5769
7
+ data.tar.gz: 5bddbd23394618c8518b0fb5c0dd50c8af1c645ac2e849f2e3006119594b52e31aa9f1ccb787b32846f10ff32fa48e46c33c10b6760222f36de7618d0c623d97
@@ -7,7 +7,7 @@ jobs:
7
7
  strategy:
8
8
  fail-fast: false
9
9
  matrix:
10
- os: [ubuntu, macos]
10
+ os: [ubuntu]
11
11
  ruby: [2.5, 2.6, 2.7]
12
12
 
13
13
  steps:
data/README.md CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  ![Specs](https://github.com/ahmad-elassuty/event_router/workflows/Specs/badge.svg)
4
4
 
5
- One domain event can have multiple side-effects in your system, for example sending emails, creating notifications, tasks, audit logs, event store, updating other system resources async and many more.
5
+ EventRouter helps you organise your application domain events in a simple and intuitive way.
6
6
 
7
- EventRouter helps you organise your application domain events side-effects in a simple and intuitive way. No more bloated workers that does many actions, which violates Single responsiblity principle.
7
+ One domain event can have multiple side-effects in your system, for example sending emails, creating notifications, tasks, audit logs, event store, updating other system resources async and many more. EventRouter will help you create dedicated classes for your events, that can be consumed by multiple destinations.
8
8
 
9
9
  ## Installation
10
10
 
@@ -22,13 +22,11 @@ Or install it yourself as:
22
22
 
23
23
  $ gem install event_router
24
24
 
25
- EventRouter is already pre-configured with some defaults. Please check the wikis for more info on how to update the configurations.
25
+ EventRouter is pre-configured with some defaults. Please check the [Wiki](https://github.com/ahmad-elassuty/event_router/wiki/Configurations) for more info on how to update the configurations.
26
26
 
27
27
  ## Usage
28
28
 
29
- ### Event
30
-
31
- It is very easy to create a new domain event and deliver it to multiple destinations.
29
+ It is very easy to create a new domain event and deliver it to multiple destinations:
32
30
 
33
31
  - Create your new domain event and define the destination:
34
32
 
@@ -42,7 +40,7 @@ It is very easy to create a new domain event and deliver it to multiple destinat
42
40
 
43
41
  ```ruby
44
42
  class EmailNotifier
45
- def self.order_placed(event, payload)
43
+ def self.order_placed(event:, payload:)
46
44
  # [TODO] Handle the event here
47
45
  end
48
46
  end
@@ -54,11 +52,15 @@ It is very easy to create a new domain event and deliver it to multiple destinat
54
52
  OrderPlaced.publish(order_id: 1, time: Time.now)
55
53
  ```
56
54
 
57
- And your are done! 🎉
55
+ Also you can publish the event async:
56
+
57
+ ```ruby
58
+ OrderPlaced.publish_async(order_id: 1, time: Time.now)
59
+ ```
58
60
 
59
- ### Destinations
61
+ And your are done! 🎉
60
62
 
61
- A single event can define multiple destinations, and each destination can have different set of options.
63
+ An event can also define multiple destinations and each destination can have different set of options:
62
64
 
63
65
  ```ruby
64
66
  class OrderPlaced < EventRouter::Event
@@ -67,30 +69,27 @@ class OrderPlaced < EventRouter::Event
67
69
  end
68
70
  ```
69
71
 
70
- For the full list of options, please check the wikis 📚.
72
+ For the full list of options, please check the [Wiki](https://github.com/ahmad-elassuty/event_router/wiki/Events-And-Destinations) 📚.
71
73
 
72
74
  ### Delivery Adapters
73
75
 
74
- You can configure how your events are delivered to their destinations. EventRouter currently supports two different delivery adapters:
76
+ You can configure how your events are delivered to their consumers. EventRouter currently supports two different delivery adapters:
75
77
 
76
- - Sync
77
- - Sidekiq
78
+ - [x] Sync (default)
79
+ - [x] [Sidekiq](https://github.com/mperham/sidekiq)
78
80
 
79
- The `sync` adapter is the default delivery adapter and does not require any dependencies. To asynchronously process your event, please use other backends, e.g Sidekiq.
81
+ The `sync` adapter is the default delivery adapter. To asynchronously process your event, please use other backends e.g Sidekiq, or create your own custom adapter.
80
82
 
81
- To setup other adapters, please check the wikis 📚.
83
+ To setup other adapters, please check the [Wiki](https://github.com/ahmad-elassuty/event_router/wiki/Configurations) 📚.
82
84
 
83
85
  ### Serializers
84
86
 
85
- Similarly, you can configure how events are serialized before they are handed to the delivery adapter. This is mainly to provide flexibility on how events are stored in the delivery backend and maintain the objects structure.
86
-
87
- - Json
88
- - Oj
89
-
90
- The `json` adapter is the default serializer adapter and does not require any dependencies.
87
+ Similarly, you can configure how events are serialized before they are handed to the delivery backend. This is mainly to provide flexibility on how events are stored and maintain the objects structure on deserialization. EventRouter currently supports two serializer adapters:
91
88
 
92
- To setup other adapters, please check the wikis 📚.
89
+ - [x] Json (default)
90
+ - [x] [Oj](https://github.com/ohler55/oj)
93
91
 
92
+ To change the default serializer adapter, please check the [Wiki](https://github.com/ahmad-elassuty/event_router/wiki/Configurations) 📚.
94
93
 
95
94
  ## Development
96
95
 
@@ -17,6 +17,10 @@ module EventRouter
17
17
  EventRouter::Publisher.publish(events, adapter: adapter)
18
18
  end
19
19
 
20
+ def publish_async(events, adapter: EventRouter.configuration.delivery_adapter)
21
+ EventRouter::Publisher.publish_async(events, adapter: adapter)
22
+ end
23
+
20
24
  def serialize(event, adapter: EventRouter.configuration.serializer_adapter)
21
25
  EventRouter::Serializer.serialize(event, adapter: adapter)
22
26
  end
@@ -21,6 +21,10 @@ module EventRouter
21
21
  def deliver(_event)
22
22
  raise NotImplementedError, "deliver method is not implemented for #{name}"
23
23
  end
24
+
25
+ def deliver_async(_event)
26
+ raise NotImplementedError, "deliver_async method is not implemented for #{name}"
27
+ end
24
28
  end
25
29
  end
26
30
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EventRouter
4
+ module DeliveryAdapters
5
+ module Helpers
6
+ module Deliver
7
+ module_function
8
+
9
+ def yield_destinations(event)
10
+ event.destinations.each do |_name, destination|
11
+ if destination.prefetch_payload?
12
+ payload = destination.extra_payload(event)
13
+ serialized_payload = EventRouter.serialize(payload)
14
+ end
15
+
16
+ yield destination, serialized_payload
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EventRouter
4
+ module DeliveryAdapters
5
+ module Helpers
6
+ module Sidekiq
7
+ module_function
8
+
9
+ def process_event(event, serialized_event: nil)
10
+ serialized_event ||= EventRouter.serialize(event)
11
+
12
+ options = EventRouter::DeliveryAdapters::Sidekiq.options
13
+
14
+ Helpers::Deliver.yield_destinations(event) do |destination, serialized_payload|
15
+ Workers::SidekiqDestinationDeliveryWorker
16
+ .set(queue: options[:queue], retry: options[:retry])
17
+ .perform_async(destination.name, serialized_event, serialized_payload)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,6 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'jobs/sidekiq_event_delivery_job'
3
+ require 'sidekiq'
4
+
5
+ require_relative '../serializer'
6
+
7
+ require_relative 'helpers/deliver'
8
+ require_relative 'helpers/sidekiq'
9
+
10
+ require_relative 'workers/sidekiq_destination_delivery_worker'
11
+ require_relative 'workers/sidekiq_event_delivery_worker'
4
12
 
5
13
  module EventRouter
6
14
  module DeliveryAdapters
@@ -17,18 +25,15 @@ module EventRouter
17
25
  end
18
26
 
19
27
  def deliver(event)
20
- serialized_event = EventRouter.serialize(event)
28
+ Helpers::Sidekiq.process_event(event)
29
+ end
21
30
 
22
- event.destinations.each do |name, destination|
23
- if destination.prefetch_payload?
24
- payload = destination.extra_payload(event)
25
- serialized_payload = EventRouter.serialize(payload)
26
- end
31
+ def deliver_async(event)
32
+ serialized_event = EventRouter.serialize(event)
27
33
 
28
- Jobs::SidekiqEventDeliveryJob
29
- .set(queue: options[:queue], retry: options[:retry])
30
- .perform_async(name, serialized_event, serialized_payload)
31
- end
34
+ Workers::SidekiqEventDeliveryWorker
35
+ .set(queue: options[:queue], retry: options[:retry])
36
+ .perform_async(serialized_event)
32
37
  end
33
38
  end
34
39
  end
@@ -3,11 +3,17 @@
3
3
  module EventRouter
4
4
  module DeliveryAdapters
5
5
  class Sync < Base
6
- def self.deliver(event)
7
- event.destinations.each do |_name, destination|
8
- payload = destination.extra_payload(event)
6
+ class << self
7
+ def deliver(event)
8
+ event.destinations.each do |_name, destination|
9
+ payload = destination.extra_payload(event)
9
10
 
10
- destination.process(event, payload)
11
+ destination.process(event, payload)
12
+ end
13
+ end
14
+
15
+ def deliver_async(event)
16
+ Thread.new { deliver(event) }
11
17
  end
12
18
  end
13
19
  end
@@ -1,12 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../../serializer'
4
- require 'sidekiq'
5
-
6
3
  module EventRouter
7
4
  module DeliveryAdapters
8
- module Jobs
9
- class SidekiqEventDeliveryJob
5
+ module Workers
6
+ class SidekiqDestinationDeliveryWorker
10
7
  include ::Sidekiq::Worker
11
8
 
12
9
  def perform(destination_name, serialized_event, serialized_payload)
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EventRouter
4
+ module DeliveryAdapters
5
+ module Workers
6
+ class SidekiqEventDeliveryWorker
7
+ include ::Sidekiq::Worker
8
+
9
+ def perform(serialized_event)
10
+ event = EventRouter.deserialize(serialized_event)
11
+
12
+ Helpers::Sidekiq.process_event(event, serialized_event: serialized_event)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -48,6 +48,10 @@ module EventRouter
48
48
  def publish(**attrs)
49
49
  EventRouter.publish(new(**attrs))
50
50
  end
51
+
52
+ def publish_async(**attrs)
53
+ EventRouter.publish_async(new(**attrs))
54
+ end
51
55
  end
52
56
  end
53
57
  end
@@ -15,6 +15,12 @@ module EventRouter
15
15
  Array(events).each { |event| adapter_class.deliver(event) }
16
16
  end
17
17
 
18
+ def publish_async(events, adapter:)
19
+ adapter_class = delivery_adapter(adapter)
20
+
21
+ Array(events).each { |event| adapter_class.deliver_async(event) }
22
+ end
23
+
18
24
  def delivery_adapter(adapter)
19
25
  EventRouter.configuration.delivery_adapter_class(adapter)
20
26
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EventRouter
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_router
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ahmad Elassuty
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-23 00:00:00.000000000 Z
11
+ date: 2021-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -143,9 +143,12 @@ files:
143
143
  - lib/event_router.rb
144
144
  - lib/event_router/configuration.rb
145
145
  - lib/event_router/delivery_adapters/base.rb
146
- - lib/event_router/delivery_adapters/jobs/sidekiq_event_delivery_job.rb
146
+ - lib/event_router/delivery_adapters/helpers/deliver.rb
147
+ - lib/event_router/delivery_adapters/helpers/sidekiq.rb
147
148
  - lib/event_router/delivery_adapters/sidekiq.rb
148
149
  - lib/event_router/delivery_adapters/sync.rb
150
+ - lib/event_router/delivery_adapters/workers/sidekiq_destination_delivery_worker.rb
151
+ - lib/event_router/delivery_adapters/workers/sidekiq_event_delivery_worker.rb
149
152
  - lib/event_router/destination.rb
150
153
  - lib/event_router/error.rb
151
154
  - lib/event_router/errors/required_option_error.rb