在 Elixir config 中使用 ENV 的一点技巧

看到了 Erlang Solution 的这篇文章,想到了这个话题。这篇文章虽然内容不多,但还是挺有用的。文中提到的编译时和运行时的区别,也是刚学 Elixir 搞不太清的问题。

而 Elixir 的 config 也并不是简单的启动前执行的代码,特别是在部署时。

有时我们可能会想在 config 里使用 ENV,比如:

config :my_app, api_key: System.get_env("API_KEY")

但直接这样写到 config 中是不行的。在部署时,比如通过 distillery 运行 mix release ,config 就会变成 sys.config,System.get_env(“API_KEY”) 已经被计算了,等运行的时候就不能动态得到 ENV 的实际值了。

有种做法就是像上边那篇文章中最后提到的方式,把 ENV 的获取逻辑放到函数里去做,这样就变成了运行时才会执行了:

# config.exs
config :my_app, api_key: {:env, "API_KEY"}

# my_app.ex
def api_key do
  get_env(Application.get_env(:my_app, :api_key))
end

def get_env({:env, key}), do: System.get_env(key)

Phoenix 的 config 支持从 ENV 中获取 port 就是这样处理的:

# https://github.com/phoenixframework/phoenix/blob/996a83a27d8ccdc7e0e3bdda9c21d537b19b2002/installer/templates/new/config/prod.exs#L15
config :<%= app_name %>, <%= app_module %>.Endpoint,
  http: [:inet6, port: {:system, "PORT"}]

 # https://github.com/phoenixframework/phoenix/blob/2295ba7440221871b64c9535dec404c7d53589eb/lib/phoenix/endpoint/handler.ex#L57
 defp to_port({:system, env_var}), do: to_port(System.get_env(env_var))
 
10
Kudos
 
10
Kudos

Now read this

What’s SSD TRIM and why use it?

最近我们的一个 Mongo cluster 总是在周末的时候有大量的 IO 操作,并严重影响了 Mongo 的性能。然后发现问题出现的时候总是会执行一个 cron job – fstrim: $ cat /etc/crontab | grep weekly 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) $ cat... Continue →