【问题标题】:What is the correct way to start `:pg`'s default scope in an Elixir 1.12 application?在 Elixir 1.12 应用程序中启动 `:pg` 的默认范围的正确方法是什么?
【发布时间】:2021-08-29 15:02:00
【问题描述】:

:pg2 模块在 OTP 24 中被删除。它的替换是 :pg。根据documentation

默认作用域 pg 在 kernel(6) 配置时自动启动。

从基于混合的 Elixir 1.12 应用程序,配置内核以自动启动默认 :pg 范围的正确方法是什么?到目前为止,我一直在这样做,但似乎真的很难编写没有 start/2 函数的库代码:

defmodule MyApp do
  use Application

  def start(_type, _args) do

    # Ew, gross:
    {:ok, _pid} = :pg.start_link()

    children() |> Supervisor.start_link([strategy: :one_for_one, name: __MODULE__])
  end

  def children, do: []
end

【问题讨论】:

  • ** (Mix) Could not start application pg: could not find application file: pg.app :pg 不是应用程序,它是 Erlang 内核的一部分

标签: elixir erlang-otp


【解决方案1】:

您可以为该模块编写自己的child_spec

defmodule MyApp do
  use Application

  def start(_type, _args) do
    Supervisor.start_link(children(), strategy: :one_for_one, name: __MODULE__)
  end

  defp children, do: [pg_spec()]

  defp pg_spec do
    %{
      id: :pg,
      start: {:pg, :start_link, []}
    }
  end
end

【讨论】:

  • 好主意。这将使监督树干净地建立起来,但它仍然没有像 OTP 文档所说的那样“配置内核”(无论 Elixir 意味着什么)。
  • @Clay 你无法在 Elixir 中轻松配置 kernel。现在最简单的方法可能是在开发中运行 env ERL_FLAGS="-kernel start_pg true" mix 并在生产中完全重启。
猜你喜欢
  • 2010-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-19
  • 2019-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多