Skip to content

Commit

Permalink
Move application callback (#764)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmj authored Mar 1, 2020
1 parent 513312b commit c12ebc3
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 61 deletions.
60 changes: 3 additions & 57 deletions lib/hex.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule Hex do
@moduledoc false

use Application

def start() do
{:ok, _} = Application.ensure_all_started(:hex)
end
Expand All @@ -14,35 +12,15 @@ defmodule Hex do
end
end

def start(_, _) do
dev_setup()

Mix.SCM.append(Hex.SCM)
Mix.RemoteConverger.register(Hex.RemoteConverger)

Hex.Version.start()
start_httpc()

opts = [strategy: :one_for_one, name: Hex.Supervisor]
Supervisor.start_link(children(), opts)
# For compatability during development
def start(start_type, start_args) do
Hex.Application.start(start_type, start_args)
end

def version(), do: unquote(Mix.Project.config()[:version])
def elixir_version(), do: unquote(System.version())
def otp_version(), do: unquote(Hex.Utils.otp_version())

defp start_httpc do
:inets.start(:httpc, profile: :hex)

opts = [
max_sessions: 8,
max_keep_alive_length: 4,
keep_alive_timeout: 120_000
]

:httpc.set_options(opts, :hex)
end

if Version.compare(System.version(), "1.2.0") == :lt do
def debug?(), do: false
else
Expand Down Expand Up @@ -130,38 +108,6 @@ defmodule Hex do
end
end

if Mix.env() == :test do
defp children do
import Supervisor.Spec

[
worker(Hex.State, []),
worker(Hex.Server, []),
worker(Hex.Parallel, [:hex_fetcher])
]
end
else
defp children do
import Supervisor.Spec

[
worker(Hex.State, []),
worker(Hex.Server, []),
worker(Hex.Parallel, [:hex_fetcher]),
worker(Hex.Registry.Server, []),
worker(Hex.UpdateChecker, [])
]
end
end

if Mix.env() in [:dev, :test] do
defp dev_setup do
:erlang.system_flag(:backtrace_depth, 20)
end
else
defp dev_setup, do: :ok
end

def create_tar!(_metadata, [], _output),
do:
Mix.raise(
Expand Down
76 changes: 76 additions & 0 deletions lib/hex/application.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
defmodule Hex.Application do
@moduledoc false

use Application

def start(_, _) do
dev_setup()

Mix.SCM.append(Hex.SCM)
Mix.RemoteConverger.register(Hex.RemoteConverger)

Hex.Version.start()
start_httpc()

opts = [strategy: :one_for_one, name: Hex.Supervisor]
Supervisor.start_link(children(), opts)
end

if Mix.env() in [:dev, :test] do
defp dev_setup do
:erlang.system_flag(:backtrace_depth, 20)
end
else
defp dev_setup, do: :ok
end

defp start_httpc() do
:inets.start(:httpc, profile: :hex)

opts = [
max_sessions: 8,
max_keep_alive_length: 4,
keep_alive_timeout: 120_000
]

:httpc.set_options(opts, :hex)
end

defmacrop worker(module) do
if Version.compare(System.version(), "1.5.0") == :lt do
import Supervisor.Spec
quote do: worker(unquote(module), [[]])
else
quote do: unquote(module)
end
end

defmacrop worker(module, args) do
if Version.compare(System.version(), "1.5.0") == :lt do
import Supervisor.Spec
quote do: worker(unquote(module), [unquote(args)])
else
quote do: {unquote(module), unquote(args)}
end
end

if Mix.env() == :test do
defp children do
[
worker(Hex.State),
worker(Hex.Server),
worker(Hex.Parallel, [:hex_fetcher])
]
end
else
defp children do
[
worker(Hex.State),
worker(Hex.Server),
worker(Hex.Parallel, [:hex_fetcher]),
worker(Hex.Registry.Server),
worker(Hex.UpdateChecker)
]
end
end
end
2 changes: 1 addition & 1 deletion lib/hex/parallel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Hex.Parallel do
use GenServer
require Logger

def start_link(name) do
def start_link([name]) do
GenServer.start_link(__MODULE__, [], name: name)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/hex/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Hex.Server do

@name __MODULE__

def start_link(opts \\ []) do
def start_link(opts) do
opts = Keyword.put_new(opts, :name, @name)
GenServer.start_link(__MODULE__, [], opts)
end
Expand Down
9 changes: 8 additions & 1 deletion lib/hex/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,18 @@ defmodule Hex.State do
}
}

def start_link() do
def start_link([]) do
global_config = Hex.Config.read()
Agent.start_link(__MODULE__, :init, [global_config], name: @name)
end

def child_spec(arg) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, [arg]}
}
end

def stop() do
Agent.stop(@name)
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule Hex.MixProject do
def application do
[
applications: applications(Mix.env()),
mod: {Hex, []}
mod: {Hex.Application, []}
]
end

Expand Down

0 comments on commit c12ebc3

Please sign in to comment.