Skip to content

Commit

Permalink
Setup config storage on boot, closes #349
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Feb 12, 2025
1 parent 6fbf87c commit b6eeef7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 14 deletions.
10 changes: 6 additions & 4 deletions lib/broadway/config_storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ defmodule Broadway.ConfigStorage do
@moduledoc false

@doc """
Optional setup for the configuration storage
Optional setup for the configuration storage.
Invoked when Broadway boots.
"""
@callback setup() :: :ok

@doc """
Lists all broadway names in the config storage
Lists all broadway names in the config storage.
"""
@callback list() :: [term()]

Expand All @@ -17,12 +19,12 @@ defmodule Broadway.ConfigStorage do
@callback put(server :: term(), value :: %Broadway.Topology{}) :: term()

@doc """
Retrieves a configuration from the underlying storage
Retrieves a configuration from the underlying storage.
"""
@callback get(server :: term()) :: term()

@doc """
Deletes a configuration from the underlying storage
Deletes a configuration from the underlying storage.
"""
@callback delete(server :: term()) :: boolean()

Expand Down
5 changes: 1 addition & 4 deletions lib/broadway/config_storage/ets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ defmodule Broadway.ConfigStorage.ETS do

@impl true
def setup do
if :undefined == :ets.whereis(@table) do
:ets.new(@table, [:named_table, :public, :set, {:read_concurrency, true}])
end

:ets.new(@table, [:named_table, :public, :set, {:read_concurrency, true}])
:ok
end

Expand Down
5 changes: 0 additions & 5 deletions lib/broadway/topology.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,8 @@ defmodule Broadway.Topology do
@impl true
def init({module, opts}) do
Process.flag(:trap_exit, true)

config_storage = ConfigStorage.get_module()

if Code.ensure_loaded?(config_storage) and function_exported?(config_storage, :setup, 0) do
config_storage.setup()
end

# We want to invoke this as early as possible otherwise the
# stacktrace gets deeper and deeper in case of errors.
{child_specs, opts} = prepare_for_start(module, opts)
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ defmodule Broadway.MixProject do
def application do
[
extra_applications: [:logger],
env: [config_storage: :persistent_term]
env: [config_storage: :persistent_term],
mod: {Broadway.Application, []}
]
end

Expand Down

0 comments on commit b6eeef7

Please sign in to comment.