Skip to content

Commit

Permalink
Avoid using Mix.env at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcooper committed Dec 9, 2020
1 parent d9fe429 commit 1c73cb0
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ config :unleash, Unleash,
backup_file: nil, # Backup file in the event that contacting the server fails
custom_http_headers: [], # A keyword list of custom headers to send to the server
disable_client: false, # Whether or not to enable the client
disable_metrics: false # Whether or not to send metrics,
disable_metrics: false, # Whether or not to send metrics,
retries: -1 # How many times to retry on failure, -1 disables limit
app_env: :dev # Which environment we're in
```

`:custom_http_headers` should follow the format prescribed by
Expand Down
3 changes: 2 additions & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ config :unleash, Unleash,
url: "http://localhost:4242/api",
appname: "dev",
instance_id: "dev",
metrics_period: 15 * 1000
metrics_period: 15 * 1000,
app_env: :dev
8 changes: 7 additions & 1 deletion lib/unleash/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ defmodule Unleash.Config do
disable_metrics: false,
retries: -1,
client: Unleash.Client,
http_client: Mojito
http_client: Mojito,
app_env: :test
]

def url do
application_env()
|> Keyword.fetch!(:url)
end

def test? do
application_env()
|> Keyword.fetch!(:app_env) == :test
end

def appname do
application_env()
|> Keyword.fetch!(:appname)
Expand Down
4 changes: 2 additions & 2 deletions lib/unleash/metrics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Unleash.Metrics do
def start_link(state, opts \\ []) do
{:ok, pid} = GenServer.start_link(__MODULE__, state, opts)

unless Code.ensure_loaded?(Mix) and Mix.env() == :test do
unless Config.test? do
initialize(pid)
end

Expand All @@ -53,7 +53,7 @@ defmodule Unleash.Metrics do
{:noreply, send_metrics(state)}
end

if Code.ensure_loaded?(Mix) and Mix.env() == :test do
if Config.test? do
def handle_call(:send_metrics, _from, state) do
{:reply, :ok, send_metrics(state)}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/unleash/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Unleash.Repo do
def start_link(state) do
{:ok, pid} = GenServer.start_link(__MODULE__, state, name: Unleash.Repo)

unless Code.ensure_loaded?(Mix) and Mix.env() == :test do
unless Config.test? do
initialize()
end

Expand Down

0 comments on commit 1c73cb0

Please sign in to comment.