Skip to content

Commit

Permalink
chore: clean up application start
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveigah committed Jun 16, 2024
1 parent b91716c commit 738c025
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ klife-*.tar

# Temporary files, for example, from tests.
/tmp/


example/*
32 changes: 1 addition & 31 deletions lib/klife/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,14 @@ defmodule Klife.Application do

@impl true
def start(_type, _args) do
# TODO: Refactor and rethink auto topic creation feature
create_topics()

children = [
Klife.ProcessRegistry,
Klife.PubSub,
handle_clients()
Klife.PubSub
]

# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Klife.Supervisor]
Supervisor.start_link(List.flatten(children), opts)
end

defp handle_clients() do
[
MyTestClient
]
end

defp create_topics() do
do_create_topics(System.monotonic_time())
end

defp do_create_topics(init_time) do
case Klife.Utils.create_topics!() do
:ok ->
:ok

:error ->
now = System.monotonic_time(:millisecond)

if now - init_time > :timer.seconds(15) do
raise "Timeout while creating topics"
else
do_create_topics(init_time)
end
end
end
end
22 changes: 21 additions & 1 deletion lib/klife/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,27 @@ defmodule Klife.Utils do
# right now there is a bug when the Connection system intialize before
# the topic are created, thats why we need to create a connection from
# scratch here. Must solve it later.
def create_topics!() do
def create_topics() do
do_create_topics(System.monotonic_time())
end

defp do_create_topics(init_time) do
case create_topics_call() do
:ok ->
:ok

:error ->
now = System.monotonic_time(:millisecond)

if now - init_time > :timer.seconds(15) do
raise "Timeout while creating topics"
else
do_create_topics(init_time)
end
end
end

defp create_topics_call() do
client_opts = Application.fetch_env!(:klife, MyTestClient)

conn_defaults =
Expand Down
5 changes: 5 additions & 0 deletions lib/mix/tasks/benchmark.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ if Mix.env() in [:dev] do

def run(args) do
Application.ensure_all_started(:klife)

:ok = Klife.Utils.create_topics()
opts = [strategy: :one_for_one, name: Benchmark.Supervisor]
{:ok, _} = Supervisor.start_link([MyTestClient], opts)

Process.sleep(1_000)
apply(Mix.Tasks.Benchmark, :do_run_bench, args)
end
Expand Down
6 changes: 6 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# ExUnit.configure(exclude: [cluster_change: true])

:ok = Klife.Utils.create_topics()

opts = [strategy: :one_for_one, name: Test.Supervisor]
{:ok, _} = Supervisor.start_link([MyTestClient], opts)

ExUnit.start()

0 comments on commit 738c025

Please sign in to comment.