Skip to content

Commit

Permalink
more updates before merge
Browse files Browse the repository at this point in the history
  • Loading branch information
btkostner committed Oct 2, 2024
1 parent 811d2e7 commit 05f0719
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Features marked with ✅ should be feature complete. Anything with 🟨 is a pla
### Event Layer

- 🟨 Phoenix PubSub setup
- 🟨 Adapter pattern for in application event bus
- ✅ Internal event bus for cross context communication
- 🟨 [Rabbitmq](https://www.rabbitmq.com/) for worker queue message producing
- 🟨 [Broadway](https://elixir-broadway.org/) for worker queue message consuming
- ✅ Document the difference between the three and when to use each
Expand Down
10 changes: 5 additions & 5 deletions docs/events.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Events

Events can be broken down into three different categories. Each category has a specific use case and different messaging garantees, so be mindful when picking.
Events can be broken down into three different categories. Each category has a specific use case and different messaging guarantees, so be mindful when picking.

## PubSub

First up, you have your PubSub events. Internally this is done with Phoenix PubSub and it's a great way to broadcast messages to web clients. This is designed for consumers who connect and disconnect very frequently and where events need to be handled across nodes. If a client is disconnected when a message is broadcasted, it will not be delivered.

## Event Bus

The event bus is a way to deal with cross context information. These are high throughput messages processed completely in memory, and therefor have no garantee of processing aside from what the BEAM can provide. This means messages will be handled unless a major software or hardware failure occures.
The event bus is a way to deal with cross context information. These are high throughput messages processed completely in memory, and therefor have no guarantee of processing aside from what the BEAM can provide. This means messages will be handled unless a major software or hardware error occures.

To summarize, think of these two pieces of psudo code as the same, but one is much easier to maintain and extend.

```elixir
# This code requires you to update code from seperate contexts, making
# This code requires you to update code from separate contexts, making
# it harder to maintain, extend, or remove.
defmodule Accounts do
def create_user(attrs) do
Expand Down Expand Up @@ -64,11 +64,11 @@ defmodule Search do
end
```

The event bus **does not provide retry or replay functionality**. It is purely a better way to decouple contexts. If you find yourself needing more garantees, retries, or are using third party services that has a finiky API, you (as the consumer) will need to handle that yourself. This is to prevent the event bus from becoming a catch all for every type of event, as well as adding complexity to the system.
The event bus **does not provide retry or replay functionality**. It is purely a better way to decouple contexts. If you find yourself needing more guarantees, retries, or are using third party services that has a finiky API, you (as the consumer) will need to handle that yourself. This is to prevent the event bus from becoming a catch all for every type of event, as well as adding complexity to the system.

## Worker Queue

The worker queue has the most garantees and is best suited for long tasks that can be done asynchronously and might need to be retried or replayed at a later date. This is currently done with Broadway and RabbitMQ but can be switched out to Oban or a number of other hosted queue systems. This is a great way to offload work from the main application and keep things running smoothly.
The worker queue has the most guarantees and is best suited for long tasks that can be done asynchronously and might need to be retried or replayed at a later date. This is currently done with Broadway and RabbitMQ but can be switched out to Oban or a number of other hosted queue systems. This is a great way to offload work from the main application and keep things running smoothly.

## What to use and when

Expand Down
14 changes: 0 additions & 14 deletions lib/jumar/audit/account_event_bus_consumer.ex

This file was deleted.

20 changes: 20 additions & 0 deletions lib/jumar/audit/event_bus_consumer.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Jumar.Audit.EventBusConsumer do
@moduledoc false

use Jumar.EventBus.Consumer,
producers: [
{Jumar.Accounts.EventBusProducer, max_demand: 100}
]

@doc false
@impl Jumar.EventBus.Consumer
def handle_event(_event) do
:ok
end

@doc false
@impl Jumar.EventBus.Consumer
def handle_failure(event, error) do
Logger.error("Failed to create audit record of event", event: event, error: inspect(error))

Check warning on line 18 in lib/jumar/audit/event_bus_consumer.ex

View workflow job for this annotation

GitHub Actions / Lint (Credo)

Logger metadata key event, error not found in Logger config.
end
end
2 changes: 1 addition & 1 deletion lib/jumar/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Jumar.Supervisor do
Jumar.PubSub,
# Start the Event Bus system
Jumar.Accounts.EventBusProducer,
Jumar.Audit.AccountEventBusConsumer,
Jumar.Audit.EventBusConsumer,
# Start Finch
{Finch, name: Jumar.Finch}
# Start a worker by calling: Jumar.Worker.start_link(arg)
Expand Down

0 comments on commit 05f0719

Please sign in to comment.