Skip to content

Commit

Permalink
implement asynchronous startup procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
asakura committed Nov 16, 2023
1 parent 11d02a7 commit ba5231d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
19 changes: 14 additions & 5 deletions lib/mixpanel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,16 @@ defmodule Mixpanel do
ignore_time: boolean
]

@doc export: true
@impl Application
@spec start(any, any) :: Supervisor.on_start()
def start(_type, _args) do
clients = Mixpanel.Config.clients()
{:ok, pid} = Mixpanel.Supervisor.start_link()
Mixpanel.Supervisor.start_link()
end

for {client, config} <- clients do
@impl Application
@spec start_phase(:clients, :normal, any) :: :ok
def start_phase(:clients, :normal, _phase_args) do
for {client, config} <- Mixpanel.Config.clients() do
case Mixpanel.Supervisor.start_child(config) do
{:ok, _pid} ->
:ok
Expand All @@ -112,9 +115,15 @@ defmodule Mixpanel do
end
end

{:ok, pid}
:ok
end

@impl Application
def prep_stop(state), do: state

@impl Application
def stop(_state), do: :ok

@doc """
Dynamically starts a new client process.
Expand Down
8 changes: 7 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ defmodule Mixpanel.Mixfile do
end

def application() do
[mod: {Mixpanel, []}, extra_applications: [:logger]]
[
mod: {Mixpanel, []},
extra_applications: [:logger],
start_phases: [
clients: []
]
]
end

defp compilers(:dev) do
Expand Down

0 comments on commit ba5231d

Please sign in to comment.