Skip to content

Commit c11ab4e

Browse files
authored
Support cortex disabled application config.
2 parents cdb42ca + 3dc5e38 commit c11ab4e

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ Then, to run cortex you would start `iex` with the following options:
108108
CORTEX_ENABLED=true iex -S mix
109109
```
110110

111+
An inverted but otherwise identical `disabled` config option is also supported.
112+
This can be used to enable cortex by default but automatically disable it in
113+
certain unwanted contexts (like build environments):
114+
115+
```ex
116+
config :cortex,
117+
disabled: {:system, "CI_RUN", false}
118+
```
111119

112120
## Phoenix
113121

config/config.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ use Mix.Config
2929
#
3030
# import_config "#{Mix.env}.exs"
3131
config :cortex,
32-
enabled: {:system, "CORTEX_ENABLED", true}
32+
enabled: {:system, "CORTEX_ENABLED", true},
33+
disabled: {:system, "CORTEX_DISABLED", false}

lib/cortex/application.ex

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule Cortex.Application do
1111
cond do
1212
Mix.env() in [:dev, :test] ->
1313
children =
14-
if enabled?() do
14+
if autostart?() do
1515
children()
1616
else
1717
[]
@@ -45,17 +45,22 @@ defmodule Cortex.Application do
4545
children ++ env_specific_children
4646
end
4747

48-
defp enabled? do
49-
case Application.get_env(:cortex, :enabled, true) do
50-
bool when is_boolean(bool) ->
51-
bool
48+
defp autostart? do
49+
[enabled, disabled] =
50+
for {config, default} <- [enabled: true, disabled: false] do
51+
case Application.get_env(:cortex, config, default) do
52+
bool when is_boolean(bool) ->
53+
bool
5254

53-
{:system, env_var, default} ->
54-
get_system_var(env_var, default)
55+
{:system, env_var, default} ->
56+
get_system_var(env_var, default)
5557

56-
invalid ->
57-
raise "Invalid config value for Cortex `:enabled`: #{inspect(invalid)}"
58-
end
58+
invalid ->
59+
raise "Invalid config value for Cortex `#{config}`: #{inspect(invalid)}"
60+
end
61+
end
62+
63+
enabled and not disabled
5964
end
6065

6166
defp get_system_var(env_var, default) do

0 commit comments

Comments
 (0)