-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move kafka to extension and implement events (#47)
* refactor: move kafka_ex to extension * feat: implement JSON encoder for Delivery * feat: add kafka events to Delivery * feat: add kafka events to Telegram bot
- Loading branch information
Showing
18 changed files
with
339 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule TololoCore.Kafka.AshNotifier do | ||
use Ash.Notifier | ||
alias Ash.Notifier.Notification | ||
|
||
def notify(%Notification{ | ||
data: resource, | ||
action: %{name: :initialize} | ||
}) do | ||
produce( | ||
resource, | ||
%{event: "delivery_initialized"} | ||
) | ||
end | ||
|
||
def notify(%Notification{ | ||
data: resource, | ||
changeset: %{data: %{state: old_state}}, | ||
action: %{name: :update_state} | ||
}) do | ||
produce( | ||
resource, | ||
%{event: "delivery_state_change", old_state: old_state} | ||
) | ||
end | ||
|
||
def notify(%Notification{ | ||
data: resource, | ||
changeset: %{data: %{state: old_state}}, | ||
action: %{name: :done_with_distance_check} | ||
}) do | ||
produce( | ||
resource, | ||
%{event: "delivery_state_change", old_state: old_state} | ||
) | ||
end | ||
|
||
def notify(_notif), do: nil | ||
|
||
defp produce(resource, data), | ||
do: TololoCore.Kafka.produce("tololo-deliveries", data |> Map.put(:resource, resource)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule TololoCore.Kafka do | ||
@moduledoc """ | ||
A kafka interface to send messages. | ||
""" | ||
|
||
# env put by Kafka extension | ||
defp driver, do: Application.get_env(:tololo, :kafka_driver, TololoCore.Kafka.Noop) | ||
|
||
@doc """ | ||
Produces a message to Kafka. Should be implemented by drivers. | ||
""" | ||
|
||
@spec produce(String.t(), String.t() | map(), term()) :: :ok | {:error, term()} | ||
def produce(topic, message, opts \\ []) | ||
|
||
def produce(topic, message, opts) when is_map(message), | ||
do: produce(topic, Jason.encode(message), opts) | ||
|
||
def produce(topic, message, opts), do: driver().produce(topic, message, opts) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
defmodule TololoCore.Kafka.Noop do | ||
@moduledoc """ | ||
Noop driver implementation for Kafka. Used when Kafka isn't configured, will always return :ok. | ||
""" | ||
|
||
@behaviour TololoCore.Kafka | ||
|
||
@spec produce(String.t(), String.t(), term()) :: :ok | {:error, term()} | ||
def produce(_topic, _message, _opts \\ []), do: :ok | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Tololo.Extensions.Kafka.Driver do | ||
@moduledoc """ | ||
Default driver implementation for Kafka. | ||
""" | ||
|
||
@behaviour TololoCore.Kafka | ||
|
||
@spec produce(String.t(), String.t(), term()) :: :ok | {:error, term()} | ||
def produce(topic, message, opts \\ []) do | ||
KafkaEx.produce(%KafkaEx.Protocol.Produce.Request{ | ||
topic: topic, | ||
partition: 0, | ||
required_acks: 1, | ||
messages: [%KafkaEx.Protocol.Produce.Message{value: message}] | ||
}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
defmodule Tololo.Extensions.Kafka do | ||
@moduledoc false | ||
use Supervisor | ||
@behaviour TololoCore.Extension | ||
|
||
@impl true | ||
def child_spec(init_arg) do | ||
%{ | ||
id: __MODULE__, | ||
start: {__MODULE__, :start_link, [init_arg]}, | ||
type: :supervisor, | ||
restart: :permanent | ||
} | ||
end | ||
|
||
def start_link(_init_arg) do | ||
Application.put_env(:tololo, :kafka_driver, Tololo.Extensions.Kafka.Driver) | ||
Supervisor.start_link(__MODULE__, nil, name: __MODULE__) | ||
KafkaEx.create_worker(:kafka_ex) | ||
end | ||
|
||
@impl true | ||
def init(_) do | ||
children = [ | ||
{Tololo.Extensions.Kafka.Supervisor, max_restarts: 10, max_seconds: 60} | ||
] | ||
|
||
Supervisor.init(children, strategy: :one_for_all) | ||
end | ||
|
||
@impl true | ||
def routes() do | ||
quote do | ||
end | ||
end | ||
|
||
@impl true | ||
def endpoint() do | ||
quote do | ||
end | ||
end | ||
|
||
@impl true | ||
def ash_domains(), do: [] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Tololo.Extensions.Kafka.Supervisor do | ||
@moduledoc """ | ||
Wrapper needed to manually pass the KafkaEx supervisor to the main app's supervision tree. | ||
""" | ||
def start_link(opts) do | ||
KafkaEx.Supervisor.start_link(opts[:max_restarts], opts[:max_seconds]) | ||
end | ||
|
||
def child_spec(opts) do | ||
%{ | ||
id: __MODULE__, | ||
start: {__MODULE__, :start_link, [opts]}, | ||
type: :supervisor, | ||
restart: :permanent, | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule Tololo.Extensions.Kafka.MixProject do | ||
use Mix.Project | ||
|
||
def project do | ||
[ | ||
app: :tololo_extension_kafka, | ||
version: "0.1.0", | ||
elixir: "~> 1.18", | ||
start_permanent: Mix.env() == :prod, | ||
deps: deps() | ||
] | ||
end | ||
|
||
def application do | ||
[ | ||
extra_applications: [:logger] | ||
] | ||
end | ||
|
||
defp deps do | ||
[ | ||
{:kafka_ex, "~> 0.11", runtime: false}, | ||
{:tololo_core, | ||
path: | ||
Path.join(["..", "..", "core"]) | ||
|> Path.expand()} | ||
] | ||
end | ||
end |
Oops, something went wrong.